Class CacheWrapper<K extends Serializable,V extends Serializable>

java.lang.Object
org.jivesoftware.util.cache.CacheWrapper<K,V>
All Implemented Interfaces:
Map<K,V>, Cache<K,V>
Direct Known Subclasses:
ComponentCacheWrapper

public class CacheWrapper<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
Acts as a proxy for a Cache implementation. The Cache implementation can be switched on the fly, which enables users to hold a reference to a CacheWrapper object, but for the underlying Cache implementation to switch from clustered to local, etc.
  • Method Details

    • getWrappedCache

      public Cache<K,V> getWrappedCache()
    • getCapacityUnit

      public Cache.CapacityUnit getCapacityUnit()
      Description copied from interface: Cache
      Defines the unit used to calculate the capacity of the cache.

      When the unit is unknown, null is returned.

      Specified by:
      getCapacityUnit in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the unit to be used to calculate the capacity of this cache.
    • getName

      public String getName()
      Description copied from interface: Cache
      Returns the name of the cache.
      Specified by:
      getName in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the name of the cache.
    • setName

      public void setName(String name)
      Description copied from interface: Cache
      Sets the name of the cache
      Specified by:
      setName in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      name - the name of the cache
    • getMaxCacheSize

      public long getMaxCacheSize()
      Description copied from interface: Cache
      Returns the maximum size of the cache.

      The value should be placed in context of the unit returned by Cache.getCapacityUnit(). When, for example, the capacity unit for this cache is Cache.CapacityUnit.BYTES, then a return value of 2048 should be interpreted as this cache having a capacity of 2048 bytes (or 2KB). When the capacity unit is Cache.CapacityUnit.ENTITIES then this cache can contain 2048 elements (irrespective of their byte size).

      If the cache grows larger than the maximum size, the least frequently used items will be removed.

      If the maximum cache size is set to -1, there is no size limit.

      Specified by:
      getMaxCacheSize in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the maximum size of the cache.
    • setMaxCacheSize

      public void setMaxCacheSize(long maxSize)
      Description copied from interface: Cache
      Sets the maximum size of the cache.

      The value should be placed in context of the unit returned by Cache.getCapacityUnit(). When, for example, the capacity unit for this cache is Cache.CapacityUnit.BYTES, then a return value of 2048 should be interpreted as this cache having a capacity of 2048 bytes (or 2KB). When the capacity unit is Cache.CapacityUnit.ENTITIES then this cache can contain 2048 elements (irrespective of their byte size).

      If the cache grows larger than the maximum size, the least frequently used items will be removed. If the maximum cache size is set to -1, there is no size limit.

      Note: If using the Hazelcast clustering plugin, this will only take effect if the cache is dynamically configured (not defined in the hazelcast-cache-config.xml file), and will not take effect until the next time the cache is created.

      Specified by:
      setMaxCacheSize in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      maxSize - the maximum size of the cache.
    • getMaxCacheSizeRemark

      public String getMaxCacheSizeRemark()
      Description copied from interface: Cache
      An optional, human-readable remark on the maximum cache capacity configuration.
      Specified by:
      getMaxCacheSizeRemark in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      an optional human-readable text
    • getMaxLifetime

      public long getMaxLifetime()
      Description copied from interface: Cache
      Returns the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.
      Specified by:
      getMaxLifetime in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the maximum number of milliseconds before objects are expired.
    • setMaxLifetime

      public void setMaxLifetime(long maxLifetime)
      Description copied from interface: Cache
      Sets the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.

      Note: If using the Hazelcast clustering plugin, this will only take effect if the cache is dynamically configured (not defined in the hazelcast-cache-config.xml file), and will not take effect until the next time the cache is created.

      Specified by:
      setMaxLifetime in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      maxLifetime - the maximum number of milliseconds before objects are expired.
    • getLongCacheSize

      public long getLongCacheSize()
      Description copied from interface: Cache
      Returns the size of the cache.

      The value should be placed in context of the unit returned by Cache.getCapacityUnit(). When, for example, the capacity unit for this cache is Cache.CapacityUnit.BYTES, then a return value of 2048 should be interpreted as all entities of this cache having a combined size of 2024 bytes (or 2KB). When the capacity unit is Cache.CapacityUnit.ENTITIES then this cache currently contains 2048 elements.

      The returned value can be an approximation.

      Specified by:
      getLongCacheSize in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the size of the cache contents.
    • getCacheSizeRemark

      public String getCacheSizeRemark()
      Description copied from interface: Cache
      An optional, human-readable remark on the current size of the cache.
      Specified by:
      getCacheSizeRemark in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      an optional human-readable text
    • getCacheHits

      public long getCacheHits()
      Description copied from interface: Cache
      Returns the number of cache hits. A cache hit occurs every time the get method is called and the cache contains the requested object.

      Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.

      Specified by:
      getCacheHits in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the number of cache hits.
    • getCacheMisses

      public long getCacheMisses()
      Description copied from interface: Cache
      Returns the number of cache misses. A cache miss occurs every time the get method is called and the cache does not contain the requested object.

      Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.

      Specified by:
      getCacheMisses in interface Cache<K extends Serializable,V extends Serializable>
      Returns:
      the number of cache hits.
    • size

      public int size()
      Specified by:
      size in interface Map<K extends Serializable,V extends Serializable>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K extends Serializable,V extends Serializable>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K extends Serializable,V extends Serializable>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K extends Serializable,V extends Serializable>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K extends Serializable,V extends Serializable>
    • values

      public Collection<V> values()
      Description copied from interface: Cache
      IMPORTANT: Unlike the standard Map.values() implementation, the collection returned from this method cannot be modified.
      Specified by:
      values in interface Cache<K extends Serializable,V extends Serializable>
      Specified by:
      values in interface Map<K extends Serializable,V extends Serializable>
      Returns:
      an unmodifiable collection view of the values contained in this map
    • putAll

      public void putAll(Map<? extends K,? extends V> t)
      Specified by:
      putAll in interface Map<K extends Serializable,V extends Serializable>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Description copied from interface: Cache
      IMPORTANT: Unlike the standard Map.entrySet() implementation, the set returned from this method cannot be modified.
      Specified by:
      entrySet in interface Cache<K extends Serializable,V extends Serializable>
      Specified by:
      entrySet in interface Map<K extends Serializable,V extends Serializable>
      Returns:
      an unmodifiable set view of the mappings contained in this map
    • keySet

      public Set<K> keySet()
      Description copied from interface: Cache
      IMPORTANT: Unlike the standard Map.keySet() implementation, the set returned from this method cannot be modified.
      Specified by:
      keySet in interface Cache<K extends Serializable,V extends Serializable>
      Specified by:
      keySet in interface Map<K extends Serializable,V extends Serializable>
      Returns:
      an unmodifiable set view of the keys contained in this map
    • getLock

      @Nonnull public Lock getLock(K key)
      Description copied from interface: Cache
      Returns an existing Lock on the specified key or creates a new one if none was found. This operation is thread safe. Successive calls with the same key may or may not return the same Lock. However, different threads asking for the same Lock at the same time will get the same Lock object.

      The supplied cache may or may not be used depending whether the server is running on cluster mode or not. When not running as part of a cluster then the lock will be unrelated to the cache and will only be visible in this JVM.

      Specified by:
      getLock in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      key - the object that defines the visibility or scope of the lock.
      Returns:
      an existing lock on the specified key or creates a new one if none was found.
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K extends Serializable,V extends Serializable>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K extends Serializable,V extends Serializable>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K extends Serializable,V extends Serializable>
    • addClusteredCacheEntryListener

      public String addClusteredCacheEntryListener(@Nonnull ClusteredCacheEntryListener<K,V> listener, boolean includeValues, boolean includeEventsFromLocalNode)
      Description copied from interface: Cache
      Registers a listener to receive entry events for this cache. To optimize the amount of data that is being processed, this method allows the listener to be registered in a way that suppresses values from being sent to it. In that case, only keys will be included in the event listener invocation.
      Specified by:
      addClusteredCacheEntryListener in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      listener - the listener to be registered.
      includeValues - defines if the listener should receive the values associated with the events.
      includeEventsFromLocalNode - defines if the listener should be invoked for events that originate on the local node.
      Returns:
      a unique identifier for the listener which is used as a key to remove the listener
    • removeClusteredCacheEntryListener

      public void removeClusteredCacheEntryListener(@Nonnull String listenerId)
      Description copied from interface: Cache
      Removes a previously registered event listener.
      Specified by:
      removeClusteredCacheEntryListener in interface Cache<K extends Serializable,V extends Serializable>
      Parameters:
      listenerId - the identifier of the listener to be removed.