Ehcache(2.9.x) - API Developer Guide, Cache Decorators
About Cache Decorators
Ehcache uses the Ehcache interface, of which Cache is an implementation. It is possible and encouraged to create Ehcache decorators that are backed by a Cache instance, implement Ehcache and provide extra functionality.
The Decorator pattern is one of the well known Gang of Four patterns.
Decorated caches are accessed from the CacheManager using CacheManager.getEhcache(String name). Note that, for backward compatibility,CacheManager.getCache(String name) has been retained. However only CacheManager.getEhcache(String name) returns the decorated cache.
Built-in Decorators
BlockingCache
This is a Blocking decorator for Ehcache that allows concurrent read access to elements already in the cache. If the element is null, other reads will block until an element with the same key is put into the cache. This decorator is useful for constructing read-through or self-populating caches. BlockingCache is used by CachingFilter.
SelfPopulatingCache
SelfPopulatingCache extends BlockingCache. Multiple threads attempting to access a null element will block until the first thread completes. If refresh is being called the threads do not block - they return the stale data. This is very useful for engineering highly scalable systems.
Caches with Exception Handling
Creating a Decorator
Declarative Creation
You can configure decorators directly in ehcache.xml. The decorators will be created and added to the CacheManager.
It accepts the name of a concrete class that extends net.sf.ehcache.constructs.CacheDecoratorFactory
The properties will be parsed according to the delimiter (default is comma ",") and passed to the concrete factory's createDecoratedEhcache(Ehcache cache, Properties properties) method along with the reference to the owning cache.
It is configured as per the following example:
<cacheDecoratorFactory
class="com.company.SomethingCacheDecoratorFactory"
properties="property1=36 ..." />
Note that decorators can be configured against the defaultCache. This is very useful for frameworks like Hibernate that add caches based on the configuration of the defaultCache.
Programmatic Creation
Cache decorators are created as follows:
BlockingCache newBlockingCache = new BlockingCache(cache);
The class must implement Ehcache.
Adding Decorated Caches to a CacheManager
Having created a decorator programmatically, it is generally useful to put it in a place where multiple threads can access it. Note that decorators created via configuration in ehcache.xml have already been added to the CacheManager.
Using CacheManager.replaceCacheWithDecoratedCache()
A built-in way is to replace the Cache in CacheManager with the decorated one. This is achieved as in the following example:
cacheManager.replaceCacheWithDecoratedCache(cache, newBlockingCache);
The CacheManager.replaceCacheWithDecoratedCache() method requires that the decorated cache be built from the underlying cache from the same name.
Any calls to get the cache out of the CacheManager now return the decorated one.
A word of caution. This method should be called in an appropriately synchronized init style method before multiple threads attempt to use it. All threads must be referencing the same decorated cache. An example of a suitable init method is found in CachingFilter:
/**
* The cache holding the web pages. Ensure that all threads for a given cache
* name are using the same instance of this.
*/
private BlockingCache blockingCache;
/**
* Initialises blockingCache to use
*
* @throws CacheException The most likely cause is that a cache has not been
* configured in Ehcache's configuration file ehcache.xml
* for the filter name
*/
public void doInit() throws CacheException {
synchronized (this.getClass()) {
if (blockingCache == null) {
final String cacheName = getCacheName();
Ehcache cache = getCacheManager().getEhcache(cacheName);
if (!(cache instanceof BlockingCache)) {
//decorate and substitute
BlockingCache newBlockingCache = new BlockingCache(cache);
getCacheManager().replaceCacheWithDecoratedCache(cache, newBlockingCache);
}
blockingCache = (BlockingCache) getCacheManager().getEhcache(getCacheName());
}
}
}
Ehcache blockingCache = singletonManager.getEhcache("sampleCache1");
The returned cache will exhibit the decorations.
Using CacheManager.addDecoratedCache()
Sometimes you want to add a decorated cache but retain access to the underlying cache.
The way to do this is to create a decorated cache and then call cache.setName(new_name) and then add it to CacheManager with CacheManager.addDecoratedCache().
/**
* Adds a decorated {@link Ehcache} to the CacheManager. This method neither
* creates the memory/disk store nor initializes the cache. It only adds the
* cache reference to the map of caches held by this cacheManager.
*
* It is generally required that a decorated cache, once constructed, is made
* available to other execution threads. The simplest way of doing this is to
* either add it to the cacheManager with a different name or substitute the
* original cache with the decorated one.
*
* This method adds the decorated cache assuming it has a different name.
* If another cache (decorated or not) with the same name already exists,
* it will throw {@link ObjectExistsException}. For replacing existing
* cache with another decorated cache having same name, please use
* {@link #replaceCacheWithDecoratedCache(Ehcache, Ehcache)}
*
* Note that any overridden Ehcache methods by the decorator will take on
* new behaviours without casting. Casting is only required for new methods
* that the decorator introduces. For more information see the well known
* Gang of Four Decorator pattern.
*
* @param decoratedCache
* @throws ObjectExistsException
* if another cache with the same name already exists.
*/
public void addDecoratedCache(Ehcache decoratedCache) throws ObjectExistsException {
Ehcache(2.9.x) - API Developer Guide, Cache Decorators的更多相关文章
- 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 ...
- 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, Cache Manager Event Listeners
About CacheManager Event Listeners CacheManager event listeners allow implementers to register callb ...
- Ehcache(2.9.x) - API Developer Guide, Cache Event Listeners
About Cache Event Listeners Cache listeners allow implementers to register callback methods that wil ...
- Ehcache(2.9.x) - API Developer Guide, Cache Exception Handlers
About Exception Handlers By default, most cache operations will propagate a runtime CacheException o ...
- Ehcache(2.9.x) - API Developer Guide, Cache Extensions
About Cache Extensions Cache extensions are a general-purpose mechanism to allow generic extensions ...
- 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, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
随机推荐
- aspose.cell制作excel常见写法
//设置Excel的基本格式信息 Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[]; St ...
- Android游戏之平台接入的一点记录
最近手头有需要接入多个渠道的工作,我负责的是Android方面的接入,一般来说,渠道是非常多的,每一个渠道调用的接口都不一致,如果每一个渠道都要自己去弄回非常的耗时,所以网上会有一些接入的中间件提供商 ...
- C#学习笔记(十):反射
反射 放射是指在程序运行时动态的获取类的信息的机制,我们下面来看看C#中的反射. Type Type 为 System.Reflection 功能的根,也是访问元数据的主要方式. 使用 Type 的成 ...
- 在MVC项目中使用RDLC报表
原文地址:http://www.cnblogs.com/wuhuacong/p/4109833.html RDLC是一个不错的报表,有着比较不错的设计模式和展现效果,在我的Winform开发里面,使用 ...
- c++未指定返回值
int foo() { } foo返回的是随机数 函数的返回值在函数堆栈的参数后面,由一个指向寄存器的指针决定,函数返回时,调用者取指针取寄存器中的值作为返回值. 如果指定返回值,函数在返回前会将返回 ...
- python登录csdn并自动评论下载资源脚本
功能 1.自动登录csdn 2.查找未评论的资源并自动评论 用到的库 1.python自带的requests,获取以及发送网页数据 2.python自带的time,用作休眠,csdn资源一段时间内只允 ...
- list删除操作 java.util.ConcurrentModificationException
首先大家先看一段代码: public static void main(String[] args) { List<String> listStr = new ArrayList<S ...
- iOS 抖动动画
-(void)animationWithCell:(WaterLevelCollectionCell *)cell{ // 添加摇晃动画 { CAKeyframeAnimation *frame=[C ...
- php 建立类POST/GET 的HTTP请求
1.第一种利用fsock的方式来建立类POST的请求. <?php $srv_ip = '192.168.1.5';//你的目标服务地址. $srv_port = 80;//端口 $url = ...
- 直接下载Google Play市场的APK
传送门在这里:http://apps.evozi.com/apk-downloader/ 似乎很方便.很迅速的样子,忍不住在这里记录一下.