Springboot~@Cacheable非侵入式缓存
早在很多年前,我曾经封装过关于.net unity aop的缓存[https://www.cnblogs.com/lori/p/5169420.html],面向方法的缓存,而如今,spring早已经集成了这个技术,并且得到了广大的应用。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
开启缓存功能
@SpringBootApplication
@EnableCaching
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
缓存功能失灵
- 检查redis依赖是否有问题,版本等
- 配置里是否关闭了redis自动配置功能
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
添加缓存
/**
* 不参数的
* redis key list::SimpleKey []
* @return
*/
@Cacheable(value = "list", key = "")
@GetMapping("list")
public ResponseEntity list() {
return ResponseEntity.ok(
new Info("zzl", "lind",new Date())
);
}
/**
* 带参数的,支持实体类型
* redis key list::name,instance of list::zhansan
* @param name
* @return
*/
@GetMapping("detail")
@Cacheable(value = "list", key = "#p0")
public ResponseEntity listOne(@RequestParam String name) {
return ResponseEntity.ok(new Info("zzl", "lind", new Date())
);
}
清除缓存
/**
* del redis key for list::SimpleKey []
* @return
*/
@GetMapping("del")
@CacheEvict(value = "list")
public String delAll() {
return "ok";
}
/**
* del redis key for list::name
* @param name
* @return
*/
@GetMapping("del/{name}")
@CacheEvict(value = "list", key = "#p0")
public String del(@PathVariable String name) {
return "ok";
}
定义redis缓存策略
/**
* spring cache配置,以json的方式存储到redis.
*
* @return
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
).entryTtl(Duration.ofMinutes(30));
return redisCacheConfiguration;
}
自定义序列化后的截图,改为json
- 之前是二进制的,可读性不高
- 正在改为json的,提高可读性
合集:
springboot(1)
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-06-04 java~mac下的终端工具oh-my-zsh
2015-06-04 MongoDB学习笔记~大叔框架实体更新支持N层嵌套~递归递归我爱你!
2014-06-04 MVVM架构~knockoutjs系列之一些异常的总结(永久更新)
2014-06-04 Nginx系列~概念与windows下环境搭建
2012-06-04 架构,改善程序复用性的设计~第六讲 我的系统结构~将所有可以抽象的项目进行抽象(大结局)