Ehcache(2.9.x) - API Developer Guide, Basic Caching
Creating a CacheManager
All usages of the Ehcache API start with the creation of a CacheManager. The following code snippets illustrate various ways to create one.
Singleton versus Instance
The following creates a singleton CacheManager using defaults, then list caches.
CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();
The following creates a CacheManager instance using defaults, then list caches.
CacheManager manager = CacheManager.newInstance();
String[] cacheNames = manager.getCacheNames();
The following creates two CacheManagers, each with a different configuration, and list the caches in each.
CacheManager manager1 = CacheManager.newInstance("src/config/ehcache1.xml");
CacheManager manager2 = CacheManager.newInstance("src/config/ehcache2.xml");
String[] cacheNamesForManager1 = manager1.getCacheNames();
String[] cacheNamesForManager2 = manager2.getCacheNames();
Loading a Configuration
When a CacheManager is created, it creates caches found in a provided configuration.
The following creates a CacheManager based on the configuration defined in the ehcache.xml file in the classpath.
CacheManager manager = CacheManager.newInstance();
The following creates a CacheManager based on a specified configuration file.
CacheManager manager = CacheManager.newInstance("src/config/ehcache.xml");
The following creates a CacheManager from a configuration resource in the classpath.
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = CacheManager.newInstance(url);
The following creates a CacheManager from a configuration in an InputStream.
InputStream fis = new FileInputStream(new File ("src/config/ehcache.xml").getAbsolutePath());
try {
CacheManager manager = CacheManager.newInstance(fis);
} finally {
fis.close();
}
Adding and Removing Caches Programmatically
Adding Caches Programmatically
You are not limited to using caches that are placed in the CacheManager configuration. A new cache based on the default configuration can be added to a CacheManager very simply:
manager.addCache(cacheName);
For example, the following adds a cache called testCache to CacheManager called singletonManager. The cache is configured using defaultCache from the CacheManager configuration.
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
As shown below, you can also create a new cache with a specified configuration and add the cache to a CacheManager. Note that when you create a new cache, it is not usable until it has been added to a CacheManager.
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
singletonManager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
Below is another way to create a new cache with a specified configuration. This example creates a cache called testCache and adds it CacheManager called manager.
// Create a singleton CacheManager using defaults
CacheManager manager = CacheManager.create();
// Create a Cache specifying its configuration.
Cache testCache = new Cache(new CacheConfiguration("testCache", maxEntriesLocalHeap)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
.eternal(false)
.timeToLiveSeconds(60)
.timeToIdleSeconds(30)
.diskExpiryThreadIntervalSeconds(0)
.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP)));
manager.addCache(testCache);
For a full list of parameters for a new Cache, see the Cache constructor at http://ehcache.org/xref/net/sf/ehcache/Cache.html.
Removing Caches Programmatically
The following removes the cache called sampleCache1:
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");
Performing Basic Cache Operations
The following examples refer to manager, which is a reference to a CacheManager that contains a cache called sampleCache1.
Obtaining a reference to a Cache
The following obtains a Cache called sampleCache1, which has been preconfigured in the configuration file
Cache cache = manager.getCache("sampleCache1");
Putting an Element in Cache
The following puts an element into a cache
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);
Updating and Element in Cache
The following updates an element in a cache. Even though cache.put( ) is used, Ehcache knows there is an existing element, and considers the put operation as an update for the purpose of notifying cache listeners.
Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1"));
// This updates the entry for "key1"
cache.put(new Element("key1", "value2"));
Getting an Element from Cache
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();
Removing an Element from Cache
The following removes an element with a key of key1.
Cache cache = manager.getCache("sampleCache1");
cache.remove("key1");
Obtaining Cache Sizes
The following gets the number of elements currently in the cache.
Cache cache = manager.getCache("sampleCache1");
int elementsInMemory = cache.getSize();
The following gets the number of elements currently in the MemoryStore.
Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getMemoryStoreSize();
The following gets the number of elements currently in the DiskStore.
Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getDiskStoreSize();
Shutdown the CacheManager
You should shut down a CacheManager after use. It does have a shut-down hook, but it is a best practice to shut it down in your code.
The following shuts down the singleton CacheManager:
CacheManager.getInstance().shutdown();
The following shuts down a CacheManager instance, assuming you have a reference to the CacheManager called manager:
manager.shutdown();
For additional examples, see CacheManagerTest at http://ehcache.org/xref-test/net/sf/ehcache/CacheManagerTest.html.
Ehcache(2.9.x) - API Developer Guide, Basic Caching的更多相关文章
- Ehcache(2.9.x) - API Developer Guide, Key Classes and Methods
About the Key Classes Ehcache consists of a CacheManager, which manages logical data sets represente ...
- Ehcache(2.9.x) - API Developer Guide, Transaction Support
About Transaction Support Transactions are supported in versions of Ehcache 2.0 and higher. The 2.3. ...
- Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches
About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...
- Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms
About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...
- Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns
There are several common access patterns when using a cache. Ehcache supports the following patterns ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking
About Explicit Locking Ehcache contains an implementation which provides for explicit locking, using ...
- Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches
About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...
- Ehcache(2.9.x) - API Developer Guide, Cache Loaders
About Cache Loaders A CacheLoader is an interface that specifies load() and loadAll() methods with a ...
随机推荐
- WebForm中如何防止页面刷新,后退导致的重复提交
当用户按下浏览器中的 F5 键刷新当前页面时,对这一过程进行检测所需的操作步骤.页面刷新是浏览器对特定用户操作(按 F5 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
- POJ 3041 Asteroids (二分图最小点覆盖)
题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...
- TypeScript学习笔记(四):函数
这篇笔记我们来看看TypeScript中的函数. 函数类型 在JavaScript中存在两种定义函数的方法,如下: //命名函数 function add(x, y) { return x+y; } ...
- CoreSeek有符号整型
数据库status字段的值有:-1,0,1 设置过滤字段,发现sql_attr_uint不支持负数,后改用sql_attr_bigint sql_attr_bigint = status sql_at ...
- eclipse scons 使用指南
http://sconsolidator.com/projects/sconsolidator/wiki/Getting_Started Add SCons support to an existin ...
- eclipse 插件
eclipse 插件 pdt(http://projects.eclipse.org/projects/tools.pdt/downloads) PHP Development Tools 3.2 R ...
- C语言中用宏来作注释
看了PostgreSQL的代码后,我觉得有不理解的地方,比如: 例如这样的: /* Options that may appear after CATALOG (on the same line) * ...
- mvc4 web-api 与unity搭建接口
对于接口重要的应该是 功能实现,合法性验证,性能监控,日志等模块 通过unity aop功能可以实现统一的日志模块和性能监控. 1.新建mvc4 webapi项目 nuget添加 unity 3.0+ ...
- 升级、备份红帽PaaS openshift 上的 wordpress
红帽提供了一个很稳定的PAAS服务平台:openshift!此博客即作为wordpress建在里面. 这里记录怎样升级与备份wordpress. 预备: 安装 openshift command li ...
- mysql-锁表机制分析(转)
为了给高并发情况下的mysql进行更好的优化,有必要了解一下mysql查询更新时的锁表机制.一.概述MySQL有三种锁的级别:页级.表级.行级.MyISAM和MEMORY存储引擎采用的是表级锁(tab ...