• 缓存框架EhCache的简单使用


     1 缓存框架EhCache的简单使用:
     2     1.Spring和EhCache框架整合
     3         1.1导入jar包
     4         <dependencies>
     5             <dependency>
     6                 <groupId>net.sf.ehcache</groupId>
     7                 <artifactId>ehcache-core</artifactId>
     8                 <version>2.6.11</version>
     9             </dependency>
    10             <dependency>
    11                 <groupId>org.springframework</groupId>
    12                 <artifactId>spring-context-support</artifactId>
    13                 <version>${spring.version}</version>
    14             </dependency>
    15         </dependencies>
    16         1.2引入ehcache.xml的配置文件
    17             <?xml version="1.0" encoding="UTF-8"?>
    18             <beans xmlns="http://www.springframework.org/schema/beans"
    19                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    20                 xmlns:cache="http://www.springframework.org/schema/cache"
    21                 xsi:schemaLocation="
    22                     http://www.springframework.org/schema/beans 
    23                     http://www.springframework.org/schema/beans/spring-beans.xsd
    24                     http://www.springframework.org/schema/cache 
    25                     http://www.springframework.org/schema/cache/spring-cache.xsd ">
    26                  <!-- 缓存配置  -->
    27                 <bean id="ehCacheManager" 
    28                     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    29                     <property name="configLocation" value="classpath:ehcache.xml" />
    30                 </bean>
    31                 <!-- shiro封装cacheManager -->
    32                 <bean id="shiroCacheManager" 
    33                     class="org.apache.shiro.cache.ehcache.EhCacheManager">
    34                     <property name="cacheManager" ref="ehCacheManager" />
    35                 </bean>
    36                 <!-- spring 封装ehcache缓存管理器  -->
    37                 <bean id="springCacheManager" 
    38                     class="org.springframework.cache.ehcache.EhCacheCacheManager">
    39                     <property name="cacheManager" ref="ehCacheManager" />
    40                 </bean>
    41                 <!-- 激活spring 缓存注解 -->
    42                 <cache:annotation-driven cache-manager="springCacheManager"/>
    43             </beans>
    44     2.配置shiro整合ehcache完成对授权数据缓存
    45         2.1配置applicationContext.xml
    46             <!-- 安全管理器  -->
    47             <bean id="securityManager" 
    48                 class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    49                 <property name="realm" ref="bosRealm" />
    50                 <property name="cacheManager" ref="shiroCacheManager" />
    51             </bean>
    52             <!-- 配置Realm -->
    53             <bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
    54                 <!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name -->
    55                 <property name="authorizationCacheName" value="bos" />
    56             </bean>
    57         2.2取消realm类上面的service注解
    58         2.3给对应的实体类实现Serializable接口
    59     3.Ehcache对普通业务数据进行缓存
    60         在被 spring 管理 bean 对象方法上 使用@Cacheable 、@CacheEvict
    61         @Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
    62             @Cacheable("standard")
    63         @CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法
    64             @CacheEvict(value="standard",allEntries=true)
    65     4.有参数的方法如何对结果数据进行缓存
    66         针对数据在不同条件下进行不同缓存,设置@Cacheable 注解key属性
    67             @Cacheable(value="standard",key="#pa.pageNumber+'_'+#pa.pageSize")
    68     小结:针对采用ehcache技术的模块,再进行数据操作是时,只会走一次数据库,
    69           接下来的每一次操作都是经过缓存从内存中获取的.
  • 相关阅读:
    [CSP校内集训]hotel
    DP小技巧——悬线法
    [SDOI2015]寻宝游戏/异象石(LCA)
    [HAOI2006]旅行
    [SDOI2013]泉(搜索+hash+容斥)
    [NOIP校内集训]home
    [AHOI2014/JSOI2014]骑士游戏(SPFA的本质)
    欧拉函数模板
    开学考试题8:神奇的集合(multiset) 动态开点线段树
    开学考试题5:2017黑龙江省选
  • 原文地址:https://www.cnblogs.com/yshang/p/8179332.html
Copyright © 2020-2023  润新知