码迷,mamicode.com
首页 > 系统相关 > 详细

二级缓存EhCache在几种应用技术的配置方法和步骤总结

时间:2015-03-18 01:01:56      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

一:Spring和Ehcache缓存集成

业务问题:如果仓库不经常变动,大量进出库,总是需要查询仓库列表 (列表重复) ,使用缓存优化 !

       阅读spring规范29章节

第一步: 导入ehcache的jar 和 ehcache.xml

       ehcache-core-2.6.6.jar

       需要导入spring-contextsupport 的jar

 

第二步: 配置自定义缓存区 <cache name=”” >

 

       <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>

 

    <defaultCache

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            maxElementsOnDisk="10000000"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU">

        <persistence strategy="localTempSwap"/>

    </defaultCache>

   

    <cache name="zidingyi"

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            maxElementsOnDisk="10000000"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU">

        <persistence strategy="localTempSwap"/>

    </cache>

</ehcache>

 

第三步: 配置Sprin的applicationContext.xml

引用cache 名称空间

<?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:aop="http://www.springframework.org/schema/aop"

  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

   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/aop

   http://www.springframework.org/schema/aop/spring-aop.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx.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>

<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">

      <property name="cacheManager" ref="ehCacheManager" />

</bean>

   <cache:annotation-driven cache-manager="springCacheManager"/>

</beans>

 

 

第四步:程序中使用 @Cacheable 和 @CacheEvict 管理缓存

@Override

@CacheEvict(allEntries = true, value = "storemanager")

// 清空缓存区

public void saveStore(Store store) {

   storeDAO.save(store);

}

 

@Override

@Cacheable("storemanager")

// 这里的storemanager 自定义缓存区的name

public List<Store> findAllStores() {

      return storeDAO.findAll(Store.class);

}

这两个动作都是在service层进行。

 

二:hibernate中Ehcache的的基本配置

第一步:导入ehcache的jar包

ehcache依赖 backport-util-concurrent 和 commons-logging

第二步:配置ehcache默认的核心配置文件ehcache.xml

ehcache.xml(名字固定)(放在类路径下)

解压 ehcache的jar ,将根目录 ehcache-failsafe.xml 改名为 ehcache.xml 复制 src

配置后如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <!-- <diskStore path="java.io.tmpdir"/> -->

    <!-- 缓存的路径改为z盘 -->

    <diskStore path="z:"/>

    <defaultCache

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU"

            />

       <!-- 自定义缓存配置 -->

       <!-- book在开启二级缓存的时候,就不走默认的缓存配置了,走自己的配置策略 -->

       <cache name="cn.itcast.a_isolation.Book"

            maxElementsInMemory="1"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU"

            />

</ehcache>

第三步:配置Hibernate启用二级缓存

在hibernate.cfg.xml中配置

<!-- 开启“基本”二级缓存 策略-->

<property name="hibernate.cache.use_second_level_cache">true</property>

第四步:配置二级缓存提供商

 <!-- 指定二级缓存产品的提供商 --> 

<property  name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

第五步:配置要缓存的数据(类)和并发策略

你要缓存哪些数据-对象,这里可以有两种方法配置:

方法一: 在hibernate.cfg.xml 进行配置

<!-- 配置缓存的类- -->

<class-cache usage="read-write" class="cn.awjw.domain.qp.Customer"/>

方法二: 在XXX.hbm.xml 进行配置

<!—对类的数据进行二级缓存—>

<cache usage=”read-write”/>

 

三:Ehcache在shiro中的使用(maven方式)

问题:当自定义Realm实现授权方法后, 每次调用需要权限控制页面,都要调用Realm的授权方法 (Realm授权方法, 根据用户查询 角色和权限信息, 每次相同用户查询数据都是一样的 ) ----------------- 浪费性能

       解决:使用缓存优化,当第一次授权,将授权数据 AuthorizationInfo 放入缓存 ,第二次以后查询,直接从缓存中取出 AuthorizationInfo 数据,不需要查询数据库

 

第一步: 通过坐标 导入ehcache 的jar包

              <ehcache.version>2.6.10</ehcache.version>

              <dependency>

         <groupId>net.sf.ehcache</groupId>

         <artifactId>ehcache-core</artifactId>

         <version>${ehcache.version}</version>

      </dependency>

第二步: 在src/main/resources 提供 ehcache.xml 配置文件

先从ehcachee的jar包copy出默认的配置,然后修改后如下:

 

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

 

    <diskStore path="java.io.tmpdir"/>

    <defaultCache

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU"

            />

              <!--自己配置的缓存区 -->

             <cache name="bosCache"

            maxElementsInMemory="10000"

            eternal="false"

            timeToIdleSeconds="120"

            timeToLiveSeconds="120"

            overflowToDisk="true"

            maxElementsOnDisk="10000000"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="120"

            memoryStoreEvictionPolicy="LRU">

            </cache>

</ehcache>

 

第三步: 配置applicationContext.xml

首先要添加spring对Ehcache的支持,引入坐标

<spring-support.version>3.2.0.RELEASE</spring-support.version>

<dependency>

         <groupId>org.springframework</groupId>

         <artifactId>spring-context-support</artifactId>

         <version>${spring-support.version}</version>

      </dependency>

 

配置ehcache缓存管理器:

<!-- ehcache 缓存管理器  -->

<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

      <property name="configLocation" value="classpath:ehcache.xml"></property>

</bean>

第四步: shiro整合ehcache

<!-- shiro 缓存管理器 -->

<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

      <property name="cacheManager" ref="ehcacheManager"></property>

</bean>

 

第五步: 将shiro缓存管理器 注入安全管理器

<!-- 安全管理器 -->

<bean id="securityManager"

      class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

   <!-- 在安全管理器,应该注入 Realm 连接安全数据  -->

   <property name="realm" ref="myRealm"></property>

   <property name="cacheManager" ref="shiroCacheManager"></property>

</bean>

 

第六步: 在realm指定 使用缓存区名称

<bean id="myRealm" class="cn.awjw.bos.realm. myRealm" >

      <!-- 在realm指定使用缓存名称-->

   <property name="authorizationCacheName" value="myCache"/>

</bean>

重启项目,即可在shiro中使用缓存技术了。

 

二级缓存EhCache在几种应用技术的配置方法和步骤总结

标签:

原文地址:http://www.cnblogs.com/sloveling/p/ehcachee.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!