Ehcache (2.9.x)-API Developer guide, Cache Decorators

Source: Internet
Author: User

About Cache Decorators

Ehcache uses the Ehcache interface, of which Cache is an implementation. It's possible and encouraged to create Ehcache decorators that's backed by a Cache instance, implement Ehcache and Prov IDE extra functionality.

The Decorator pattern is one of the well known Gang of four patterns.

Decorated caches is 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 DecoratorsBlockingcache

This was a Blocking decorator for Ehcache, allows concurrent read access to elements already in the cache. If the element is null, and other reads would block until an element with the same key was put into the cache. This decorator are useful for constructing read-through or self-populating caches. Blockingcache is used by Cachingfilter.

SelfpopulatingcacheA self-populating Decorator for Ehcache This creates entries on demand. Clients of the cache simply call it without needing knowledge of whether, the entry exists in the cache. If NULL, the entry is created. The cache is designed to be refreshed. Refreshes operate on the backing cache, and does not degrade performance of get calls.

Selfpopulatingcache extends Blockingcache. Multiple threads attempting to access a null element would block until the first thread completes. If Refresh is being called the threads does not block-they return the stale data. This was very useful for engineering highly scalable systems.

Caches with Exception handlingcaches with Exception handlers is decorated. For information about adding an exception handler to a cache, seeCache Exception handlers.

Creating a decoratordeclarative Creation

You can configure decorators directly in ehcache.xml. The decorators is created and added to the CacheManager.

It accepts the name of a concrete class that extends net.sf.ehcache.constructs.CacheDecoratorFactory

The properties would be parsed according to the delimiter (default was comma ",") and passed to the concrete factor Y ' s createdecoratedehcache (Ehcache cache, properties properties) method along with the reference to the owning CA Che.

IT is configured as per the following example:

< cachedecoratorfactory          class = "Com.company.SomethingCacheDecoratorFactory"          Properties= "property1=36 ..."/>

Note that decorators can configured against the Defaultcache. This was very useful for frameworks like Hibernate that add caches based on the configuration of the Defaultcache.

Programmatic Creation

Cache decorators is 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's generally useful to put it in a place where multiple threads can access It. Note that decorators created via configuration in Ehcache.xml has already been added to the CacheManager.

Using Cachemanager.replacecachewithdecoratedcache ()

A built-in-is-to-replace the Cache in CacheManager with the decorated one. achieved as in the following example:

Cachemanager.replacecachewithdecoratedcache (cache, Newblockingcache);

The Cachemanager.replacecachewithdecoratedcache () method requires that the decorated cache is built from the Unde Rlying cache from the same name.

Note that any overridden Ehcache methods would take on new behaviors without casting, as per the normal rules of Java. Casting is a required for new methods, the decorator introduces.

Any calls to get the cache out of the CacheManager now return the decorated one.

A word of caution. This method should is called in a appropriately synchronized init style method before multiple threads attempt to use it. All threads must is referencing the same decorated cache. An example of a suitable Init method was found in Cachingfilter:

/*** The cache holding the Web pages. Ensure that all threads for a given cache * name is using the the same instance of this. */ PrivateBlockingcache Blockingcache;/*** initialises Blockingcache to use * *@throwscacheexception The most likely cause are that a cache have not been * configured in Ehcache ' s CO Nfiguration file Ehcache.xml * For the filter name*/  Public voidDoinit ()throwscacheexception {synchronized( This. GetClass ()) {     if(Blockingcache = =NULL) {       FinalString CacheName =Getcachename (); Ehcache Cache=Getcachemanager (). Getehcache (CacheName); if(! (CacheinstanceofBlockingcache)) {         //Decorate and substituteBlockingcache Newblockingcache =NewBlockingcache (cache);       Getcachemanager (). Replacecachewithdecoratedcache (cache, Newblockingcache); } Blockingcache=(Blockingcache) Getcachemanager (). Getehcache (Getcachename ()); }   } }
Ehcache Blockingcache = Singletonmanager.getehcache ("SampleCache1");

The returned cache would exhibit the decorations.

Using Cachemanager.adddecoratedcache ()

Sometimes want to add a decorated cache but retain access to the underlying cache.

The decorated cache and then call Cache.setname (new_name) and then add it to CacheManager WI Th Cachemanager.adddecoratedcache ().

/*** Adds a decorated {@linkEhcache} to the CacheManager. This method neither * creates the Memory/disk store nor initializes the cache. It is only adds the * cache reference to the map of caches held by this cachemanager. * * It is generally required that a decorated cache, once constructed, was made * available to other execution threads. The simplest-on-doing this was to * either add it to the CacheManager with a different name or substitute the * Origina L 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'll throw {@linkobjectexistsexception}. For replacing existing * cache with another decorated cache has same name, please use * {@link#replaceCacheWithDecoratedCache (Ehcache, Ehcache)} * * Note that any overridden Ehcache methods by the decorator would Take the 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. * * @paramDecoratedcache *@throwsObjectexistsexception * If another cache with the same name already exists.*/  Public voidAdddecoratedcache (Ehcache Decoratedcache)throwsobjectexistsexception {

Ehcache (2.9.x)-API Developer guide, Cache Decorators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.