#緩存默認有效期1h (60 * 60 = 3600秒)
redis.expiration=3600
#最大空閒數,數據庫鏈接的最大空閒時間。超過空閒時間,數據庫鏈接將被標記爲不可用,而後被釋放。設爲0表示無限制。
redis.maxIdle=300
#鏈接池的最大數據庫鏈接數。設爲0表示無限制。
#Redis默認容許客戶端鏈接的最大數量是10000。如果看到鏈接數超過5000以上,那可能會影響Redis的性能。假若一些或大部分客戶端發送大量的命令過來,這個數字會低的多。
redis.maxActive=5000
#最大創建鏈接等待時間。若是超過此時間將接到異常。設爲-1表示無限制。
redis.maxWait=-1
#申請鏈接時檢測鏈接是否有效,配置true會下降性能,可是能夠檢測連接有效性,默認false
redis.testOnBorrow=true
#返回前會先校驗這個連接有效性,若是無效會被銷燬,默認值false
redis.testOnReturn=true
redis.database=0
#緩存時間範圍
cache.cacheTime=300,400
#同步等待時間
cache.syncWaitTime=300
#空值緩存時間
cache.nullCacheTime=60
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.3.xsd">
<description>Jedis Configuration</description>
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config/config.properties"/>
<!-- ******************** redis緩存 **********************-->
<!-- 啓用緩存註解功能,不然註解不會生效 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- redis 相關配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
p:database="${redis.database}" p:timeout="${redis.timeout}"
p:pool-config-ref="poolConfig" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<!--對key的序列化器 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!--是對value的列化器 默認:JdkSerializationRedisSerializer -->
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
</bean>
<!-- 擴展RedisCacheManager -->
<bean id="cacheManager" class="com.we.core.web.cache.TFRedisCacheManager">
<constructor-arg ref="redisTemplate" />
<!-- 是否使用前綴 默認: -->
<!--<property name="usePrefix" value="true" />-->
<!-- 默認有效期1h (60 * 60 = 3600秒) -->
<property name="defaultExpiration" value="${redis.expiration}" />
</bean>
<!-- ******************** redis緩存 **********************-->
</beans>
<import resource="spring-data-redis.xml" />
基於註解的緩存聲明,須要掌握的有:@Cacheable、@CachePut 、 @CacheEvict 和@Cachinggit
如下列舉了幾種經常使用的使用屬性,詳情可自行查閱github
@Cacheable(value="",condition="",key="",unless="")
public @interface Cacheable{
String[] value(); //緩存的名字,能夠把數據寫到多個緩存,咱們擴展了屬性:area#60*10, area是value,60*10是緩存時間(單位秒),不加走默認(1h)
String key() default ""; //緩存key,若是不指定將使用默認的KeyGenerator生成,後邊介紹
String condition() default ""; //知足緩存條件的數據纔會放入緩存,condition在調用方法以前和以後都會判斷
String unless() default ""; //用於否決緩存更新的,不像condition,該表達只在方法執行以後判斷,此時能夠拿到返回值result進行判斷了
String keyGenerator() default ""; //指定key規則
}
@CachePut(value="",condition="",key="",unless="")
public @interface CachePut {
String[] value(); //緩存的名字,能夠把數據寫到多個緩存
String key() default ""; //緩存key,若是不指定將使用默認的KeyGenerator生成,後邊介紹
String condition() default ""; //知足緩存條件的數據纔會放入緩存,condition在調用方法以前和以後都會判斷
String unless() default ""; //用於否決緩存更新的,不像condition,該表達只在方法執行以後判斷,此時能夠拿到返回值result進行判斷了
}
@Cacheable(value="",condition="",key="",unless="")
public @interface CacheEvict {
String[] value(); //緩存的名字,能夠把數據寫到多個緩存
String key() default ""; //緩存key,若是不指定將使用默認的KeyGenerator生成,後邊介紹
String condition() default ""; //知足緩存條件的數據纔會放入緩存,condition在調用方法以前和以後都會判斷
boolean allEntries() default false; //是否移除全部數據
boolean beforeInvocation() default false;//是調用方法以前移除/仍是調用以後移除
@Caching(value="",condition="",key="",unless="")
public @interface Caching {
Cacheable[] cacheable() default {}; //從緩存獲取多個,若是沒有則執行方法體,獲取值後加入緩存
CachePut[] put() default {}; //緩存多個
CacheEvict[] evict() default {}; //從緩存移除多個
}
較經常使用,用在查詢方法上,先從緩存中讀取,若是緩存不存在再調用該方法獲取數據,而後把返回的數據添加到緩存中去web
正如其名字,@Cacheable用於添加在需高速緩存的方法上。這些方法默認會以參數爲主鍵把返回結果存儲到高速緩存中,以便在隨後的調用(使用相同的參數)方法,直接返回高速緩存中的值,不須要實際執行此方法。redis
最簡單的方式,只須要聲明一個相關緩存策略的名稱spring
@Cacheable("area#60*10")
public Book getAreaVersion4(String code) {...}
也能夠設置多個緩衝塊,其中一個緩衝塊命中即會返回,並會同步其餘緩存塊:數據庫
@Cacheable(value = {"area#60*10","city#60*30"})
public Book getAreaVersion4(String code) {...}
定製key,且加同步緩存
@Cacheable(value = {"area#60*10"}, key = "'code:'+#code",sync = true)
public AreaDto getAreaVersion4(String code) {
return areaBaseService.get (code);
}
定製key,且加同步,加條件less
@Cacheable(value = {"area#60*10"}, key = "'code:'+#code",sync = true, condition = "#code=='100000'")
public AreaDto getAreaVersion4(String code) {
return areaBaseService.get (code);
}
主要對方法配置,用來標記要清空緩存的方法,當這個方法被調用並知足必定條件後,即會清空緩存。異步
value:緩存的位置,不能爲空。
key:緩存的key,默認爲空。
condition:觸發的條件,只有知足條件的狀況纔會清楚緩存,默認爲空,支持SpEL。
allEntries:true表示清除value中的所有緩存,默認爲false。性能
/**
* 狀況area#60*10下全部緩存
*/
@CacheEvict(value = {"area#60*10"}, key = "'code:'+#code", condition = "#code=='100000'")
public AreaDto delete(String code) {
return areaBaseService.delete (code);
}
/**
* 只要執行了delArea2方法,就刷新緩存名爲」getAreaVersion4」下面的全部緩存
*/
@Caching(evict = {@CacheEvict(value = {"getAreaVersion4#60*5"}, allEntries = true)})
public void delArea2() {
}
主要針對方法的配置,可以根據方法的請求參數對其結果進行緩存,和@Cacheable不一樣的是,它每次都會觸發真實方法的調用。
@CachePut(value = {"area#60*10"}, key = "'code:'+#code",sync = true, condition = "#code=='100000'")
public AreaDto getAreaVersion4(String code) {
return areaBaseService.get (code);
}
spring-data-redis
在apollo中添加 或者 在config/config.properties中添加
優先級順序:一、apollo;二、配置文件;
#緩存時間範圍
cache.cacheTime=300,400
#同步等待時間
cache.syncWaitTime=3
#空值緩存時間
cache.nullCacheTime=60
使用demo
/**
* 目標方法
* <p&
* 支持限流
* 支持穿透
* 支持異步處理
* </p&
*
* @param code
* @return
*/
@TFCacheable(groupName = CACHE_GROUP_NAME, cacheTime = {300, 400}, syncWaitTime = 300)
public AreaDto getXXX(String code) {
return xxxBaseService.get (code);
}
註解介紹
/**
* 分組名
*/
String groupName() default "";
/**
* 緩存的時間範圍
* <br/&
* 過時時間,單位爲秒
*
* <p&
* 格式:minTime-maxTime,如:60-120
* </p&
*/
int[] cacheTime() default 0;
/**
* 是否同步
* 同步排隊時間:{@link #syncWaitTime}.
* <p&
* 細粒度同步鎖,鎖定級別:參數級別
* </p&
* @see #syncWaitTime
*/
boolean sync() default true;
/**
* 同步等待時間
* <br/&
* 過時時間,單位爲秒
*
* <p&
* 過時時間,單位爲秒
* 若是開啓同步,默認排隊時間,超事後,拋超時異常
* </p&
* @see #sync
*/
int syncWaitTime() default 0;
/**
* 空值緩存時間
*
* <p&
* 空值會緩存短暫的時間,防止方法請求不斷請求數據庫,減小穿透概率
* </p&
*/
int nullCacheTime() default 0
@Cacheable(value = "ActivityScopeRedis#60*60",key = "#root.methodName + #activityId") public List<ActivityScopeDto> findByActivityId(long activityId) { return activityScopeBaseDao.findByActivityId(activityId); }