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&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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值