Cacheable註解使用詳解

完成Redis基礎配置以後,就可使用Redis對數據進行緩存了。

  • 最簡單的方式就是使用springframe爲咱們提供的@Cacheable註解,如下是@Cacheable註解的具體使用方式。

@Cacheable註解參數解讀

  • 示例

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


  • value

用來存放咱們要保存的key的集合。相似咱們以前定義的"uiset",類型爲標準的Stringredis


  • key

咱們實際要保存到redis的key,能夠增長參數,以方法的參數或者屬性。類型爲String,可是須要作處理。 須要將咱們自定義的字符串以"'"括起來再與參數進行拼接。若是須要用到方法中的參數,能夠用 #+參數名直接獲 取。若是須要用到方法中參數的屬性,能夠向Java對象同樣,用 . 獲取。如 #channel.name。spring


  • condition

觸發條件。這個參數是規定這個緩存觸發的條件拼接。如 condition="#channel != null",就是在channel不 爲null的時候觸發。緩存


  • unless

排除條件。這個參數是規定這個緩存在何時不處罰。如 unless="#result == null",就是在結果爲null的 時候觸發。less


緩存設置失效時間

  • 在以前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>
相關文章
相關標籤/搜索