缓存框架EhCache的简单使用
2024-10-15 09:32:08
缓存框架EhCache的简单使用:
1.Spring和EhCache框架整合
1.1导入jar包
<dependencies>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
1.2引入ehcache.xml的配置文件
<?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:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd ">
<!-- 缓存配置 -->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- shiro封装cacheManager -->
<bean id="shiroCacheManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- spring 封装ehcache缓存管理器 -->
<bean id="springCacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
<!-- 激活spring 缓存注解 -->
<cache:annotation-driven cache-manager="springCacheManager"/>
</beans>
2.配置shiro整合ehcache完成对授权数据缓存
2.1配置applicationContext.xml
<!-- 安全管理器 -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosRealm" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
<!-- 配置Realm -->
<bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
<!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name -->
<property name="authorizationCacheName" value="bos" />
</bean>
2.2取消realm类上面的service注解
2.3给对应的实体类实现Serializable接口
3.Ehcache对普通业务数据进行缓存
在被 spring 管理 bean 对象方法上 使用@Cacheable 、@CacheEvict
@Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
@Cacheable("standard")
@CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法
@CacheEvict(value="standard",allEntries=true)
4.有参数的方法如何对结果数据进行缓存
针对数据在不同条件下进行不同缓存,设置@Cacheable 注解key属性
@Cacheable(value="standard",key="#pa.pageNumber+'_'+#pa.pageSize")
小结:针对采用ehcache技术的模块,再进行数据操作是时,只会走一次数据库,
接下来的每一次操作都是经过缓存从内存中获取的.
最新文章
- Android源码阅读 &ndash; Zygote
- 从jquery里的$.ajax()到angularjs的$http
- MySQL完整性语言
- 前端框架react研究
- zoj 3599 Game 博弈论
- VMWare ESX Server
- 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错
- The Suspects(并查集求节点数)
- 解决火狐中用JQUERY .removeAttr()无法去除元素属性的方法
- 一款轻量级前端框架Avalon.Js
- BZOJ 2194 [快速傅里叶变换 卷积]
- spring data jpa 组合条件查询封装
- Struts2中五个重要的常量
- kettle连接mysql数据库并进行数据分析
- Vlc支持IE 360 低版本的Google浏览器
- PHP-CLI环境变量的设置和读取
- win10 添加项目右键用vscode打开
- 【C】——APUE小程序之递归遍历目录
- 【BZOJ】2456 mode(乱搞)
- Sandglass
热门文章
- [ActionScript 3.0] 利用ColorTransform实现对象(图片)的曝光过渡效果
- NSUserdefault读书笔记
- sqlalchemy中使用event设置条件触发短信与邮件通知
- Windows下Anaconda安装 python + tensorflow GPU版
- sqlalchemy 常用总结
- mybatis中调用游标,存储过程,函数
- wordpress 后台页面无法显示绑定的台湾语言
- vue 2.0创建新项目
- 关于ASP.NET MVC+Repository+Service架构的一些思考
- GridView控件详解