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 ...
随机推荐
- Serializable 序列化为文件
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- CodeForces 732B Cormen — The Best Friend Of a Man (贪心)
题意:给定n和k表示,狗要在任意连续两天散步次数要至少为k,然后就是n个数,表示每天的时间,让你增加最少次数使得这个条件成立. 析:贪心,策略是从开始到最后暴力,每次和前面一个相比,如果相加不够k,那 ...
- 关于mysql_fetch_****
今天调试如下代码: mysql_select_db('content',$link);//选择数据库 mysql_query("set names utf8");//设置编码格式 ...
- spring事务注解
@Transactional只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能. Spring使用声明式事务处理,默认 ...
- CKeditor3.6.2 配置与精简
一.使用方法: 1.在页面<head>中引入ckeditor核心文件ckeditor.js <script type="text/javascript" src= ...
- Spring JdbcTemplate小结
提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流程的动作,提供 ...
- C#中反射的使用(How to use reflect in CSharp)(2)
在上一篇里,我们叨逼了好多如何获取到程序集里的对象,但是对象有了,还不知道怎么调,OK,下面开始干这个对象: 首先,我们对上一篇的对象做了一些修改,以适应多种情况: using System; usi ...
- MEF 编程指南(二):定义可组合部件和契约
可组合部件(Composable Parts) 在 MEF 内部可组合部件是一个可组合单元.可组合部件导出其他可组合部件需要的服务,并且从其他可组合部件导入服务.在 MEF 编程模型中,可组合部件 ...
- 一个奇怪的html上url参数问题
今天踩了一个坑 如xxx.com/xxx/xxx?code=+adfdf 我需要拿到 code=+adfdf 但是后台拿到的是 adfdf, 后来只能对 code的值进行 urlencode处理了
- BW知识点总结及面试要点
1. 如何理解数据仓库? 数据仓库 是 一个面向主题的,集成的,相对稳定的,反应历史变化的数据集合,用于支持管理决策. 2. OLAP 和 OLTP的基本概念 和 区别? Ol ...