完成Redis基础配置之后,就可以使用Redis对数据进行缓存了。

  • 最简单的方式就是使用springframe为我们提供的@Cacheable注解,以下是@Cacheable注解的具体使用方式。

@Cacheable注解参数解读

  • 示例

@Cacheable(value = CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST, key = "'" + CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST + "_' + #channel")


  • value

用来存放我们要保存的key的集合。类似我们之前定义的"uiset",类型为标准的String


  • key

我们实际要保存到redis的key,可以增加参数,以方法的参数或者属性。类型为String,但是需要做处理。 需要将我们自定义的字符串以"'"括起来再与参数进行拼接。如果需要用到方法中的参数,可以用 #+参数名直接获 取。如果需要用到方法中参数的属性,可以向Java对象一样,用 . 获取。如 #channel.name。


  • condition

触发条件。这个参数是规定这个缓存触发的条件拼接。如 condition="#channel != null",就是在channel不 为null的时候触发。


  • unless

排除条件。这个参数是规定这个缓存在什么时候不处罚。如 unless="#result == null",就是在结果为null的 时候触发。


缓存设置失效时间

  • 在之前Redis基础配置中,有一个地方是配置缓存默认失效时间的。

  1. //设置缓存过期时间

  2. cacheManager.setDefaultExpiration(30);

  • 这里是将缓存默认的失效设置为30秒,但是我们有的数据需要单独配置,配置方法如下:

  1. //针对key单独设置过期时间

  2. Map<String, Long> expireMap = new HashMap<String, Long>();

  3. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_AD_LIST, 5 * 60L);

  4. cacheManager.setExpires(expireMap);

  • 在RedisConfig.java类,cacheManager方法中增加如下配置。增加后的RedisConfig.java如下:

  1. package com.shanyuan.platform.ms.base.cache.config;

  2. import java.util.HashMap;

  3. import java.util.Map;

  4. import org.springframework.cache.CacheManager;

  5. import org.springframework.cache.annotation.CachingConfigurerSupport;

  6. import org.springframework.cache.annotation.EnableCaching;

  7. import org.springframework.context.annotation.Bean;

  8. import org.springframework.context.annotation.Configuration;

  9. import org.springframework.data.redis.cache.RedisCacheManager;

  10. import org.springframework.data.redis.connection.RedisConnectionFactory;

  11. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

  12. import org.springframework.data.redis.core.RedisTemplate;

  13. import org.springframework.data.redis.core.StringRedisTemplate;

  14. import org.springframework.data.redis.serializer.StringRedisSerializer;

  15. import com.shanyuan.platform.ms.base.cache.serializer.FastJson2JsonRedisSerializer;

  16. import com.shanyuan.platform.ms.base.common.CommonRedisKey;

  17. import redis.clients.jedis.JedisPoolConfig;

  18. @Configuration

  19. @EnableCaching

  20. public class RedisConfig extends CachingConfigurerSupport{

  21. //缓存管理器

  22. @Bean

  23. public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {

  24. RedisConnectionFactory factory = redisTemplate.getConnectionFactory();

  25. RedisCacheManager cacheManager = null;

  26. if(factory instanceof JedisConnectionFactory) {

  27. JedisConnectionFactory jcf = (JedisConnectionFactory) factory;

  28. JedisPoolConfig npc = (JedisPoolConfig) jcf.getPoolConfig().clone();

  29. JedisConnectionFactory njcf= new JedisConnectionFactory(npc);

  30. njcf.setHostName(jcf.getHostName());

  31. njcf.setPort(jcf.getPort());

  32. njcf.setPassword(jcf.getPassword());

  33. njcf.setTimeout(jcf.getTimeout());

  34. njcf.setDatabase(0);

  35. njcf.setUsePool(true);

  36. njcf.afterPropertiesSet();

  37. @SuppressWarnings("rawtypes")

  38. RedisTemplate ntemplate = new StringRedisTemplate(njcf);

  39. setSerializer(ntemplate);//设置序列化工具

  40. ntemplate.afterPropertiesSet();

  41. cacheManager = new RedisCacheManager(ntemplate);

  42. }

  43. if(cacheManager==null) {

  44. cacheManager = new RedisCacheManager(redisTemplate);

  45. }

  46. //设置缓存过期时间

  47. cacheManager.setDefaultExpiration(30);

  48. //针对key单独设置过期时间

  49. Map<String, Long> expireMap = new HashMap<String, Long>();

  50. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_AD_LIST, 5 * 60L);

  51. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST, 5 * 60L);

  52. expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_SUPPORT_AREA_LIST, 24 * 60 * 60L);

  53. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_HELP_GOODS, 6 * 60 * 60L);

  54. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_SPECIAL_GOODS, 30 * 60L);

  55. expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_UNIONITEM_GOODS, 6 * 60 * 60L);

  56. expireMap.put(CommonRedisKey.BizGoodsClass.BIZ_GOODS_CLASS_SET, 6 * 60 * 60L);

  57. expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_GOODS_CLASS + "_set", 6 * 60 * 60L);

  58. expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_SUPPORT_AREA_LIST, 6 * 60 * 60L);

  59. cacheManager.setExpires(expireMap);

  60. return cacheManager;

  61. }

  62. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){

  63. if(factory instanceof JedisConnectionFactory) {

  64. JedisConnectionFactory jcf = (JedisConnectionFactory) factory;

  65. jcf.setDatabase(3);

  66. }

  67. StringRedisTemplate template = new StringRedisTemplate(factory);

  68. setSerializer(template);//设置序列化工具

  69. template.afterPropertiesSet();

  70. return template;

  71. }

  72. private void setSerializer(RedisTemplate template){

  73. FastJson2JsonRedisSerializer<object width="300" height="150"> fastJsonRedisSerializer = new FastJson2JsonRedisSerializer(Object.class);template.setValueSerializer(fastJsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());}}```* 这里用Map&lt;String, Long&gt;,其key是@Cacheable注解中的 value 属性, value是要是设置的有效期,单位为秒。* 配置完之后,需要将配置应用到项目中,必须执行这行代码,否则配置是不生效的。```cacheManager.setExpires(expireMap);```----------------------------------------#### 注意!!!* 在使用这个方式对数据进行缓存的时候,还需要注意一下几点。||注意事项||-----|:-----:|-----:|| 1. | 如果缓存的类的构造器为有参构造时,必须保证该类有无参构造 || 2. | key与value属性为必填属性,且值不能相同 || 3. | key拼接的时候注意使用 ' ,否则无法解析 || 4. | 尽量使用unless或者condition去限制缓存触发机制,防止缓存中进入无效数据 || 5. | 尽量对自定义的缓存进行expire配置,即过期时间,每种数据需要的缓存时间可能是不一样的,尽量单独配置 || 6. | 配置类中expireMap的key,是@Cacheable注解中 value 属性,不需要对key设置时效,这么做就够了 |----------------------------------------</object>

转载于:https://my.oschina.net/u/2617082/blog/1592527

Cacheable注解使用详解相关推荐

  1. spring之旅第四篇-注解配置详解

    spring之旅第四篇-注解配置详解 一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会 ...

  2. Java 注解用法详解——@SuppressWarnings

    转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解--@SuppressWarnings 一.前言 编码时我们总 ...

  3. Java注解(Annotation)详解

    转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...

  4. Spring 3.0 注解注入详解

    Spring 3.0 注解注入详解 2011-04-15 09:44 17ZOUGUO ITEYE博客 我要评论(1) 字号:T | T AD: 一.各种注解方式 1.@Autowired注解(不推荐 ...

  5. spring MVC请求处理类注解属性详解

    spring MVC请求处理类注解属性详解

  6. Retrofit 注解参数详解

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/121000230 本文出自[赵彦军的博客] 系列文章推荐: Android Flow ...

  7. JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited)

    转载自 JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited) jdk1.5起开始提供了4个元注解,用来定义自定义注解的注解,它们 ...

  8. Spring 注解@Value详解

    一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plaincopy @Value("#{con ...

  9. SpringBoot - @EnableConfigurationProperties注解使用详解

    @EnableConfigurationProperties注解的作用是什么? 将标注了@ConfigurationProperties注解的类注入到Spring容器中.该注解是用来开启对@Confi ...

最新文章

  1. UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multib...
  2. php有意思知识分享,分享几个有意思的数组方法
  3. 联想n308 android 一体机,附文:N308设计回顾_联想 N308_一体电脑评测-中关村在线...
  4. 有关identity的小技巧
  5. [vc]如何对radio按钮分组
  6. c# list集合根据某个字段去重_Python list、dataframe去重
  7. FileNotFoundError: Could not find module xxx\Library\bin\geos_c.dl paddle安装采坑
  8. elementUI表单验证
  9. 最优阈值生长算法_手淘搜索阈值刻度表:让你更加清楚类目搜索增长的规律
  10. Rulo扫地机器人app_米家扫拖机器人1T测评|米家扫拖机器人1T的3D避障实际效果如何?...
  11. [javascript] Promise API
  12. 三成手机电子书暗藏陷阱 诱骗下载强行吸费
  13. 使用opencv和python进行智能图像处理pdf_OpenCV图像处理编程实例 PDF 高清版
  14. python制造童年回忆:猫和老鼠小游戏【附源码】
  15. abb机器人goto指令用法_详解ABB机器人编程含中英文指令对照
  16. python 多行注释后可运行程序报错
  17. Flyway 报错:Detected applied migration not resolved locally:2和执行脚本错误
  18. 基于python的数据挖掘实验报告_数据挖掘实验报告一
  19. 多线程有几种实现方法,同步有几种实现方法
  20. python生成统计图_用python Linux(无GUI)中生成统计图

热门文章

  1. Web前端开发笔记——第三章 CSS语言 第八节 CSS3文本文字设置
  2. 程序员圣诞节相册源码_程序员分享圣诞刷屏源码,这次朋友圈千万不要再@微信官方了!...
  3. Shell脚本函数(函数传参、递归、创建库)
  4. eclipse/myeclipse高亮显示相同变量名 .
  5. java的int、char、long、float、double对byte的转换,在通信的时候会用到
  6. java委托机制教程_通过反射实现Java下的委托机制代码详解
  7. keystore文件_如何手动给APK文件签名
  8. ef 排序string转int_Java排序算法——基数排序(Radix Sort)
  9. 最小二乘多项式拟合程序matlab,最小二乘法的多项式拟合(matlab实现)
  10. java 远程查看电脑磁盘,请问你如何在Java中监视计算机的CPU、内存和磁盘使用情况?...