Spring 之 Cacheable

Cacheable用来做缓存,

Cache需要在启动类上加入

@SpringBootApplication
@EnableCaching
public class PaymentApplication {public static void main(String[] args) {SpringApplication springApplication = new SpringApplication(PaymentApplication.class);springApplication.run(args);}
}

然后在方法上加入@Cacheable(cacheNames = "hehe")

Spring是怎么识别Cacheable注解的

在启动类中我们加入了EnableCaching 注解,

在注解中import了org.springframework.cache.annotation.CachingConfigurationSelector,这个类会默认选择org.springframework.context.annotation.AutoProxyRegistrarorg.springframework.cache.annotation.ProxyCachingConfiguration

org.springframework.cache.annotation.ProxyCachingConfiguration中 定义了切面类,并定义切点为

static {CACHE_OPERATION_ANNOTATIONS.add(Cacheable.class);CACHE_OPERATION_ANNOTATIONS.add(CacheEvict.class);CACHE_OPERATION_ANNOTATIONS.add(CachePut.class);CACHE_OPERATION_ANNOTATIONS.add(Caching.class);}

这四种类型。然后AOP注册切面。

CacheManager

Spring中实现了一部分的CacheManager。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2DglyJSx-1656295215912)(C:\Users\jm011254\AppData\Roaming\Typora\typora-user-images\image-20220624171210471.png)]

RedisCache的配置

redis:time-to-live: 60  //超时时间key-prefix: aa //前缀cache-null-values: off //redis是否存空的值use-key-prefix: true //是否打开redis前缀

不同的key,自定义不同的配置

上面redis的配置是统一的,如果我们需要对不同的key执行不同的缓存策略。那么我们可以

public RedisCacheManagerBuilderCustomizer getRedisCacheManagerBuilderCustomizer(CacheProperties cacheProperties){return new RedisCacheManagerBuilderCustomizer() {@Overridepublic void customize(RedisCacheManagerBuilder builder) {Map<String, RedisCacheConfiguration> cacheConfigurationMap = new HashMap<>();RedisCacheConfiguration redisCacheConfiguration = createConfiguration(cacheProperties,getClass().getClassLoader());redisCacheConfiguration.entryTtl(Duration.ofMillis(3000));cacheConfigurationMap.put("aa",redisCacheConfiguration);builder.withInitialCacheConfigurations(cacheConfigurationMap);}};}private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(CacheProperties cacheProperties, ClassLoader classLoader) {Redis redisProperties = cacheProperties.getRedis();org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig();config = config.serializeValuesWith(SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));if (redisProperties.getTimeToLive() != null) {config = config.entryTtl(redisProperties.getTimeToLive());}if (redisProperties.getKeyPrefix() != null) {config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()) {config = config.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()) {config = config.disableKeyPrefix();}return config;}

Spring cacheable相关推荐

  1. Spring@Cacheable注解在类内部调用失效的问题

    如图所示,getRecomendGoogs方法里面调用findImgUrlByName方法,在findImgUrlByName方法上使用@Cacheable注解的时候,并没有走缓存. 要解决这个问题, ...

  2. 【Spring】Spring @Cacheable 官方学习及demo

    文章目录 前言 @Cacheable 的来源 应用场景 集成Redis的思路 代码及验证 后记 前言 Spring 有很多声明式的编程风格,@Transactional 是,@Cacheable 也是 ...

  3. 为@Cacheable设置TTL – Spring

    今天,我被要求为应用程序正在使用的某些键设置缓存的过期时间,因此我Swift开始寻找Spring @Cacheable表示法提供的所有选项,以设置过期时间或生存时间. 由于Spring没有提供任何可配 ...

  4. Spring中使用缓存时你应该知道的知识

    2019独角兽企业重金招聘Python工程师标准>>> 常见问题 缓存穿透,雪崩,击穿 下面的文章里都有详细介绍 http://blog.csdn.net/zeb_perfect/a ...

  5. Spring Boot 集成 Redis 实现缓存机制

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  6. 缓存设计方案 你了解吗 SpringBoot 快速集成实现一级缓存Redis和二级缓存Caffeine 可自定义扩展

    项目背景: 在系统初期为了实现业务的快速增长.对系统没有很好的设计.后期想实现缓存,提升应用系统的性能. 面临问题: 缓存配置混乱.系统微服务化基本上需要实现缓存.序列化方式不统一 研发效率低下.每次 ...

  7. Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx \s2018 s4 doc compc dtS44 \s2018 s4 doc dvc

    Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx \s2018 s4 doc compc dtS44 \s2018 s4 doc dvcCo ...

  8. Java开发中的工具类——基于JedisPool的Redis工具类

    目录 一.Maven依赖 二.Redis配置类 三.使用@Cacheable注解进行数据缓存 四.自定义Redis工具类及使用 4.1 序列化工具类 4.2 redis客户端工具类 4.3 redis ...

  9. SpringBoot集成Redis和配置Redis做缓存

    Redis介绍 Redis是一个开源的.高性能的.基于键值对的缓存与存储系统,通过提供多种键值数据类型来适应不同场景下的缓存与存储需求,直观的存储结构使得通过程序交互十分简单. Redis数据库中所有 ...

最新文章

  1. 《DL/T 1476-2015 电力安全工器具预防性试验规程》中的样品名称及试验项目
  2. 如何让类对象只在栈(堆)上分配空间?(转)
  3. pku 3270 Cow Sorting 置换群
  4. java xml 默认名称空间 xpath_创意产业园办公空间设计
  5. 为什么不懂技术的人可以做产品经理(下)?
  6. IDEA引入外部jar包的方法
  7. 实验五 burpsuite重放攻击实验
  8. Linkage 使用方法总结
  9. gbox推荐源_分享一批自己用的软件源 gbox软件源
  10. easyboot的一个严重不足
  11. 一种简单的生成伪随机数的方法(翻译)
  12. CC00054.bigdatajava——|Java分支结构.V04|——|Java.v04|ifelse.v02|判断负数和非负数|
  13. 负载均衡(Load Balance)介绍
  14. c#自学二之KTV点歌系统
  15. matlab生成棋盘格网的命令,Matlab 画棋盘格
  16. 2016年最新版App内购买详细指南
  17. 上周技术关注:疯狂的社会性应用开发平台-NING
  18. web期末大作业-前端网页--H5--海贼王动态网页源码-海贼王网页
  19. 本地Java项目连接Linux系统上的Redis
  20. META-INF/MANIFEST.MF 文件

热门文章

  1. 解决谷歌浏览器打开是2345主页的问题
  2. 破局“人工智能+大数据”产业痛点,保险极客百万医疗震撼出击
  3. 2021.12.13 - 176.保持城市天际线
  4. D-脱硫生物素dethiobiotinCAS号:533-48-2
  5. 青柚文案:水果店青柚推广文案,水果青柚广告文案
  6. 2)美国佬与才女薛涛的共同点
  7. Hadoop之图解MapReduce与WordCount示例分析
  8. 夏季室内甲醛净化 A.O.史密斯数显甲醛净化器让用户更放心
  9. python 32bit? 64bit?
  10. Kaggle_NBME NLP比赛Baseline详解(2)