1、xml配置

 <bean id="poolConfigTax" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis_tax.maxIdle}" />
<property name="minIdle" value="${redis_tax.minIdle}" />
<property name="maxTotal" value="${redis_tax.maxTotal}" />
<property name="testOnBorrow" value="${redis_tax.testOnBorrow}" />
</bean>
<!-- Tax redis 数据库 -->
<bean id="connectionFactoryTax" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis_tax.host}" p:port="${redis_tax.port}" p:password="${redis_tax.pass}" p:pool-config-ref="poolConfigTax"
p:database="0"/>
<!--redis操作模版,使用该对象可以操作redis -->
<bean id="redisTemplateTax" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="connectionFactoryTax" />
<!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! -->
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<!--开启事务 -->
<property name="enableTransactionSupport" value="false"></property>
</bean>
<!-- 配置RedisCacheManager -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplateTax" />
</bean>
<cache:annotation-driven cache-manager="redisCacheManager"/>

  

2、缓存注解@Cacheable、@CacheEvict、@CachePut详解
一、@Cacheable用法详解
1、用在哪里?用在方法或者类上。
2、这两种用法有什么区别?
 用在方法上表示:该方法的返回值将被缓存起来 
 用在类上表示:表示该类的所有方法都支持该注解
3、使用后的结果是什么?下次使用相同方法和相同参数调用这个方法的时候将直接从缓存取值,而不需要再次执行该方法。
4、返回值在缓存中怎么存储的?以键值对的方式存储在缓存中,value就是返回值,key由两种策略生成:默认策略和自定义策略
5、默认策略和默认策略怎么用?
  默认策略:在value值后双“::”拼接,形参列表,当形参是对象时,使用json格式:

@CacheConfig(cacheNames="enterprise")//<!-- 声明缓存使用的缓存名称 -->
public interface EnterpriseRepo extends JpaRepository<Enterprise, Integer>,JpaSpecificationExecutor<Enterprise>{
@Cacheable(value="cash1")
Enterprise findByid(Integer id);
@CachePut(value="cash1")
Enterprise save(Enterprise enterprise); }

  自定义策略:key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。

自定义策略是指我们可以通过Spring的EL表达式来指定我们的key。这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。下面是几个使用参数作为key的示例。

@Cacheable(value="users", key="#id")

   public User find(Integer id) {
return null;
}
@Cacheable(value="users", key="#p0")
public User find(Integer id) {
return null;
}
@Cacheable(value="users", key="#user.id")
public User find(User user) {
return null;
} @Cacheable(value="users", key="#p0.id")
public User find(User user) {
return null;
}

  除了上述使用方法参数作为key之外,Spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。

当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为Spring默认使用的就是root对象的属性。如:

@Cacheable(value={"users", "xxx"}, key="caches[1].name")

public User find(User user) {
return null;
}

6、condition属性指定发生的条件

有的时候我们可能并不希望缓存一个方法所有的返回结果。通过condition属性可以实现这一功能。condition属性默认为空,表示将缓存所有的调用情形。其值是通过SpringEL表达式来指定的,当为true时表示进行缓存处理;当为false时表示不进行缓存处理,即每次调用该方法时该方法都会执行一次。如下示例表示只有当user的id为偶数时才会进行缓存。

@Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
public User find(User user) {
System.out.println("find user by user " + user);
return user;
}

  

二、@CachePut

在支持Spring Cache的环境下,对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
  一般使用在保存,更新方法中。

@CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的。

@CachePut(“users”)//每次都会执行方法,并将结果存入指定的缓存中

public User find(Integer id) {
return null;
}

  

三、@CacheEvict

@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与@Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的(对应Cache的名称);key表示需要清除的是哪个key,如未指定则会使用默认策略生成的key;condition表示清除操作发生的条件。下面我们来介绍一下新出现的两个属性allEntries和beforeInvocation。
1、 allEntries属性
allEntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率。

@CacheEvict(value="users", allEntries=true)
public void delete(Integer id) {
System.out.println("delete user by id: " + id);
}

 
2、beforeInvocation属性
 清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间,当我们指定该属性值为true时,Spring会在调用该方法之前清除缓存中的指定元素。

@CacheEvict(value="users", beforeInvocation=true)
public void delete(Integer id) {
System.out.println("delete user by id: " + id);
}

spring redis 注解实现缓存机制的更多相关文章

  1. Spring控制Hibernate的缓存机制ehcache

    首先在spring.xml中进入bean <prop key="hibernate.cache.use_second_level_cache">true</pro ...

  2. Spring Boot2(二):使用Spring Boot2集成Mybatis缓存机制

    前言 学习SpringBoot集成Mybatis的第二章,了解到Mybatis自带的缓存机制,在部署的时候踩过了一些坑.在此记录和分享一下Mybatis的缓存作用. 本文章的源码再文章末尾 什么是查询 ...

  3. Spring基于注解ehCache缓存整合

    注解的使用参照:http://blog.csdn.net/wjacketcn/article/details/50945887 (侵删) ehCache是纯java实现的缓存组件,spring从3.1 ...

  4. Spring Cacheable 注解不缓存null值

    用Cacheable注解时,发现空值,也会被缓存下来.如果我们期望空值不被缓存,可以做如下设置: @Cacheable(key = "#id", unless="#res ...

  5. spring 缓存机制

    简介 Spring3.1开始引入了基于注释的缓存,其使用方法和原理类似于Spring对事务管理的支持.可以对容器中的任意的bean或bean的方法添加缓存.   配置Spring缓存 Spring缓存 ...

  6. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  7. 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例

    之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...

  8. 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

    本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...

  9. Mybatis整合Redis实现二级缓存

    Mybatis集成ehcache . 为什么需要缓存 拉高程序的性能 . 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 . ehcac ...

随机推荐

  1. win10 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...

  2. 【GO】【sublime】

    1.首先下载GO的安装包:https://golang.org/doc/install#testing 找到上面一个Download超大按钮,找不到的可以不用再看了. 下载完成,安装. 安装成功后,启 ...

  3. 716-River Crossing

    深奥dp,状态转来转去,反正就是能解题 #include <cstdio> #include <iostream> #include <cstring> #incl ...

  4. jenkins配置QQ邮箱自动发送RF测试构建结果通知邮件

    声明:转载请注明出处,谢谢 首先确认QQ邮箱SMTP服务器的地址和端口号.如下图所示,请谨记,JENKINS全局邮箱配置需要使用: 步骤1:开启QQ邮箱的smtp服务:登陆QQ邮箱-设置-账户-开启P ...

  5. Flutter采坑之路 Run Configuration error:broken configuration due to unavailable

    今天把AndroidStudio升级成了3.3.1 原先还能编译成功的Flutter工程突然连编译都不行了, 错误是 Run Configuration error:broken configurat ...

  6. python学习(五)

  7. Lua 语言变量

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量.编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  8. serial front_door signment and gps signment

    import socketimport serialimport osimport sysimport struct#serial ser_intf = serial.Serial(port='/de ...

  9. position的sticky与fixed

    fixed(固定定位) 生成绝对定位的元素,相对于浏览器窗口进行定位.元素的位置通过 "left", "top", "right" 以及 & ...

  10. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...