zoukankan      html  css  js  c++  java
  • Ehcache(2.9.x)

    About Cache Extensions

    Cache extensions are a general-purpose mechanism to allow generic extensions to a cache. Cache extensions are tied into the cache lifecycle. For that reason, this interface has the lifecycle methods.

    Cache extensions are created using the CacheExtensionFactory, which has a createCacheCacheExtension() method that takes as a parameter a Cache and properties. It can thus call back into any public method on Cache, including, of course, the load methods. Cache extensions are suitable for timing services, where you want to create a timer to perform cache operations. (Another way of adding Cache behavior is to decorate a cache. For an example of, Blocking and Self Populating Caches.)

    Because a CacheExtension holds a reference to a Cache, the CacheExtension can do things such as registering a CacheEventListener or even a CacheManagerEventListener, all from within a CacheExtension, creating more opportunities for customization.

    Declarative Configuration

    Cache extensions are configured per cache. Each cache can have zero or more.

    You configure a cache extension by adding a cacheExceptionHandlerFactory element as shown in the following example:

    <cache ...> 
        <cacheExtensionFactory 
            class="com.example.FileWatchingCacheRefresherExtensionFactory" 
            properties="refreshIntervalMillis=18000, loaderTimeout=3000, flushPeriod=whatever, ..."/> 
    </cache>

    Implementing a Cache Extension Factory and Cache Extension

    A CacheExtensionFactory is an abstract factory for creating cache extension. 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 CacheExtensionFactory, which is reproduced below:

    /**
     * An abstract factory for creating <code>CacheExtension</code>s. Implementers should provide their own
     * concrete factory extending this factory. It can then be configured in ehcache.xml.
     *
     * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
     * @version $Id: CacheExtensionFactory.java 5594 2012-05-07 16:04:31Z cdennis $
     */
    public abstract class CacheExtensionFactory {
    
        /**
         * @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
         */
        public abstract CacheExtension createCacheExtension(Ehcache cache, Properties properties); 
    
    }

    The factory creates a concrete implementation of the CacheExtension interface, which is reproduced below:

    /**
     * This is a general purpose mechanism to allow generic extensions to a Cache.
     * <p/>
     * CacheExtensions are tied into the Cache lifecycle. For that reason this interface has the
     *  lifecycle methods.
     * <p/>
     * CacheExtensions are created using the CacheExtensionFactory which has a
     * <code>createCacheCacheExtension()</code> method which takes as a parameter a Cache and
     * properties. It can thus call back into any public method on Cache, including, of course,
     *  the load methods.
     * <p/>
     * CacheExtensions are suitable for timing services, where you want to create a timer to
     * perform cache operations. The other way of adding Cache behaviour is to decorate a cache.
     * See {@link net.sf.ehcache.constructs.blocking.BlockingCache} for an example of how to do
     * this.
     * <p/>
     * Because a CacheExtension holds a reference to a Cache, the CacheExtension can do things
     * such as registering a CacheEventListener or even a CacheManagerEventListener, all from
     * within a CacheExtension, creating more opportunities for customisation.
     *
     * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
     * @version $Id: CacheExtension.java 5594 2012-05-07 16:04:31Z cdennis $
     */
    public interface CacheExtension {
    
        /**
         * Notifies providers to initialise themselves.
         * <p/>
         * 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 CacheException
         */
        void init();
    
        /**
         * Providers may be doing all sorts of exotic things and need to be able to clean up on
         * dispose.
         * <p/>
         * Cache operations are illegal when this method is called. The cache itself is partly
         * disposed when this method is called.
         *
         * @throws CacheException
         */
        void dispose() throws CacheException;
    
        /**
         * Creates a clone of this extension. This method will only be called by ehcache before a
         * cache is initialized.
         * <p/>
         * Implementations should throw CloneNotSupportedException if they do not support clone
         * but that will stop them from being used with defaultCache.
         *
         * @param cache the cache extended
         * @return a clone
         * @throws CloneNotSupportedException if the extension could not be cloned.
         */
        public CacheExtension clone(Ehcache cache) throws CloneNotSupportedException;
    
    
        /**
         * @return the status of the extension
         */
        public Status getStatus();
    }

    Programmatic Configuration

    Cache extensions can also be programmatically added to a Cache as shown below:

    TestCacheExtension testCacheExtension = new TestCacheExtension(cache, ...); 
    testCacheExtension.init(); 
    cache.registerCacheExtension(testCacheExtension);
  • 相关阅读:
    页面内容[置顶] 采用Div+Css布局——牛腩
    区域函数[置顶] linux 3.4.10 内核内存管理源代码分析5:伙伴系统初始化
    安装应用android批量安装APK
    选择版本Win7系统VS2010下搭建qt开发环境
    字体格式The format of Oracle tnsnames.ora file
    程序执行vhdl中延时器的编写
    概率链接nbu 2416 奇怪的散步
    中国主题ASP.Net课堂实验4
    要求终点HDU1010:Tempter of the Bone
    解决方案编程苦B和二B程序员别忘了养生
  • 原文地址:https://www.cnblogs.com/huey/p/5847160.html
Copyright © 2011-2022 走看看