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 variety of parameters. CacheLoaders are incorporated into the core Ehcache classes and can be configured in ehcache.xml. CacheLoaders are invoked in the following Cache methods:
- getWithLoader (synchronous)
- getAllWithLoader (synchronous)
- load (asynchronous)
- loadAll (asynchronous)
The methods will invoke a CacheLoader if there is no entry for the key or keys requested. By implementing CacheLoader, an application form of loading can take place. The get... methods follow the pull-through cache pattern. The load... methods are useful as cache warmers.
CacheLoaders are similar to the CacheEntryFactory used in SelfPopulatingCache, however SelfPopulatingCache is a decorator to Ehcache.
CacheLoaders can be set either declaratively in the ehcache.xml configuration file or programmatically. If a CacheLoader is set, it becomes the default CacheLoader. Some of the methods invoking loaders enable an override CacheLoader to be passed in as a parameter. More than one CacheLoader can be registered, in which case the loaders form a chain which are executed in order. If a loader returns null, the next in chain is called.
Declarative Configuration
The cacheLoaderFactory element specifies a CacheLoader, which can be used both asynchronously and synchronously to load objects into a cache.
<cache ...>
<cacheLoaderFactory class="com.example.ExampleCacheLoaderFactory" properties="type=int,startCounter=10"/>
</cache>
Implementing a CacheLoaderFactory and CacheLoader
CacheLoaderFactory is an abstract factory for creating CacheLoaders. Implementers should provide their own concrete factory, extending this abstract factory. It can then be configured in ehcache.xml. The factory class needs to be a concrete subclass of the abstract factory class CacheLoaderFactory, which is reproduced below:
/**
* An abstract factory for creating cache loaders. Implementers should provide
* their own concrete factory extending this factory.
*
* There is one factory method for JSR107 Cache Loaders and one for Ehcache ones.
* The Ehcache loader is a sub interface of the JSR107 Cache Loader.
*
* Note that both the JCache and Ehcache APIs also allow the CacheLoader to be set
* programmatically.
* @author Greg Luck
*/
public abstract class CacheLoaderFactory {
/**
* Creates a CacheLoader using the JSR107 creational mechanism.
* This method is called from {@link net.sf.ehcache.jcache.JCacheFactory}
*
* @param environment the same environment passed into
* {@link net.sf.ehcache.jcache.JCacheFactory}.
* This factory can extract any properties it needs from the environment.
* @return a constructed CacheLoader
*/
public abstract net.sf.jsr107cache.CacheLoader createCacheLoader(Map environment);
/**
* Creates a CacheLoader using the Ehcache configuration mechanism at the time
* the associated cache is created.
*
* @param properties implementation specific properties. These are configured as
* comma separated name value pairs in ehcache.xml
* @return a constructed CacheLoader
*/
public abstract net.sf.ehcache.loader.CacheLoader createCacheLoader(Properties properties);
/**
* @param cache the cache this extension should hold a reference to,
* and to whose lifecycle it should be bound.
* @param properties implementation specific properties configured as delimiter
* separated name value pairs in ehcache.xml
* @return a constructed CacheLoader
*/
public abstract CacheLoader createCacheLoader(Ehcache cache, Properties properties);
}
The factory creates a concrete implementation of the CacheLoader interface, which is reproduced below. A CacheLoader is bound to the lifecycle of a cache, so that the init() method is called during cache initialization, and dispose() is called on disposal of a cache.
/**
* Extends JCache CacheLoader with load methods that take an argument in addition
* to a key
* @author Greg Luck
*/
public interface CacheLoader extends net.sf.jsr107cache.CacheLoader {
/**
* Load using both a key and an argument.
*
* JCache will call through to the load(key) method, rather than this method,
* where the argument is null.
*
* @param key the key to load the object for
* @param argument can be anything that makes sense to the loader
* @return the Object loaded
* @throws CacheException
*/
Object load(Object key, Object argument) throws CacheException;
/**
* Load using both a key and an argument.
*
* JCache will use the loadAll(key) method where the argument is null.
*
* @param keys the keys to load objects for
* @param argument can be anything that makes sense to the loader
* @return a map of Objects keyed by the collection of keys passed in.
* @throws CacheException
*/
Map loadAll(Collection keys, Object argument) throws CacheException;
/**
* Gets the name of a CacheLoader
*
* @return the name of this CacheLoader
*/
String getName();
/**
* Creates a clone of this extension. This method will only be called by Ehcache
* before a cache is initialized.
*
* Implementations should throw CloneNotSupportedException if they do not support
* clone, but that will stop them from being used with defaultCache.
*
* @return a clone
* @throws CloneNotSupportedException if the extension could not be cloned.
*/
public CacheLoader clone(Ehcache cache) throws CloneNotSupportedException;
/**
* Notifies providers to initialise themselves.
*
* This method is called during the Cache's initialise method after it has changed
* it's status to alive. Cache operations are legal in this method.
*
* @throws net.sf.ehcache.CacheException
*/
void init();
/**
* Providers may be doing all sorts of exotic things and need to be able to clean
* up on dispose.
*
* Cache operations are illegal when this method is called. The cache itself is
* partly disposed when this method is called.
*
* @throws net.sf.ehcache.CacheException
*/
void dispose() throws net.sf.ehcache.CacheException;
/**
* @return the status of the extension
*/
public Status getStatus();
}
The implementations need to be placed in the classpath accessible to ehcache. For details on how the loading of these classes will be done, see Class Loading.
Programmatic Configuration
The following methods on Cache allow runtime interrogation, registration and unregistration of loaders:
/**
* Register a {@link CacheLoader} with the cache. It will then be tied into the
* cache lifecycle.
*
* If the CacheLoader is not initialised, initialise it.
*
* @param cacheLoader A Cache Loader to register
*/
public void registerCacheLoader(CacheLoader cacheLoader) {
registeredCacheLoaders.add(cacheLoader);
}
/**
* Unregister a {@link CacheLoader} with the cache. It will then be detached
* from the cache lifecycle.
*
* @param cacheLoader A Cache Loader to unregister
*/
public void unregisterCacheLoader(CacheLoader cacheLoader) {
registeredCacheLoaders.remove(cacheLoader);
}
/**
* @return the cache loaders as a live list
*/
public List<CacheLoader> getRegisteredCacheLoaders() {
return registeredCacheLoaders;
}
Ehcache(2.9.x) - API Developer Guide, Cache Loaders的更多相关文章
- Ehcache(2.9.x) - API Developer Guide, Cache Decorators
About Cache Decorators Ehcache uses the Ehcache interface, of which Cache is an implementation. It i ...
- 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 ...
随机推荐
- ADUM1201在隔离RS232中的应用 【瓦特芯收藏】
ADUM1201在隔离RS232中的应用 引言: RS-232是PC机与工业通信中应用最广泛的一种串行接口.RS-232接口最初是由美国EIA(电子工业联合会)规定的用于计算机与终端设备之间通讯的一种 ...
- android WebView将新浪天气为我所用 ------>仅供娱乐
新浪天气提供了一个网页 http://w.sina.com 浏览器访问: 这效果还可以了哦,直接用webview加载出来,效果也可以了哦,不过,这不是我要的.我不希望在我写的应用里到处铺满si ...
- hash_map vs unordered_map vs map vs unordered_set
hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...
- 转:使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
原文地址:http://blog.codinglabs.org/articles/nginx-memc-and-srcache.html 为了提高性能,几乎所有互联网应用都有缓存机制,其中Memcac ...
- ERP存储过程
[dbo].[st_MES_MonitorMachine] -------------------------------------------- USE [ChiefMESNew]GO /**** ...
- HTML5 - HTML5 postMessage API 注意事项
一:发送 window.postMessage("Hello, world", "http://127.0.0.1:8080"); 注意,必须要加上http:/ ...
- Spring声明式事务的配置~~~
/*2011年8月28日 10:03:30 by Rush */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...
- 下载discuz 6 论坛的附件
前段时间我下了个python脚本把emsky的附件全部下载了,之前是因为偶然发现emsky附件不登陆也能访问,直接访问一个url就行了. 后来发现大部分discuz6的论坛都有这个bug,我想是因为d ...
- pjsip视频通信开发(上层应用)之数字键盘的制作
在pjsip视频通信开发(上层应用)之EditText重写中我制作了一个显示输入内容的EditText,这里将制作一个数字键盘,其实跟计算器一样,最多的就是用TableLayout来实现,内部通过权重 ...
- OpenCV 2.2版本号以上显示图片到 MFC 的 Picture Control 控件中
OpenCV 2.2 以及后面的版本号取消掉了 CvvImage.h 和CvvImage.cpp 两个文件,直接导致了苦逼的程序猿无法调用里面的显示函数来将图片显示到 MFC 的 Picture Co ...