1. 程式人生 > >EhCache 介紹和在spring中配置

EhCache 介紹和在spring中配置

複製程式碼
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">
    
    <diskStore path="java.io.tmpdir"/>     
   <
defaultCache maxEntriesLocalHeap="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds
="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <cache name="myCache" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="30" timeToIdleSeconds
="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap"/> </cache>

</ehcache>

引入jar <!-- 引入ehcache快取 --> 

 <dependency> 

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

 <artifactId>ehcache</artifactId> 

 <version>2.8.3</version> 

 </dependency> 

第一步:首先配置ehcache.xml複製程式碼

用上面的配置可能執行會報錯,最好用初始配置,具體情況再看情況加,下面是初始配置

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="andCache"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> 

    <!-- Place configuration for your caches following -->

</ehcache>

複製程式碼第二步:在spring.xml的配置檔案中引入schema,  
xmlns:aop="http://www.springframework.org/schema/aop"和http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.2.xsd      
快取的配置:複製程式碼    <!-- 啟用快取註解功能,這個是必須的,否則註解不會生效,另外,該註解一定要宣告在spring主配置檔案中才會生效 -->    
<cache:annotation-driven cache-manager="ehcacheManager"/>        
<!-- cacheManager工廠類,指定ehcache.xml的位置 -->    
<bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">         
<property name="configLocation" value="classpath:ehcache.xml" />    
</bean>    
<!-- 宣告cacheManager -->    
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">         
<property name="cacheManager" ref="ehcacheManagerFactory" />    
</bean>
複製程式碼OK!快取的相關配置已經完成。下面開始編寫測試程式。
這裡需要連線資料庫,我就不寫了。
這裡為了方便就隨便找了之前寫過的model,這個model就是AOP註解實現日誌管理的實體,為了偷懶就直接用了,希望你們不要誤解,沒有特殊意義的第三步:
編寫model,這裡需要注意,要實現快取的實體必須要序列化 private static final long serialVersionUID = -6579533328390250520L;  關於序列化的生成這裡就不介紹了,大家可以百度看看。 
View Code第四步:編寫dao,service 
第五步:編寫serviceImpl並新增快取註解。這裡快取註解的引數不介紹了,不懂得看我上一篇部落格,我這裡先把需要的註解都寫上了,一會一個一個介紹。
@Service("systemLogService")
public class SystemLogServiceImpl implements SystemLogService {    
@Resource    
private SystemLogMapper systemLogMapper;        
@Override    
public int deleteSystemLog(String id) {                      
return systemLogMapper.deleteByPrimaryKey(id);    
}    
@Override    
//@CachePut(value="myCache")    
//@CacheEvict(value="myCache",allEntries=true,beforeInvocation=true)    
@CacheEvict(value="myCache",key="0",beforeInvocation=true)    
public int insert(SystemLog record) {                      
return systemLogMapper.insertSelective(record);    
}    
@Override    
@Cacheable(value="myCache",key="#id")    
public SystemLog findSystemLog(String id) {                     
return systemLogMapper.selectByPrimaryKey(id);    
}    
@Override    
public int updateSystemLog(SystemLog record) {                     
return systemLogMapper.updateByPrimaryKeySelective(record);    
}    
@Override    
public int insertTest(SystemLog record) {                   
return systemLogMapper.insert(record);    
}    
@Override    
@Cacheable(value="myCache",key="0")    
public int count() {           
int num = systemLogMapper.count();           
return num;    
}}
複製程式碼第六步:編寫controller,即我們的測試。
@Controller
@RequestMapping("systemLogController")
public class SystemLogController {    
@Resource    
private SystemLogService systemLogService;        
@RequestMapping("testLog")    
public ModelAndView testLog(){            
ModelMap modelMap = new ModelMap();        
SystemLog systemLog = systemLogService.findSystemLog("c30e2398-079a-406b-a2f7-a85fa15ccac7");        
modelMap.addAttribute("data", systemLog);        
return new ModelAndView("index",modelMap);    
}    
@RequestMapping("insert")    
@ResponseBody    
public boolean Insert(SystemLog record){        
systemLogService.insert(record);        
return true;    
}        
@RequestMapping("test1")    
public ModelAndView test1(){        
ModelMap modelMap = new ModelMap();        
int num =systemLogService.count();        
modelMap.addAttribute("num", num);        
return  new ModelAndView("pageEhcache",modelMap);    
    }    
}

相關推薦

Hibernate 事務處理spring配置事務

原文連結:http://blog.csdn.net/sd0902/article/details/8393700 1.非整合spring事務管理 事務是指由一個或者多個SQL語句組成的工作單元,這個單元中SQL語句只要有一個SQL語句執行失敗,就會撤銷整個工作單元

