程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> ehcache配置:使用Spring+SpringMVC+Mybatis或者有shiro,ehcachespringmvc

ehcache配置:使用Spring+SpringMVC+Mybatis或者有shiro,ehcachespringmvc

編輯:JAVA綜合教程

ehcache配置:使用Spring+SpringMVC+Mybatis或者有shiro,ehcachespringmvc


SSM框架的搭建就不在敘述了

本文主要是講解在SSM基礎上再加上ehcache

1:首先:pom.xml需要的jar

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-ehcache</artifactId>
  <version>1.0.0</version>
 </dependency>

 <dependency>
  <groupId>org.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>3.0.1</version>
 </dependency>

 

2:在src/main/resources中添加ehcache.xml的配置文件,該路徑跟隨你的框架而定(大家都懂的)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  updateCheck="false">
       <diskStore path="java.io.tmpdir" />
       <defaultCache eternal="false" maxElementsInMemory="1000"
       overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
       timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
       <cache name="testCache" eternal="false" maxElementsInMemory="100"
       overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
       timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />
</ehcache>

<!-- 這裡是說明
name:Cache的唯一標識
maxElementsInMemory:內存中最大緩存對象數
maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大
eternal:Element是否永久有效,一但設置了,timeout將不起作用
overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中
timeToIdleSeconds:設置Element在失效前的允許閒置時間。僅當element不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大
timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介於創建時間和失效時間之間。僅當element不是永久有效時使用,默認是0.,也就是element存活時間無窮大
diskPersistent:是否緩存虛擬機重啟期數據
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用) -->

 

3:在spring-mybatis.xml中假如encache的配置

<!-- 使用ehcache緩存 -->
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean

--------重點來了---------

假如你的框架裡面集成了shiro,這裡就需要變下了,具體配置如下

<!-- MyBatis使用ehcache緩存 start -->  
    <bean id="ehCacheManager"  
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml" />  
        <property name="shared" value="true"></property> <!-- 這裡是關鍵!!!沒有必錯  -->  
    </bean>  
    <!-- end MyBatis使用ehcache緩存 -->  
<!-- 緩存管理器 使用Ehcache實現 --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="ehCacheManager" /> </bean>

 

4:最後在你的mapper.xml中配置encache,加入以下配置

<cache type="org.mybatis.caches.ehcache.LoggingEhcache" >  
      <property name="timeToIdleSeconds" value="3600"/>
      <property name="timeToLiveSeconds" value="3600"/>
      <property name="maxEntriesLocalHeap" value="1000"/>  
      <property name="maxEntriesLocalDisk" value="10000000"/>  
      <property name="memoryStoreEvictionPolicy" value="LRU"/>  
  </cache>

 

這裡說明下,LoggingEhcache  這個會在打印log,如果不像要log的話可以使用EhcacheCache 

最後說明下,按照我這樣配置的話,這個mapper.xml裡面的操作是全局,默認為useCache="true"  都會有作用,

假如某個業務是不要緩存的,可以在當前業務下加上useCache="false"

<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" useCache="false">

 

---------本博文為博主根據互聯網資料加上自己實際應用原創,可以轉載但需說明出處--------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved