漁人碼頭

          天行健,君子以自強不息。地勢坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          EHCache 初步使用指南

          Posted on 2007-01-11 17:46 Fisher 閱讀(526) 評論(0)  編輯  收藏 所屬分類: Java應(yīng)用
          EHCache 初步使用指南 ??

          Cc.Net BLoG 寫的比較初級,后續(xù)將加入對配置文件,內(nèi)存/磁盤存儲,優(yōu)化,虛擬機宕機,日志等的說明
          1. EHCache 的特點,系統(tǒng)要求及安裝
          是一個純Java ,過程中(也可以理解成插入式)緩存實現(xiàn)
          Hibernate2.1,Spring支持EHcache嵌入,自我感覺Hibernate2.1 + EHCache 很過癮。。(測試結(jié)果比Hibernate+JCS 好多了)
          支持多CPU服務(wù)器
          其版本發(fā)布前進行了產(chǎn)品級測試
          支持:運行環(huán)境jdk1.2到5版本(源代碼編譯需1.4或1.5 版本)
          jdk1.4和1.5 版本,需加入apache 的 commons-logging類庫 http://jakarta.apache.org/commons/logging.html
          jdk1.2和1.3 版本,需加入commons-collections 2.1 版本 http://jakarta.apache.org/commons/collections.html
          和 xerces (xml-apis.jar and xercesImpl.jar), 2.5 版本 http://xml.apache.org/xerces2-j/

          如果運行環(huán)境為IBM JDK1.4版本上的IBM Websphere 5.1,也需加入 commons-collections 類庫(如需要)

          單獨安裝Ehcache ,需把ehcache-X.X.jar 和相關(guān)類庫方到classpath中。
          如項目已安裝了Hibernate2.1 ,則不需要做什么。。直接可以使用Ehcache

          Cache 存儲方式 :內(nèi)存或磁盤

          2. 單獨使用 EHCache

          使用CacheManager 創(chuàng)建并管理Cache
          使用默認配置文件創(chuàng)建
          CacheManager manager = CacheManager.create();
          使用指定配置文件創(chuàng)建
          CacheManager manager = CacheManager.create("src/config/ehcache.xml");
          從classpathq找尋配置文件并創(chuàng)建
          URL url = getClass().getResource("/anothername.xml");
          CacheManager manager = CacheManager.create(url);
          通過輸入流創(chuàng)建
          InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
          try {
          manager = CacheManager.create(fis);
          } finally {
          fis.close();
          }

          卸載CacheManager ,關(guān)閉Cache

          manager.shutdown();

          使用Caches

          取得配置文件中預先 定義的sampleCache1設(shè)置,生成一個Cache
          Cache cache = manager.getCache("sampleCache1");


          設(shè)置一個名為test 的新cache,test屬性為默認
          CacheManager manager = CacheManager.create();
          manager.addCache("test");

          設(shè)置一個名為test 的新cache,并定義其屬性
          CacheManager manager = CacheManager.create();
          Cache cache = new Cache("test", 1, true, false, 5, 2);
          manager.addCache(cache);


          往cache中加入元素
          Element element = new Element("key1", "value1");
          cache.put(new Element(element);


          從cache中取得元素
          Element element = cache.get("key1");



          3. 在 Hibernate 中運用EHCache

          hibernate.cfg.xml中需設(shè)置如下:
          2.1版本加入
          <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
          2.1以下版本加入
          <property name="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCache</property>

          在 Hibernate 映射文件的每個需要Cache的Domain中
          <hibernate-mapping>
          <class
          name="com.somecompany.someproject.domain.Country"
          table="ut_Countries"
          dynamic-update="false"
          dynamic-insert="false"
          >
          ...

          </hibernate-mapping>
          加入類似如下格式信息:
          <cache usage="read-write|nonstrict-read-write|read-only" />
          比如:
          <cache usage="read-write" />

          然后在ehcache.xml中加入
          <ehcache>
          <cache name="com.somecompany.someproject.domain.Country"
          maxElementsInMemory="10000"
          eternal="false"
          timeToIdleSeconds="300"
          timeToLiveSeconds="600"
          overflowToDisk="false"
          />
          </ehcache>
          主站蜘蛛池模板: 锦州市| 固始县| 新建县| 广元市| 监利县| 张家口市| 靖州| 巫山县| 冕宁县| 铁岭市| 尤溪县| 安塞县| 洞头县| 额尔古纳市| 延庆县| 沙湾县| 邢台县| 托克托县| 安泽县| 额尔古纳市| 无为县| 广灵县| 教育| 武鸣县| 望奎县| 普兰店市| 清原| 神农架林区| 寿光市| 博白县| 大田县| 稻城县| 屏边| 舟曲县| 文登市| 贡觉县| 舒城县| 宁津县| 高邮市| 大竹县| 英山县|