Spring整合ehcache
来源:互联网 发布:linux显示文本名 编辑:程序博客网 时间:2024/06/11 15:35
配置文件
导入maven坐标
<!-- ehcache缓存 --><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version></dependency>
Spring整合ehcache包 spring-context-support包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version></dependency>
导入ehcache.xml配置文件
解压ehcache-core.jar包,将ehcache-failsafe.xml 复制src/main/resources 改名为ehcache.xml,并自定义缓存区
<cache name="bos" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache>
将ehcacheManager交给spring管理
<!-- 缓存配置 --><bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /></bean>
整合ehcache
配置shiro封装缓存管理器
<!-- spring 封装ehcache缓存管理器 --> <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager" /> </bean>
将shiro的缓存管理器,注入到安全管理器中
<!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="bosRealm" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean>
对认证数据、授权数据哪些进行缓存
配置Realm
注意:使需要缓存对象,实现Serializable接口
// 自定义Realm ,实现安全数据 连接// @Service("bosRealm")public class BosRealm extends AuthorizingRealm
<!-- 配置Realm --><bean id="bosRealm" class="com.kayo.bos.realm.BosRealm"> <!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name --> <property name="authorizationCacheName" value="bos" /></bean>
@Cacheable、@CacheEvict缓存注解的使用
参考spring整合ehcache管理CacheManager,前三步是一样的,接下来继续配置
配置spring缓存管理器,封装ehcache自带CacheManager
<!-- spring 封装ehcache缓存管理器 --><bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager" /></bean>
在applicationContext.xml中引入cache名称空间
xmlns:cache="http://www.springframework.org/schema/cache"
激活spring缓存注解
当spring整合缓存框架后,提供了一套注解
<!-- 激活spring 缓存注解(这个注解是可以让工程中其他类中,通过注解可以快速方便使用缓存技术 --> <cache:annotation-driven cache-manager="springCacheManager"/>
在被spring管理bean对象方法上使用@Cacheable、@CacheEvict
@Cacheable应用缓存区,对方法返回结果进行缓存,用于查询方法
@CacheEvict清除缓存区数据,用于增加、修改、删除方法
在applicationContext-ehcache.xml自定义缓存区
<cache name="standard" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache>
对实体类进行序列化,在service层增加注解
阅读全文
0 0
- Spring整合EhCache缓存
- spring+hibernate整合ehcache
- EhCache与spring整合
- Ehcache整合spring配置
- Spring整合ehcache
- Ehcache整合Spring实例
- Ehcache整合spring配置
- Spring整合Ehcache缓存
- Spring整合Ehcache
- ehcache + spring+mybatis整合
- spring整合ehcache缓存
- Ehcache 整合Spring 使用
- spring+Mybatis+Ehcache整合
- Ehcache整合spring配置
- Ehcache整合spring配置
- Ehcache整合spring配置
- Ehcache整合spring配置
- spring boot ehcache整合
- ns2仿真学习(二)-tcp拥塞窗口的跟踪
- 接口限流算法总结
- NOIP模拟 最佳序列【二分答案+线段树(单调队列)】
- PID算法Demo
- openGL之glsl入门2--helloworld
- Spring整合ehcache
- nodeJS入门教程
- 学习阿里巴巴开发手册-12
- 2013NOIP普级组第一题--计数问题(参考洛谷题解)
- Hdu--Rescue之bfs加优先队列
- 【实战】从网络地址上下载图片
- Android --- MediaPlayer的使用详解
- 安装ApachePHP步骤详解
- Codeforces 827C DNA Evolution(多维树状数组)