日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Ehcache中核心类和方法

發(fā)布時(shí)間:2025/3/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ehcache中核心类和方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Ehcache核心類和方法

?

EhCache里面有一個(gè)CacheManager類型,它負(fù)責(zé)管理cacheCache里面存儲(chǔ)著Element對(duì)象,Element必須是key-value對(duì)。Cache是實(shí)際物理實(shí)現(xiàn)的,在內(nèi)存中或者磁盤。這些組件的邏輯表示就是下面即將要討論的類。他們的方法提供了可編程的訪問方式。

?

CacheManager

負(fù)責(zé)Cache的創(chuàng)建、訪問、移除。

?

CacheManager創(chuàng)建

CacheManager支持兩種創(chuàng)建模式:單例(Singleton mode)和實(shí)例(InstanceMode)。

2.5之前的版本中,在同一個(gè)JVM中允許存在任意數(shù)量相同名字的CacheManager。每調(diào)用new CacheManager(...)一次,就會(huì)產(chǎn)生一個(gè)新的CacheManager實(shí)例,而不管已經(jīng)存在多少個(gè)。調(diào)用CacheManager.create(...),則返回的是已經(jīng)存在的那個(gè)配置對(duì)應(yīng)的單例CacheManager,如果不存在,則創(chuàng)建一個(gè)。


2.5之后的版本,不允許在同一個(gè)JVM內(nèi)存在多個(gè)具有相同名字的CacheManager。創(chuàng)建非單例實(shí)例的CacheManager()構(gòu)造函數(shù)可能會(huì)打破這一規(guī)則,但是會(huì)拋出NPE異常。如果你的代碼要在同一個(gè)JVM創(chuàng)建多個(gè)同名的實(shí)例,請(qǐng)使用靜態(tài)方法CacheManager.create(),總是返回對(duì)應(yīng)名的CacheManager(如果已經(jīng)存在),否則創(chuàng)建一個(gè)。

?

事實(shí)上,我們可以直接利用Spring中的EhCacheManagerFactoryBean[spring2.5.4]來(lái)幫我們完成CacheManager的創(chuàng)建,看看它的具體創(chuàng)建方式:

