<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${boot.version}</version>
</dependency>
spring:
application:
name: spring-boot-redis
redis:
host: 192.168.145.132
port: 6379
timeout: 20000
cluster:
nodes: 192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002
maxRedirects: 6
pool:
max-active: 8
min-idle: 0
max-idle: 8
max-wait: -1
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
@Service
public class PersonService {
@Autowired
private PersonRepo personRepo;
/**
* @Cacheable 应用到读取数据的方法上,先从缓存中读取,如果没有再从DB获取数据,然后把数据添加到缓存中
* unless 表示条件表达式成立的话不放入缓存
* @param username
* @return
*/
@Cacheable(value = "user", key = "#root.targetClass + #username", unless = "#result eq null")
public Person getPersonByName(String username) {
Person person = personRepo.getPersonByName(username);
return person;
}
/**
* @CachePut 应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存
* @param person
* @return
*/
@CachePut(value = "user", key = "#root.targetClass + #result.username", unless = "#person eq null")
public Person savePerson(Person person) {
return personRepo.savePerson(person);
}
/**
* @CacheEvict 应用到删除数据的方法上,调用方法时会从缓存中删除对应key的数据
* @param username
* @return
*/
@CacheEvict(value = "user", key = "#root.targetClass + #username", condition = "#result eq true")
public boolean removePersonByName(String username) {
return personRepo.removePersonByName(username) > 0;
}
public boolean isExistPersonName(Person person) {
return personRepo.existPersonName(person) > 0;
}
}
@Component
@Path("personMgr")
public class PersonMgrResource {
@Autowired
private PersonService personService;
@GET
@Path("getPersonByName")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp getPersonByName(@QueryParam("username") String username) {
Person person = personService.getPersonByName(username);
return JsonResp.success(person);
}
@POST
@Path("removePersonByName")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp removePersonByName(@QueryParam("username") String username) {
if (personService.removePersonByName(username)) {
return JsonResp.success();
}
return JsonResp.fail("系统错误!");
}
@POST
@Path("savePerson")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp savePerson(Person person) {
if (personService.isExistPersonName(person)) {
return JsonResp.fail("用户名已存在!");
}
if (personService.savePerson(person).getId() > 0) {
return JsonResp.success();
}
return JsonResp.fail("系统错误!");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有