@Cacheable(value = CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST, key = "'" + CommonRedisKey.IndexRedisKey.INDEX_FOCUS_LIST + "_' + #channel")java
用来存放咱们要保存的key的集合。相似咱们以前定义的"uiset",类型为标准的Stringredis
咱们实际要保存到redis的key,能够增长参数,以方法的参数或者属性。类型为String,可是须要作处理。 须要将咱们自定义的字符串以"'"括起来再与参数进行拼接。若是须要用到方法中的参数,能够用 #+参数名直接获 取。若是须要用到方法中参数的属性,能够向Java对象同样,用 . 获取。如 #channel.name。spring
触发条件。这个参数是规定这个缓存触发的条件拼接。如 condition="#channel != null",就是在channel不 为null的时候触发。缓存
排除条件。这个参数是规定这个缓存在何时不处罚。如 unless="#result == null",就是在结果为null的 时候触发。less
//设置缓存过时时间 cacheManager.setDefaultExpiration(30);
//针对key单独设置过时时间 Map<String, Long> expireMap = new HashMap<String, Long>(); expireMap.put(CommonRedisKey.IndexRedisKey.INDEX_AD_LIST, 5 * 60L); cacheManager.setExpires(expireMap);
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>