[java]?view plaincopyprint?
  • if?(this.shared)?{??
  • ????????????//?Shared?CacheManager?singleton?at?the?VM?level.??
  • ????????????if?(this.configLocation?!=?null)?{??
  • ????????????????this.cacheManager?=?CacheManager.create(this.configLocation.getInputStream());??
  • ????????????}??
  • ????????????else?{??
  • ????????????????this.cacheManager?=?CacheManager.create();??
  • ????????????}??
  • ????????}??
  • ????????else?{??
  • ????????????//?Independent?CacheManager?instance?(the?default).??
  • ????????????if?(this.configLocation?!=?null)?{??
  • ????????????????this.cacheManager?=?new?CacheManager(this.configLocation.getInputStream());??
  • ????????????}??
  • ????????????else?{??
  • ????????????????this.cacheManager?=?new?CacheManager();??
  • ????????????}??
  • ????????}??
  • ????????if?(this.cacheManagerName?!=?null)?{??
  • ????????????this.cacheManager.setName(this.cacheManagerName);??
  • ????????}??



  • EhCache2.5.2及其以上版本的創(chuàng)建方法歸納如下:

    • CacheManager.newInstance(Configuration configuration) :創(chuàng)建一個(gè)新的CacheManager 對(duì)象或者返回已經(jīng)存在的對(duì)應(yīng)配置中名字的CacheManager
    • CacheManager.create():創(chuàng)建一個(gè)新的默認(rèn)配置的單例CacheManager ,或者返回一個(gè)已經(jīng)存在的單例。
    • CacheManager.create(Configuration configuration),創(chuàng)建一個(gè)對(duì)應(yīng)傳入配置文件中名字的單例CacheManager,或者返回已經(jīng)存在的單例CacheManager。
    • new CacheManager(Configuration configuration),創(chuàng)建一個(gè)新的CacheManager,或者如果對(duì)應(yīng)配置的CacheManager已經(jīng)存在或配置參數(shù)為空,拋出異常。

    ?

    Instance Mode中,如果Cache均使用MemoryStore,則沒有什么特別需要注意的。但是如果使用了DIskStore,那么每個(gè)CacheManager必須具有不同的diskStore路徑。當(dāng)一個(gè)新的CacheManager被創(chuàng)建的時(shí)候,需要檢查沒有別的CacheManager使用同樣的DiskStore路徑。如果有,就會(huì)拋出異常CacheException。如果CacheManager是集群中一部分,那么其監(jiān)聽端口必須唯一。

    ?

    SingletonmodeInstance Mode可以混合使用,而不會(huì)產(chǎn)生沖突。

    ?

    Ehcache

    所有的cache都實(shí)現(xiàn)了接口Ehcache。每個(gè)cache都有名字和屬性,且包含Element

    Ehcache中的cache相當(dāng)于其他緩存系統(tǒng)中的一塊緩存區(qū)域。

    ?

    Element

    Element是存放于cache中的原子單位。它有一個(gè)key、一個(gè)value以及關(guān)于訪問的記錄。Element被放進(jìn)到cache或者從cache移除。他們也可能會(huì)由于過(guò)期被移除,這依賴于配置。


    使用實(shí)例

    下面給出了一個(gè)使用Ehcache的實(shí)際例子。

    首先新建一個(gè)maven java工程,在pom.xml中添加Ehcache依賴。

    [html]?view plaincopyprint?
  • <!--?Ehcache?-->??
  • <dependency>??
  • ????<groupId>net.sf.ehcache</groupId>??
  • ????<artifactId>ehcache</artifactId>??
  • ????<version>2.8.3</version>??
  • </dependency>??
  • 下面是java代碼。代碼實(shí)現(xiàn)的功能非常簡(jiǎn)單,即創(chuàng)建CacheManager,往里面存放一個(gè)Cache,然后往cache里面存數(shù)據(jù)和取數(shù)據(jù),目的是展示Ehcache的基本使用。

    [java]?view plaincopyprint?
  • /**?
  • ?*?XXX.com?Inc.?
  • ?*?Copyright?(c)?2004-2014?All?Rights?Reserved.?
  • ?*/??
  • package?com.test.encache;??
  • ??
  • import?net.sf.ehcache.Cache;??
  • import?net.sf.ehcache.CacheManager;??
  • import?net.sf.ehcache.Element;??
  • ??
  • /**?
  • ?*??
  • ?*?@author?XXX?
  • ?*?@version?$Id:?EncacheTest.java,?v?0.1?2014年8月8日?下午5:30:03?XXX?Exp?$?
  • ?*/??
  • public?class?EncacheTest?{??
  • ????//一些配置參數(shù)??
  • ????//private?final?static?String?configFileName??????=?"ehcache.xml";??
  • ????//private?final?static?int????maxEntriesLocalHeap?=?1000;??
  • ????private?static?CacheManager?cacheManager;??
  • ????static?String???????????????cacheName?=?"cache1";??
  • ??
  • ????public?static?void?main(String[]?args)?{??
  • ????????ehcacheSetUp();??
  • ??
  • ????????ehcacheUse();??
  • ????}??
  • ??
  • ????private?static?void?ehcacheSetUp()?{??
  • ??
  • ????????cacheManager?=?CacheManager.create();??
  • ??
  • ????????//CacheConfiguration?configuration?=?new?CacheConfiguration(configFileName,??
  • ????????//????maxEntriesLocalHeap);??
  • ??
  • ????????//Cache?cache?=?new?Cache(configuration);??
  • ????????cacheManager.addCache(cacheName);??
  • ??
  • ????}??
  • ??
  • ????private?static?void?ehcacheUse()?{??
  • ????????Cache?cache1?=?cacheManager.getCache(cacheName);??
  • ????????String?key?=?"key1";??
  • ????????String?value?=?"value1";??
  • ??
  • ????????writeSomeData(cache1,?key,?value);??
  • ??
  • ????????Element?element?=?readSomeData(cache1,?key,?value);??
  • ??
  • ????????System.out.println(element);??
  • ????}??
  • ??
  • ????private?static?void?writeSomeData(Cache?cache,?String?key,?String?value)?{??
  • ????????cache.put(new?Element(key,?value));??
  • ????}??
  • ??
  • ????private?static?Element?readSomeData(Cache?cache,?String?key,?String?value)?{??
  • ????????return?cache.get(key);??
  • ????}??
  • }??

  • 程序輸出:
    [html]?view plaincopyprint?
  • SLF4J:?Failed?to?load?class?"org.slf4j.impl.StaticLoggerBinder".??
  • SLF4J:?Defaulting?to?no-operation?(NOP)?logger?implementation??
  • SLF4J:?See?http://www.slf4j.org/codes.html#StaticLoggerBinder?for?further?details.??
  • [?key?=?key1,?value=value1,?version=1,?hitCount=1,?CreationTime?=?1411807398768,?LastAccessTime?=?1411807398771?]??

  • 其中的錯(cuò)誤信息是因?yàn)闆]有配置日志相關(guān)的SLF4J所致。


    下面我們要配置日志。首先在pom.xml中添加依賴:

    [html]?view plaincopyprint?
  • <!--?SLF4J?-->??
  • <dependency>??
  • ????<groupId>org.slf4j</groupId>??
  • ????<artifactId>slf4j-log4j12</artifactId>??
  • ????<version>1.6.1</version>??
  • ;/dependency>??
  • 然后建立log4j的配置文件log4j.properties:

    [plain]?view plaincopyprint?
  • #?Root?logger?option??
  • log4j.rootLogger=INFO,?file,?stdout??
  • log4j.logger.com.test.encache.EncacheTest=INFO,file??
  • ???
  • #?Direct?log?messages?to?a?log?file??
  • log4j.appender.file=org.apache.log4j.DailyRollingFileAppender??
  • log4j.appender.file.File=C:\\logging.log??
  • log4j.appender.file.layout=org.apache.log4j.PatternLayout??
  • log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd?HH:mm:ss}?%-5p?%c{1}:%L?-?%m%n??
  • ???
  • #?Direct?log?messages?to?stdout??
  • log4j.appender.stdout=org.apache.log4j.ConsoleAppender??
  • log4j.appender.stdout.Target=System.out??
  • log4j.appender.stdout.layout=org.apache.log4j.PatternLayout??
  • log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd?HH:mm:ss}?%-5p?%c{1}:%L?-?%m%n??

  • 并且將這個(gè)文件放置到工程的classpath下,在這里我建立的是用Eclipse創(chuàng)建的maven工程,將其放置在工程主目錄下的\target\classes文件夾下。
    然后在代碼中添加logger的初始化代碼:
    [java]?view plaincopyprint?
  • private?static?final?Logger?logger????=?LoggerFactory.getLogger(EncacheTest.class);??

  • 然后就可以使用了:

    [java]?view plaincopyprint?
  • logger.info("Setup?ehcache");??

  • 輸出:

    [html]?view plaincopyprint?
  • 2014-09-27?17:22:45?INFO??EncacheTest:35?-?Setup?ehcache??
  • 2014-09-27?17:22:45?WARN??ConfigurationFactory:136?-?No?configuration?found.?Configuring?ehcache?from?ehcache-failsafe.xml??found?in?the?classpath:?jar:file:/D:/MavenRepo/net/sf/ehcache/ehcache/2.8.3/ehcache-2.8.3.jar!/ehcache-failsafe.xml??
  • 2014-09-27?17:22:46?WARN??DiskStorePathManager:162?-?diskStorePath?'C:\Users\xxxx\AppData\Local\Temp'?is?already?used?by?an?existing?CacheManager?either?in?the?same?VM?or?in?a?different?process.??
  • The?diskStore?path?for?this?CacheManager?will?be?set?to?C:\Users\xxxx\AppData\Local\Temp\ehcache_auto_created7989392067865891865diskstore.??
  • To?avoid?this?warning?consider?using?the?CacheManager?factory?methods?to?create?a?singleton?CacheManager?or?specifying?a?separate?ehcache?configuration?(ehcache.xml)?for?each?CacheManager?instance.??
  • [?key?=?key1,?value=value1,?version=1,?hitCount=1,?CreationTime?=?1411809766273,?LastAccessTime?=?1411809766276?] ?

  • 總結(jié)

    以上是生活随笔為你收集整理的Ehcache中核心类和方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。