EhCache 介紹spring配置

<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehca

定時器的實現、java定時器TimerQuartz介紹Spring定時器的配置

欄位 允許值 允許的特殊字元    秒 0-59 , - * /    分 0-59 , - * /    小時 0-23 , - * /    日期 1-31 , - * ? / L W C    月份 1-12 或者 JAN-DEC , - * /    星期 1-7 或者 SUN-SAT , - *

Spring配置讀取多個Properties檔案--轉

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">       &

定時器的實現、java定時器介紹Spring定時器的配置

1定時器的作用 在實際的開發中,如果專案中需要定時執行或者需要重複執行一定的工作,定時器顯現的尤為重要。 當然如果我們不瞭解定時器就會用執行緒去實現,例如: package org.lzstone.action public class FinanceAction exte

JavaJOB介紹SpringJOB的配置

1定時器的作用 在實際的開發中,如果專案中需要定時執行或者需要重複執行一定的工作,定時器顯現的尤為重要。 當然如果我們不瞭解定時器就會用執行緒去實現,例如: package org.lzstone.action public class FinanceAction extends Thread{       

spring配置mongodb的使用者名稱密碼

1、在spring的配置檔案中 <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="ht

Spring配置SLF4JLog4J

背景 SLF4J越來越流行,介面也比JCL好用,如何在基於Spring的應用中使用SLF4J而不是JCL。 排除spring對JCL的依賴 <dependency> <groupId>org.springframework</groupId

jedisspring配置檔案

jedis配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.o

spring配置監聽隊列的MQ

msl tin listen ann lazy ati app ssa clas 一、spring中配置監聽隊列的MQ相關信息註:${}是讀取propertites文件的常量,這裏忽略。綠色部分配置在接收和發送端都要配置。 <bean id="axx" class=

node.js環境在WindowMac配置,已經安裝cnpm配置Less環境

use usr 版本 htm args gin targe mpi ffffff Node.js 和cnpm安裝 最近準備學習vue.js,但首先需要配置電腦的環境。配置node.js。 1.在node(https://nodejs.org/en/)官網上下載安裝node.

servlet的介紹 & xml配置 以及 & 三種實現方式(補充設定瀏覽器不快取的方法)

開始時間:2018年10月13日20:53:30 | 2018年10月14日16:10:56 結束時間:2018年10月13日21:53:30 | 2018年10月14日17:02:23 累計時間:2小時 備註:幾乎每一句話都很有收穫,複習的時候務必要仔細一點 Servlet

Spring配置檔案application xml配置的含義

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

SpringMVCSpring配置檔案掃描包詳解

其實Spring和SpringMVC是有父子容器關係的,而且正是因為這個才往往會出現包掃描的問題,我們在此來分析和理解Spring和SpringMVC的父子容器關係並且給出Spring和SpringMVC配置檔案中包掃描的官方推薦方式。   在Spring整體框架的核

Spring的AOP簡介Spring的通知使用方法以及異常

AOP中關鍵性概念 連線點(Joinpoint):程式執行過程中明確的點,如方法的呼叫,或者異常的丟擲. 目標(Target):被通知(被代理)的物件 注1:完成具體的業務邏輯 通知(Advice):在某個特定的連線點上執行的動作,同時Advice也是程式程式碼的具體實現,例如一個

HibernateSpring整合配置

工具:myeclipse2014 Spring版本:3.1 Hibernate版本:3.3 文章介紹了hibernate和spring的配置,並將兩個框架整合起來,自己寫了測試程式碼測試是否整合成功。 配置hibernate和spring: 先配置hibe

Spring配置Quartz的過程;Spring與Quartz的相容問題

1. 根據spring和Quratz的版本不同,觸發器的方法可能不同,有org.springframework.scheduling.quartz.SimpleTriggerFactoryBean和org.springframework.scheduling.quar

win7平臺cygwin安裝Clion配置cygwin編譯器小白教程

文章目錄 系統平臺 安裝cygwin 在Clion中使用cygwin編譯器 系統平臺 本教程系統平臺為win7 64位旗艦版。 安裝cygwin 從官網:https://cygwin.com/install.html 上下載

怎樣在iis6 iis7 配置自定義的IHttpHandler類

現在我們有一個解決方案,裡面有兩個專案,分別命名為common和test,在common這個專案中我們新建了一個名為MyHandler的類,該類繼承了IHttpHandler這個介面。在test這個專案中新增common這個專案的引用。為了應用MyHandler這個自定義的

spring配置profile

1、新建一個maven專案,匯入如下依賴 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <sprin