纯Java的进程内缓存框架-EhCache

Java缓存框架 EhCache  EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
JAVA实例:
  1. package test.ehcache; 
  2.  
  3. import net.sf.ehcache.Cache; 
  4. import net.sf.ehcache.CacheManager; 
  5. import net.sf.ehcache.Element; 
  6.  
  7. public class TestCache { 
  8.      
  9.     public static void main(String[] args) { 
  10.          
  11.         /**
  12.          * Create a singleton CacheManager using defaults, then list caches.
  13.          */ 
  14.         CacheManager.create(); 
  15.         String[] cacheNames = CacheManager.getInstance().getCacheNames(); 
  16.         System.out.println(cacheNames.length); 
  17.          
  18.         /**
  19.          * Create one CacheManager with a configuration, and list the caches in each.
  20.          */ 
  21.         CacheManager manager2 = CacheManager.newInstance("ehcache1.xml"); 
  22.         String[] cacheNames2 = manager2.getCacheNames(); 
  23.         System.out.println(cacheNames2.length); 
  24.  
  25.         /**
  26.          * Create a Cache and add it to the CacheManager, then use it.
  27.          * Note that Caches are not usable until they have been added to a CacheManager.
  28.          */ 
  29.         CacheManager singletonManager = CacheManager.create(); 
  30.         Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2); 
  31.         singletonManager.addCache(memoryOnlyCache); 
  32.         Cache cache = singletonManager.getCache("testCache"); 
  33.          
  34.         Element element = new Element("key1","value1"); 
  35.         cache.put(element); 
  36.         Element value = cache.get("key1"); 
  37.          
  38.         System.out.println(value.getObjectValue()); 
  39.         //value1 
  40.         System.out.println(value.toString()); 
  41.         //[ key = key1, value=value1, version=1, hitCount=1, CreationTime = 1359130974484, LastAccessTime = 1359130974500 ] 
  42.  
  43.     } 
  44.  
package test.ehcache;

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

public class TestCache {
	
	public static void main(String[] args) {
		
		/**
		 * Create a singleton CacheManager using defaults, then list caches.
		 */
		CacheManager.create();
		String[] cacheNames = CacheManager.getInstance().getCacheNames();
		System.out.println(cacheNames.length);
		
		/**
		 * Create one CacheManager with a configuration, and list the caches in each.
		 */
		CacheManager manager2 = CacheManager.newInstance("ehcache1.xml");
		String[] cacheNames2 = manager2.getCacheNames();
		System.out.println(cacheNames2.length);

		/**
		 * Create a Cache and add it to the CacheManager, then use it. 
		 * Note that Caches are not usable until they have been added to a CacheManager.
		 */
		CacheManager singletonManager = CacheManager.create();
		Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
		singletonManager.addCache(memoryOnlyCache);
		Cache cache = singletonManager.getCache("testCache");
		
		Element element = new Element("key1","value1");
		cache.put(element);
		Element value = cache.get("key1");
		
		System.out.println(value.getObjectValue());
		//value1
		System.out.println(value.toString());
		//[ key = key1, value=value1, version=1, hitCount=1, CreationTime = 1359130974484, LastAccessTime = 1359130974500 ]

	}

}

配置文件ehcache1.xml:
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" 
  4.     monitoring="autodetect" dynamicConfig="true"> 
  5.     <diskStore path="user.dir" /> 
  6.  
  7.     <defaultCache  
  8.         name="default-cache" 
  9.         maxElementsInMemory="10000"  
  10.         maxElementsOnDisk="0" 
  11.         eternal="false"  
  12.         overflowToDisk="true"  
  13.         timeToIdleSeconds="1200" 
  14.         timeToLiveSeconds="1200"> 
  15.     </defaultCache> 
  16.     <cache  
  17.         name="my-cache"   
  18.         maxElementsInMemory="10000" 
  19.         maxElementsOnDisk="0"  
  20.         eternal="false"   
  21.         overflowToDisk="true" 
  22.         diskSpoolBufferSizeMB="20"  
  23.         timeToIdleSeconds="300"  
  24.         timeToLiveSeconds="600" 
  25.         memoryStoreEvictionPolicy="FIFO"> 
  26.          
  27.     </cache> 
  28.  
  29. </ehcache> 
  30.  
  31.  
  32. <!-- 
  33.     Default Cache configuration. These will applied to caches programmatically created through the CacheManager.   
  34.     The following attributes are required for defaultCache:   
  35.  
  36.       maxInMemory       - Sets the maximum number of objects that will be created in memory   
  37.        
  38.       eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element   
  39.                           is never expired.   
  40.                            
  41.       timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used   
  42.                           if the element is not eternal. Idle time is now - last accessed time   
  43.  
  44.       timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used   
  45.                           if the element is not eternal. TTL is now - creation time   
  46.  
  47.       overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache   
  48.                           has reached the maxInMemory limit.   
  49. -->  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值