Spring Cache使用RedisCache案例解析

网友投稿 252 2022-12-24


Spring Cache使用RedisCache案例解析

一、RedisCache使用演示

Redis是一个key-value存储系统,在web应用上被广泛应用,这里就不对其过多描述了。

本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用RedisCache作为缓存,我们先引入相关依赖。

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-redis

然后SpringBoot配置文件中,对redis进行配置。

server:

port: 10900

spring:

profiles:

active: dev

redis:

host: localhost #redis服务器地址

port: 6379 #redis端口

password: 1234 #redis密码

timeout: 60000 #连接超时时间

database: 0 #数据库索引,默认为0

SpringBoot中使用Redis,可以通过Spring Cache的注解,也可以使用RedisTemplate来实现,大部分情况下,因为注解存在一定局限性不够灵活,一般实际开发中都是使用的RedisTemplate。

附上CacheConfig注入RedisTemplate,如果不需要使用RedisTemplate,直接将@EnableCaching注解加在SpringBoot启动类上即可。

@Configuration

@EnableCaching

public class CacheConfig {

@Bean

public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {

RedisTemplate redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactoryhttp://(connectionFactory);

// 使用Jackson2jsonAzpGpMgvRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)

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);

redisTemplate.setValueSerializer(serializer);

// 使用StringRedisSerializer来序列化和反序列化redis的key值

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.afterPropertiesSet();

return redisTemplate;

}

}

这样就可以开始使用RedisCache了,测试代码与Spring Boot集成Spring Cache一致。

CacheApi接口调用类,方便调用进行测试。

@RestController

@RequestMapping("cache")

public class CacheApi {

@Autowired

private CacheService cacheService;

@GetMapping("get")

public User get(@RequestParam int id){

return cacheService.get(id);

}

@PostMapping("set")

public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){

User u = new User(code, name);

return cacheService.set(id, u);

}

@DeleteMapping("del")

public void del(@RequestParam int id){

cacheService.del(id);

}

}

CacheService缓存业务处理类,添加缓存,更新缓存和删除。

@Slf4j

@Service

public class CacheService {

private Map dataMap = new HashMap (){

{

for (int i = 1; i < 100 ; i++) {

User u = new User("code" + i, "name" + i);

put(i, u);

}

}

};

// 获取数据

@Cacheable(value = "cache", key = "'user:' + #id")

public User get(int id){

log.info("通过id{}查询获取", id);

return dataMap.get(id);

}

// 更新数据

@CachePut(value = "cache", key = "'user:' + #id")

public User set(int id, User u){

log.info("更新id{}数据", id);

dataMap.put(id, u);

return u;

}

//删除数据

@CacheEvict(value = "cache", key = "'user:' + #id")

public void del(int id){

log.info("删除id{}数据", id);

AzpGpMgv dataMap.http://remove(id);

}

}

启动服务进行测试,可以看到缓存功能正常,且打开redis进行查看,也能看到对应的缓存数据。

源码地址:https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 必知必会的 URL 和 URLConnection使用
下一篇:Spring Boot集成Spring Cache过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~