缓存框架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技术的模块,再进行数据操作是时,只会走一次数据库,
接下来的每一次操作都是经过缓存从内存中获取的.

最新文章

  1. Android源码阅读 &ndash; Zygote
  2. 从jquery里的$.ajax()到angularjs的$http
  3. MySQL完整性语言
  4. 前端框架react研究
  5. zoj 3599 Game 博弈论
  6. VMWare ESX Server
  7. 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错
  8. The Suspects(并查集求节点数)
  9. 解决火狐中用JQUERY .removeAttr()无法去除元素属性的方法
  10. 一款轻量级前端框架Avalon.Js
  11. BZOJ 2194 [快速傅里叶变换 卷积]
  12. spring data jpa 组合条件查询封装
  13. Struts2中五个重要的常量
  14. kettle连接mysql数据库并进行数据分析
  15. Vlc支持IE 360 低版本的Google浏览器
  16. PHP-CLI环境变量的设置和读取
  17. win10 添加项目右键用vscode打开
  18. 【C】——APUE小程序之递归遍历目录
  19. 【BZOJ】2456 mode(乱搞)
  20. Sandglass

热门文章

  1. [ActionScript 3.0] 利用ColorTransform实现对象(图片)的曝光过渡效果
  2. NSUserdefault读书笔记
  3. sqlalchemy中使用event设置条件触发短信与邮件通知
  4. Windows下Anaconda安装 python + tensorflow GPU版
  5. sqlalchemy 常用总结
  6. mybatis中调用游标,存储过程,函数
  7. wordpress 后台页面无法显示绑定的台湾语言
  8. vue 2.0创建新项目
  9. 关于ASP.NET MVC+Repository+Service架构的一些思考
  10. GridView控件详解