Class BlockingCache

All Implemented Interfaces:

public class BlockingCache extends Object implements Cache

Simple blocking decorator

Simple and inefficient version of EhCache's BlockingCache decorator. It sets a lock over a cache key when the element is not found in cache. This way, other threads will wait until this element is filled instead of hitting the database.

By its nature, this implementation can cause deadlock when used incorrectly.

Eduardo Macarron
  • Constructor Details

    • BlockingCache

      public BlockingCache(Cache delegate)
  • Method Details

    • getId

      public String getId()
      Specified by:
      getId in interface Cache
      The identifier of this cache
    • getSize

      public int getSize()
      Description copied from interface: Cache
      Optional. This method is not called by the core.
      Specified by:
      getSize in interface Cache
      The number of elements stored in the cache (not its capacity).
    • putObject

      public void putObject(Object key, Object value)
      Specified by:
      putObject in interface Cache
      key - Can be any object but usually it is a CacheKey
      value - The result of a select.
    • getObject

      public Object getObject(Object key)
      Specified by:
      getObject in interface Cache
      key - The key
      The object stored in the cache.
    • removeObject

      public Object removeObject(Object key)
      Description copied from interface: Cache
      As of 3.3.0 this method is only called during a rollback for any previous value that was missing in the cache. This lets any blocking cache to release the lock that may have previously put on the key. A blocking cache puts a lock when a value is null and releases it when the value is back again. This way other threads will wait for the value to be available instead of hitting the database.
      Specified by:
      removeObject in interface Cache
      key - The key
      Not used
    • clear

      public void clear()
      Description copied from interface: Cache
      Clears this cache instance.
      Specified by:
      clear in interface Cache
    • getTimeout

      public long getTimeout()
    • setTimeout

      public void setTimeout(long timeout)