#缓存默认有效期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); }