Class SelfPopulatingCache

  • All Implemented Interfaces:
    java.lang.Cloneable, Ehcache, InternalEhcache
    Direct Known Subclasses:
    UpdatingSelfPopulatingCache

    public class SelfPopulatingCache
    extends BlockingCache
    A selfpopulating decorator for Ehcache that creates entries on demand.

    Clients of the cache simply call it without needing knowledge of whether the entry exists in the cache.

    The cache is designed to be refreshed. Refreshes operate on the backing cache, and do not degrade performance of BlockingCache.get(java.io.Serializable) calls.

    Thread safety depends on the factory being used. The UpdatingCacheEntryFactory should be made thread safe. In addition users of returned values should not modify their contents.

    Version:
    $Id$
    Author:
    Greg Luck
    • Field Detail

      • factory

        protected final CacheEntryFactory factory
        A factory for creating entries, given a key
    • Method Detail

      • refresh

        public void refresh()
                     throws CacheException
        Refresh the elements of this cache.

        Refreshes bypass the BlockingCache and act directly on the backing Ehcache. This way, BlockingCache gets can continue to return stale data while the refresh, which might be expensive, takes place.

        Quiet methods are used, so that statistics are not affected. Note that the refreshed elements will not be replicated to any cache peers.

        Configure ehcache.xml to stop elements from being refreshed forever:

        • use timeToIdle to discard elements unused for a period of time
        • use timeToLive to discard elmeents that have existed beyond their allotted lifespan
        Throws:
        CacheException
      • refresh

        public void refresh​(boolean quiet)
                     throws CacheException
        Refresh the elements of this cache.

        Refreshes bypass the BlockingCache and act directly on the backing Ehcache. This way, BlockingCache gets can continue to return stale data while the refresh, which might be expensive, takes place.

        Quiet methods are used if argument 0 is true, so that statistics are not affected, but note that replication will then not occur

        Configure ehcache.xml to stop elements from being refreshed forever:

        • use timeToIdle to discard elements unused for a period of time
        • use timeToLive to discard elmeents that have existed beyond their allotted lifespan
        Parameters:
        quiet - whether the backing cache is quietly updated or not, if true replication will not occur
        Throws:
        CacheException
        Since:
        1.6.1
      • refresh

        public Element refresh​(java.lang.Object key)
                        throws CacheException
        Refresh a single element.

        Refreshes bypass the BlockingCache and act directly on the backing Ehcache. This way, BlockingCache gets can continue to return stale data while the refresh, which might be expensive, takes place.

        If the element is absent it is created

        Quiet methods are used, so that statistics are not affected. Note that the refreshed element will not be replicated to any cache peers.

        Parameters:
        key -
        Returns:
        the refreshed Element
        Throws:
        CacheException
        Since:
        1.6.1
      • refresh

        public Element refresh​(java.lang.Object key,
                               boolean quiet)
                        throws CacheException
        Refresh a single element.

        Refreshes bypass the BlockingCache and act directly on the backing Ehcache. This way, BlockingCache gets can continue to return stale data while the refresh, which might be expensive, takes place.

        If the element is absent it is created

        Quiet methods are used if argument 1 is true, so that statistics are not affected, but note that replication will then not occur

        Parameters:
        key -
        quiet - whether the backing cache is quietly updated or not, if true replication will not occur
        Returns:
        the refreshed Element
        Throws:
        CacheException
        Since:
        1.6.1
      • refreshElement

        protected void refreshElement​(Element element,
                                      Ehcache backingCache)
                               throws java.lang.Exception
        Refresh a single element.
        Parameters:
        element - the Element to refresh
        backingCache - the underlying Ehcache.
        Throws:
        java.lang.Exception
      • refreshElement

        protected Element refreshElement​(Element element,
                                         Ehcache backingCache,
                                         boolean quiet)
                                  throws java.lang.Exception
        Refresh a single element.
        Parameters:
        element - the Element to refresh
        backingCache - the underlying Ehcache.
        quiet - whether to use putQuiet or not, if true replication will not occur
        Returns:
        the refreshed Element
        Throws:
        java.lang.Exception
        Since:
        1.6.1
      • makeAndCheckElement

        protected static Element makeAndCheckElement​(java.lang.Object key,
                                                     java.lang.Object value)
                                              throws CacheException
        Both CacheEntryFactory can return an Element rather than just a regular value this method test this, making a fresh Element otherwise. It also enforces the rule that the CacheEntryFactory must provide the same key (via equals() not necessarily same object) if it is returning an Element.
        Parameters:
        key -
        value -
        Returns:
        the Element to be put back in the cache
        Throws:
        CacheException - for various illegal states which could be harmful