spring和ehcache整合,实现基于注解的缓存实现
来源:互联网 发布:印度 网络空间 编辑:程序博客网 时间:2024/05/22 06:32
首先需要在spring的配置文件中添加对缓存注解的实现:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="com.xxx" /> <!-- 启用缓存注解功能 --> <cache:annotation-driven cache-manager="ehcacheManager"/> <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManagerFactory" /> </bean></beans>
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="300" timeToLiveSeconds="1800" eternal="false" overflowToDisk="true" /> <cache name="springCache" maxElementsInMemory="300" timeToLiveSeconds="1800" eternal="false" overflowToDisk="true"/></ehcache>
注解的使用:
@Servicepublic class MyServiceImpl implements MyService { ...... @Cacheable(value="springCache",key="'userlist_' + #param['username']") public List<Map<String, Object>> queryUserList(Map<String, Object> param) throws Exception { return userDAO.queryUserList(param); } ......}
添加了@Cacheable的注解,value属性设为固定值springCache,该值对应ehcache.xml文件中cache的name,key属性设为缓存的名称,可以是一个固定值,比如’userlist’,也可以添加上请求方法的属性值,比如’userlist_’ + #param[‘username’],用于确保输入参数不同,输出的值不同的情况。
除了@Cacheable注解,还有@CachePut和@CacheEvict两个注解,区别在于:
@Cacheable:如果使用@Cacheable注解,先检查对应的缓存是否存在,如果缓存存在,直接取缓存值返回,不再执行方法体内容;如果缓存不存在,则执行方法体内容,并且将返回的内容写入缓存
@CachePut:使用@CachePut注解和@Cacheable注解类似,只是不管对应缓存是否存在,都执行方法体,并且将返回的内容写入缓存
@CacheEvict:@CacheEvict注解表示对应缓存的删除
缓存的使用总结:
在读取数据时,使用@Cacheable注解将数据加入缓存
在数据发生改变时,使用@CachePut注解更新缓存
在数据被删除时,使用@CacheEvict注解删除缓存
阅读全文
0 0
- spring和ehcache整合,实现基于注解的缓存实现
- Spring基于注解整合Hibernate EhCache实现对象缓存
- Spring基于注解ehCache缓存整合
- Spring整合Mybaits实现ehcache 注解查询缓存
- Spring整合Mybaits实现ehcache 注解查询缓存
- spring整合ehcache实现缓存
- ehcache-02 : spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
- Spring + ehcache 缓存配置,注解实现
- Spring MVC3整合缓存Ehcache实现
- Spring Boot整合Ehcache实现缓存功能
- Spring与ehcache整合,通过注解的方式使用缓存
- EhCache 缓存 使用注解与Spring整合
- 【spring-boot】spring-boot整合ehcache实现缓存机制
- ehcache通过注解实现缓存
- Spring基于注解的缓存配置--EHCache AND OSCache
- Spring基于注解的缓存配置--EHCache AND OSCache
- 刷新iframe页面$('#iframe').attr('src', $('#iframe').attr('src'));
- WorkFlow工作流
- 关于FreeRTOS源码分析的论坛
- 分享一个整理了很多Android开发工具类的链接AndroidUtilCode
- spring中每个jar的作用
- spring和ehcache整合,实现基于注解的缓存实现
- hadoop2.7.3清洗服务器访问日志之partitioner的学习和应用(六)
- js跨域访问post
- 分割一个整数的各个数字
- GridLayout初识
- Python 安装 SQLAlchemy(未完成)
- IBM developerWorks linux 技术论坛
- canvas实现环形文本
- Unity工程Jenkins配置笔记