`
carlosfu
  • 浏览: 571911 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Ba8b5055-9c58-3ab0-8a1c-e710f0495d2c
BigMemory实战与理...
浏览量:30026
53b2087e-c637-34d2-b61d-257846f73ade
RedisCluster开...
浏览量:149142
C9f66038-7478-3388-8086-d20c1f535495
缓存的使用与设计
浏览量:122714
社区版块
存档分类
最新评论

BigMemory系列文章--4.Ehcache重要类和常用API

阅读更多

1. CacheManager: 管理Cache

2. Cache: 管理K-V缓存对象

3. Element: K-V缓存对象

下面这张图形象表现三者的关系:


 

二、Ehcache常用API

1. 创建CacheManager有多种方法:

CacheManager.newInstance(Configuration configuration) – Create a new CacheManager or return the existing one named in the configuration.(非单例)
CacheManager.create() – Create a new singleton CacheManager with default configuration, or return the existing singleton. This is the same as CacheManager.getInstance().(单例)
CacheManager.create(Configuration configuration) – Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.(单例)
new CacheManager(Configuration configuration) – Create a new CacheManager, or throw an exception if the CacheManager named in the configuration already exists or if the parameter (configuration) is null.(非单例)

 

实际应用建议使用第三种:CacheManager.create(Configuration configuration)

CacheManager cacheManager = CacheManager.create(BaseTest.class.getClassLoader().getResourceAsStream("ehcache.xml"));
//加入jmx
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);        
Cache cache = cacheManager.getCache("yourCacheName");

  

2. Cache常用API

(1) 添加和更新key-value
//add
cache.put(new Element("key1", "value1")); 
//This updates the entry for "key1" 
cache.put(new Element("key1", "value2"));
  
(2) 获取key-value:

 

The following gets a Serializable value from an element with a key of key1.
Cache cache = manager.getCache("sampleCache1"); 
Element element = cache.get("key1"); 
Serializable value = element.getValue();
 
The following gets a NonSerializable value from an element with a key of key1.
Cache cache = manager.getCache("sampleCache1"); 
Element element = cache.get("key1"); 
Object value = element.getObjectValue();

 

(3) 删除key-value:

 

cache.remove("key1");

 

 

(4)获取cache大小:

 

The following gets the number of elements currently in the cache.
int elementsInMemory = cache.getSize();
 
The following gets the number of elements currently in the MemoryStore.
long elementsInMemory = cache.getMemoryStoreSize();
 
 
The following gets the number of elements currently in the DiskStore.
long elementsOnDisk = cache.getDiskStoreSize();

 

(5)获取内存使用情况(生产环境不用使用,影响性能):

 

cache.calculateInMemorySize()
(6) 判断key是否过期:

 

boolean isExpired = cache.isExpired(Element element)

 

(7) 获取统计信息:

 

StatisticsGateway statisticsGateway = ehcache.getStatistics(); 
(8) 批量添加:

 

void putAll(Collection<Element> elements)
(9) 批量获取:

 

Map<Object,Element> getAll(Collection<?> keys)
(10) 判断key的位置:

 

boolean isElementInMemory(Object key)
boolean isElementOnDisk(Object key)

 

  • 大小: 82 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics