`

Ehcache 初次体验

阅读更多

听说ehcache可以做内存缓存,也可以做磁盘缓存。于是就拿来研究研究。

commons-logging-1.1.jar(www.apache.org 可以下载)

ehcache-1.2.3.jar(http://ehcache.sourceforge.net 可以下载)

ehcache.xml

<ehcache xmlns:xsi="&amp;lt;a href=" www.w3.org=""></ehcache>[ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"]
    [diskStore path="java.io.tmpdir"/]
    [cacheManagerEventListenerFactory class="" properties=""/]
     [defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            /]
    [cache name="personalHomeCache"
           maxElementsInMemory="5000"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LFU"
           /]
[/ehcache]

 

TestEhache.java

 

package photoblog.cache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestEhcache {

 /**
  * @param args
  */
 public static void main(String[] args) {
  String fileName="E:\\project\\photoblog\\src\\photoblog\\cache\\ehcache.xml";
  CacheManager manager = new CacheManager(fileName);
  String names[]=manager.getCacheNames();
  Cache cache=manager.getCache(names[0]);
  cache.put(new Element("key1","values1"));
  Element element = cache.get("key1");
  Object obj=element.getObjectValue();
  System.out.println(obj.toString());
  manager.shutdown();
  

 }

}

评论

相关推荐

Global site tag (gtag.js) - Google Analytics