Cacheable注解使用详解
完成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基础配置中,有一个地方是配置缓存默认失效时间的。
//设置缓存过期时间
cacheManager.setDefaultExpiration(30);
- 这里是将缓存默认的失效设置为30秒,但是我们有的数据需要单独配置,配置方法如下:
//针对key单独设置过期时间
Map<String, Long> expireMap = new HashMap<String, Long>();
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_AD_LIST, 5 * 60L);
cacheManager.setExpires(expireMap);
- 在RedisConfig.java类,cacheManager方法中增加如下配置。增加后的RedisConfig.java如下:
package com.shanyuan.platform.ms.base.cache.config;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.shanyuan.platform.ms.base.cache.serializer.FastJson2JsonRedisSerializer;
import com.shanyuan.platform.ms.base.common.CommonRedisKey;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
//缓存管理器
@Bean
public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisCacheManager cacheManager = null;
if(factory instanceof JedisConnectionFactory) {
JedisConnectionFactory jcf = (JedisConnectionFactory) factory;
JedisPoolConfig npc = (JedisPoolConfig) jcf.getPoolConfig().clone();
JedisConnectionFactory njcf= new JedisConnectionFactory(npc);
njcf.setHostName(jcf.getHostName());
njcf.setPort(jcf.getPort());
njcf.setPassword(jcf.getPassword());
njcf.setTimeout(jcf.getTimeout());
njcf.setDatabase(0);
njcf.setUsePool(true);
njcf.afterPropertiesSet();
@SuppressWarnings("rawtypes")
RedisTemplate ntemplate = new StringRedisTemplate(njcf);
setSerializer(ntemplate);//设置序列化工具
ntemplate.afterPropertiesSet();
cacheManager = new RedisCacheManager(ntemplate);
}
if(cacheManager==null) {
cacheManager = new RedisCacheManager(redisTemplate);
}
//设置缓存过期时间
cacheManager.setDefaultExpiration(30);
//针对key单独设置过期时间
Map<String, Long> expireMap = new HashMap<String, Long>();
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_AD_LIST, 5 * 60L);
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST, 5 * 60L);
expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_SUPPORT_AREA_LIST, 24 * 60 * 60L);
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_HELP_GOODS, 6 * 60 * 60L);
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_SPECIAL_GOODS, 30 * 60L);
expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_UNIONITEM_GOODS, 6 * 60 * 60L);
expireMap.put(CommonRedisKey.BizGoodsClass.BIZ_GOODS_CLASS_SET, 6 * 60 * 60L);
expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_GOODS_CLASS + "_set", 6 * 60 * 60L);
expireMap.put(CommonRedisKey.GoodsFilterRedisKey.DACS_SUPPORT_AREA_LIST, 6 * 60 * 60L);
cacheManager.setExpires(expireMap);
return cacheManager;
}
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
if(factory instanceof JedisConnectionFactory) {
JedisConnectionFactory jcf = (JedisConnectionFactory) factory;
jcf.setDatabase(3);
}
StringRedisTemplate template = new StringRedisTemplate(factory);
setSerializer(template);//设置序列化工具
template.afterPropertiesSet();
return template;
}
private void setSerializer(RedisTemplate template){
FastJson2JsonRedisSerializer<object width="300" height="150"> fastJsonRedisSerializer = new FastJson2JsonRedisSerializer(Object.class);template.setValueSerializer(fastJsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());}}```* 这里用Map<String, Long>,其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注解使用详解相关推荐
- spring之旅第四篇-注解配置详解
spring之旅第四篇-注解配置详解 一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会 ...
- Java 注解用法详解——@SuppressWarnings
转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解--@SuppressWarnings 一.前言 编码时我们总 ...
- Java注解(Annotation)详解
转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...
- Spring 3.0 注解注入详解
Spring 3.0 注解注入详解 2011-04-15 09:44 17ZOUGUO ITEYE博客 我要评论(1) 字号:T | T AD: 一.各种注解方式 1.@Autowired注解(不推荐 ...
- spring MVC请求处理类注解属性详解
spring MVC请求处理类注解属性详解
- Retrofit 注解参数详解
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/121000230 本文出自[赵彦军的博客] 系列文章推荐: Android Flow ...
- JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited)
转载自 JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited) jdk1.5起开始提供了4个元注解,用来定义自定义注解的注解,它们 ...
- Spring 注解@Value详解
一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plaincopy @Value("#{con ...
- SpringBoot - @EnableConfigurationProperties注解使用详解
@EnableConfigurationProperties注解的作用是什么? 将标注了@ConfigurationProperties注解的类注入到Spring容器中.该注解是用来开启对@Confi ...
最新文章
- UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multib...
- php有意思知识分享,分享几个有意思的数组方法
- 联想n308 android 一体机,附文:N308设计回顾_联想 N308_一体电脑评测-中关村在线...
- 有关identity的小技巧
- [vc]如何对radio按钮分组
- c# list集合根据某个字段去重_Python list、dataframe去重
- FileNotFoundError: Could not find module xxx\Library\bin\geos_c.dl paddle安装采坑
- elementUI表单验证
- 最优阈值生长算法_手淘搜索阈值刻度表:让你更加清楚类目搜索增长的规律
- Rulo扫地机器人app_米家扫拖机器人1T测评|米家扫拖机器人1T的3D避障实际效果如何?...
- [javascript] Promise API
- 三成手机电子书暗藏陷阱 诱骗下载强行吸费
- 使用opencv和python进行智能图像处理pdf_OpenCV图像处理编程实例 PDF 高清版
- python制造童年回忆:猫和老鼠小游戏【附源码】
- abb机器人goto指令用法_详解ABB机器人编程含中英文指令对照
- python 多行注释后可运行程序报错
- Flyway 报错:Detected applied migration not resolved locally:2和执行脚本错误
- 基于python的数据挖掘实验报告_数据挖掘实验报告一
- 多线程有几种实现方法,同步有几种实现方法
- python生成统计图_用python Linux(无GUI)中生成统计图
热门文章
- Web前端开发笔记——第三章 CSS语言 第八节 CSS3文本文字设置
- 程序员圣诞节相册源码_程序员分享圣诞刷屏源码,这次朋友圈千万不要再@微信官方了!...
- Shell脚本函数(函数传参、递归、创建库)
- eclipse/myeclipse高亮显示相同变量名 .
- java的int、char、long、float、double对byte的转换,在通信的时候会用到
- java委托机制教程_通过反射实现Java下的委托机制代码详解
- keystore文件_如何手动给APK文件签名
- ef 排序string转int_Java排序算法——基数排序(Radix Sort)
- 最小二乘多项式拟合程序matlab,最小二乘法的多项式拟合(matlab实现)
- java 远程查看电脑磁盘,请问你如何在Java中监视计算机的CPU、内存和磁盘使用情况?...