Class CaffeineCache<K extends Serializable,​V extends Serializable>

  • Type Parameters:
    K - Cache key type.
    V - Cache value type.
    All Implemented Interfaces:
    Map<K,​V>, Cache<K,​V>

    public class CaffeineCache<K extends Serializable,​V extends Serializable>
    extends Object
    implements Cache<K,​V>
    Wraps an instance of Ben Manes' Caffeine cache in a class that inherits from Cache. This implementation intends to make Caffeine caches compatible with utility functionality that is based on Cache. Caffeine is a highly configurable implementation. To be able to achieve compatibility with Cache, a specific configuration is required:
    • The cache must have an eviction policy, which must be based on 'weight'. Caffeine does not define a unit for weight. This class assumes that the weight is based on byte size of the cache entry.
    • The cache must be configured to expire entries after write.
    See Also:
    Caffeine
    • Method Detail

      • of

        public static <K extends Serializable,​V extends SerializableCaffeineCache of​(com.github.benmanes.caffeine.cache.Cache<K,​V> cache,
                                                                                             String name)
        Wraps a Caffeine cache in an implementation that's Compatible with Cache.
        Type Parameters:
        K - Cache key type.
        V - Cache value type.
        Parameters:
        cache - The cache to be wrapped (cannot be null).
        name - A human-readable name for the cache that's wrapped.
        Returns:
        A wrapped cache (never null).
        Throws:
        IllegalArgumentException - when the cache configuration does not conform to the contract defined in the javadoc of this class.
      • getMaxCacheSize

        public long getMaxCacheSize()
        Returns the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max 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 in bytes.
      • setMaxCacheSize

        public void setMaxCacheSize​(long maxSize)
        Sets the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.

        Note: If using the Hazelcast clustering plugin, this 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 in bytes.
      • getMaxLifetime

        public long getMaxLifetime()
        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)
        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 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()
        Returns the size of the cache contents in bytes. This value is only a rough approximation, so cache users should expect that actual VM memory used by the cache could be significantly higher than the value reported by this method.
        Specified by:
        getLongCacheSize in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the size of the cache contents in bytes.
      • getCacheHits

        public long getCacheHits()
        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()
        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()
        Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Specified by:
        size in interface Map<K extends Serializable,​V extends Serializable>
        Returns:
        the number of key-value mappings in this map
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key-value mappings.
        Specified by:
        isEmpty in interface Map<K extends Serializable,​V extends Serializable>
        Returns:
        true if this map contains no key-value mappings
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
        Specified by:
        containsKey in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
        Throws:
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • containsValue

        public boolean containsValue​(Object value)
        Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
        Specified by:
        containsValue in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        value - value whose presence in this map is to be tested
        Returns:
        true if this map maps one or more keys to the specified value
        Throws:
        ClassCastException - if the value is of an inappropriate type for this map (optional)
        NullPointerException - if the specified value is null and this map does not permit null values (optional)
      • get

        public V get​(Object key)
        Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

        More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

        If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

        Specified by:
        get in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this map contains no mapping for the key
        Throws:
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • put

        public V put​(K key,
                     V value)
        Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
        Specified by:
        put in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
        Throws:
        UnsupportedOperationException - if the put operation is not supported by this map
        ClassCastException - if the class of the specified key or value prevents it from being stored in this map
        NullPointerException - if the specified key or value is null and this map does not permit null keys or values
        IllegalArgumentException - if some property of the specified key or value prevents it from being stored in this map
      • remove

        public V remove​(Object key)
        Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)

        Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

        If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.

        The map will not contain a mapping for the specified key once the call returns.

        Specified by:
        remove in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        the previous value associated with key, or null if there was no mapping for key.
        Throws:
        UnsupportedOperationException - if the remove operation is not supported by this map
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • putAll

        public void putAll​(Map<? extends K,​? extends V> m)
        Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
        Specified by:
        putAll in interface Map<K extends Serializable,​V extends Serializable>
        Parameters:
        m - mappings to be stored in this map
        Throws:
        UnsupportedOperationException - if the putAll operation is not supported by this map
        ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map
        NullPointerException - if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values
        IllegalArgumentException - if some property of a key or value in the specified map prevents it from being stored in this map
      • clear

        public void clear()
        Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
        Specified by:
        clear in interface Map<K extends Serializable,​V extends Serializable>
        Throws:
        UnsupportedOperationException - if the clear operation is not supported by this map
      • 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.