以Redis为缓存模块的SpringCache使用
来源:互联网 发布:windows.old怎么使用 编辑:程序博客网 时间:2024/06/16 16:13
1、pom文件中,导入maven依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency>
2、在SpringMVC配置文件中,引入classes类路径下的Redis配置文件
<context-property-placehoder location="classpath:redis.properties" />
3、配置缓存管理器,开启Spring对缓存的支持
<!--开启Spring对缓存的支持--> <cache:annotation-driven /> <!--配置以Redis为缓存模块的缓存管理器--> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory"></property> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> <!--配置缓存管理器--> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" > <constructor-arg ref="redisTemplate"></constructor-arg> </bean>
4、在Service的查询方法中使用缓存
public class ServiceImpl{ //自定义的存储空间名称 @Cacheble(value="namespace",key="caches[0].name.concat(':').concat(#argument)") public Object findByArgument(argument){ . . . return Object; }}
5、定义使缓存失效的方法,一般为对所缓存对象的增、删的方法,如果方法参数不能获取与缓存对象对应的key值,应该使在方法参数中增加一个key值
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.classException.class}) //更新缓存对象时,使缓存失效,下次使用时执行真正的查询动作@CacheEvict(value="namespace",key="(#argument)")public int updateMethod(Object argument) throws Exception { . . .}@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.class, Exception.class})//删除所缓存对象时,使已经缓存的对象失效@CacheEvict(value="namespace",key="(#argument)") public int updateCredenceForEdit(Object argument) throws Exception { . . .}
阅读全文
0 0
- 以Redis为缓存模块的SpringCache使用
- Scrapy Pipeline之使用专门的Twisted客户端(以Redis缓存为例)
- springCache使用
- redis缓存的使用
- Redis-Cluster集群整合SpringCache
- Redis缓存的若干使用
- redis缓存的使用总结
- SpringCache(Spring使用Cache)
- redis结合ssm框架中的使用,以AOP原理做一个redis缓存管理
- 以SQLServer2005为数据库的数据库缓存的实现
- Django 缓存 使用 Redis Memcached 为网站提速
- springcache使用笔记001_注释驱动的 Spring cache 简单使用
- springcache使用笔记002_注释驱动的 Spring cache 按条件查询
- redis缓存的安装和使用
- 使用redis缓存的实践总结
- redis缓存的安装和使用
- 使用redis缓存的实践总结
- redis缓存的安装和使用
- CentOS6 脚本自动安装redis sentinel
- cocoapods最新使用方法
- 据说美剧《硅谷》是程序员成功人士的必看剧?
- C++中struct 和 class的sizeof大小
- 客户端检测
- 以Redis为缓存模块的SpringCache使用
- python学习(八)之递归函数
- 多线程开发之读写锁
- 远程服务器上文件的Last-Modified 格式化
- 【计算机网络】第2章 物理层 知识总结
- vmware中扩展linux的磁盘
- Spring笔记(二)
- JVM随笔
- [编程题] 头条校招