Class CacheImpl<K,​V>

    • Method Detail

      • get

        public V get​(K key)
        Description copied from interface: Cache
        Gets an entry from the cache.

        If the cache is configured read-through, and get would return null because the entry is missing from the cache, the Cache's CacheLoader is called which will attempt to load the entry.

        Effects:

        • Expiry - updates Access Time.
        • Read-Through - will use the CacheLoader if enabled and key not present in cache
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the element, or null, if it does not exist.
        See Also:
        Map.get(Object)
      • getAll

        public Map<K,​V> getAll​(Set<? extends K> keys)
        Description copied from interface: Cache
        The getAll method will return, from the cache, a Map of the objects associated with the Collection of keys in argument "keys".

        If the cache is configured read-through, and a get would return null because an entry is missing from the cache, the Cache's CacheLoader is called which will attempt to load the entry. This is done for each key in the collection for which this is the case. If an entry cannot be loaded for a given key, the key will not be present in the returned Map.

        Specified by:
        getAll in interface Cache<K,​V>
        Parameters:
        keys - The keys whose associated values are to be returned.
        Returns:
        A map of entries that were found for the given keys. Keys not found in the cache are not in the returned map.
      • syncCaches

        public void syncCaches()
        Deprecated.
        This method is highly inefficient. Do not use.
      • containsKey

        public boolean containsKey​(K key)
        Description copied from interface: Cache
        Returns true if this cache contains a mapping for the specified key. More formally, returns true if and only if this cache contains a mapping for a key k such that key.equals(k). (There can be at most one such mapping.)

        Specified by:
        containsKey in interface Cache<K,​V>
        Parameters:
        key - key whose presence in this cache is to be tested.
        Returns:
        true if this map contains a mapping for the specified key
        See Also:
        Map.containsKey(Object)
      • load

        public Future<V> load​(K key)
        Description copied from interface: Cache
        The load method provides a means to "pre-load" the cache. This method will, asynchronously, load the specified object into the cache using the associated CacheLoader for the given key.

        If the object already exists in the cache, no action is taken and null is returned. If no loader is associated with the cache no object will be loaded into the cache and null is returned.

        If a problem is encountered during the retrieving or loading of the object, an exception must be propagated on Future.get() as a ExecutionException

        Specified by:
        load in interface Cache<K,​V>
        Parameters:
        key - the key
        Returns:
        a Future which can be used to monitor execution.
      • loadAll

        public Future<Map<K,​? extends V>> loadAll​(Set<? extends K> keys)
        Description copied from interface: Cache
        The loadAll method provides a means to "pre-load" objects into the cache. This method will, asynchronously, load the specified objects into the cache using the associated cache loader for the given keys.

        If the an object already exists in the cache, no action is taken. If no loader is associated with the object, no object will be loaded into the cache. If a problem is encountered during the retrieving or loading of the objects, an exception (to be defined) should be logged.

        If a problem is encountered during the retrieving or loading of the object, an exception must be propagated on Future.get() as a ExecutionException

        Specified by:
        loadAll in interface Cache<K,​V>
        Parameters:
        keys - the keys
        Returns:
        a Future which can be used to monitor execution
      • getStatistics

        public CacheStatistics getStatistics()
        Description copied from interface: Cache
        Returns the CacheStatistics MXBean associated with the cache. May return null if the cache does not support statistics gathering.
        Specified by:
        getStatistics in interface Cache<K,​V>
        Returns:
        the CacheStatisticsMBean
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: Cache
        Associates the specified value with the specified key in this cache If the cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only if c.containsKey(k) would return true.)

        In contrast to the corresponding Map operation, does not return the previous value.

        Specified by:
        put in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        See Also:
        Map.put(Object, Object), Cache.getAndPut(Object, Object), Cache.getAndReplace(Object, Object)
      • getAndPut

        public V getAndPut​(K key,
                           V value)
        Description copied from interface: Cache
        Associates the specified value with the specified key in this cache, returning an existing value if one existed.

        If the cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only if c.containsKey(k) would return true.)

        The the previous value is returned, or null if there was no value associated with the key previously.

        Specified by:
        getAndPut in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the value associated with the key at the start of the operation or null if none was associated
        See Also:
        Map.put(Object, Object), Cache.put(Object, Object), Cache.getAndReplace(Object, Object)
      • putAll

        public void putAll​(Map<? extends K,​? extends V> map)
        Description copied from interface: Cache
        Copies all of the mappings from the specified map to this cache. The effect of this call is equivalent to that of calling put(k, v) on this cache once for each mapping from key k to value v in the specified map. The order in which the individual puts will occur is undefined. The behavior of this operation is undefined if the specified cache or map is modified while the operation is in progress.
        Specified by:
        putAll in interface Cache<K,​V>
        Parameters:
        map - mappings to be stored in this cache
        See Also:
        Map.putAll(java.util.Map)
      • putIfAbsent

        public boolean putIfAbsent​(K key,
                                   V value)
        Description copied from interface: Cache
        Atomically associates the specified key with the given value if it is not already associated with a value.

        This is equivalent to

           if (!cache.containsKey(key)) {}
               cache.put(key, value);
               return true;
           } else {
               return false;
           }
        except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value.
        Specified by:
        putIfAbsent in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        true if a value was set.
        See Also:
        ConcurrentMap.putIfAbsent(Object, Object)
      • remove

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

        Returns true if this cache previously associated the key, or false if the cache contained no mapping for the key.

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

        Specified by:
        remove in interface Cache<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the cache
        Returns:
        returns false if there was no matching key
        See Also:
        Map.remove(Object)
      • removeLocal

        public boolean removeLocal​(Object key)
        This method is added to only remove the cache locally. This is required since remove(Object) method notifies the other nodes in a cluster in addition to removing the local cache.
      • remove

        public boolean remove​(K key,
                              V oldValue)
        Description copied from interface: Cache
        Atomically removes the mapping for a key only if currently mapped to the given value.

        This is equivalent to

           if (cache.containsKey(key) && cache.get(key).equals(oldValue)) {
               cache.remove(key);
               return true;
           } else {
               return false;
           }
        except that the action is performed atomically.
        Specified by:
        remove in interface Cache<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the cache
        oldValue - value expected to be associated with the specified key
        Returns:
        returns false if there was no matching key
        See Also:
        Map.remove(Object)
      • getAndRemove

        public V getAndRemove​(K key)
        Description copied from interface: Cache
        Atomically removes the entry for a key only if currently mapped to a given value.

        This is equivalent to

           if (cache.containsKey(key)) {
               V oldValue = cache.get(key);
               cache.remove(key);
               return oldValue;
           } else {
               return null;
           }
        except that the action is performed atomically.
        Specified by:
        getAndRemove in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is associated
        Returns:
        the value if one existed or null if no mapping existed for this key
        See Also:
        Map.remove(Object)
      • replace

        public boolean replace​(K key,
                               V oldValue,
                               V newValue)
        Description copied from interface: Cache
        Atomically replaces the entry for a key only if currently mapped to a given value.

        This is equivalent to

           if (cache.containsKey(key) && cache.get(key).equals(oldValue)) {
               cache.put(key, newValue);
               return true;
           } else {
               return false;
           }
        except that the action is performed atomically.
        Specified by:
        replace in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is associated
        oldValue - value expected to be associated with the specified key
        newValue - value to be associated with the specified key
        Returns:
        true if the value was replaced
        See Also:
        ConcurrentMap.replace(Object, Object, Object)
      • replace

        public boolean replace​(K key,
                               V value)
        Description copied from interface: Cache
        Atomically replaces the entry for a key only if currently mapped to some value.

        This is equivalent to

           if (cache.containsKey(key)) {
               cache.put(key, value);
               return true;
           } else {
               return false;
           }
        except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value.
        Specified by:
        replace in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is associated
        value - value to be associated with the specified key
        Returns:
        true if the value was replaced
        See Also:
        Cache.getAndReplace(Object, Object), ConcurrentMap.replace(Object, Object)
      • getAndReplace

        public V getAndReplace​(K key,
                               V value)
        Description copied from interface: Cache
        Atomically replaces the entry for a key only if currently mapped to some value.

        This is equivalent to

           if (cache.containsKey(key)) {
               V value = cache.get(key, value);
               cache.put(key, value);
               return value;
           } else {
               return null;
           }
        except that the action is performed atomically.
        Specified by:
        getAndReplace in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key.
        See Also:
        ConcurrentMap.replace(Object, Object)
      • removeAll

        public void removeAll​(Set<? extends K> keys)
        Description copied from interface: Cache
        Removes entries for the specified keys.

        The order in which the individual removes will occur is undefined.

        Specified by:
        removeAll in interface Cache<K,​V>
        Parameters:
        keys - the keys to remove
      • removeAll

        public void removeAll()
        Description copied from interface: Cache
        Removes all of the mappings from this cache.

        The order in which the individual removes will occur is undefined. This is potentially an expensive operation as listeners are invoked. Use #clear() to avoid this.

        Specified by:
        removeAll in interface Cache<K,​V>
        See Also:
        #clear()
      • removeAllLocal

        public void removeAllLocal()
        This method is added to only remove the cache locally. This is required since removeAll() method notifies the other nodes in a cluster in addition to removing the local cache.
      • registerCacheEntryListener

        public boolean registerCacheEntryListener​(CacheEntryListener<? super K,​? super V> cacheEntryListener)
        Description copied from interface: Cache
        Adds a listener to the notification service.

        Specified by:
        registerCacheEntryListener in interface Cache<K,​V>
        Parameters:
        cacheEntryListener - The listener to add. Listeners fire synchronously in the execution path, and after the causing event. if a listener throws an exception it will be wrapped in a CacheException and propagated back to the caller.
        Returns:
        true if the listener is being added and was not already added
      • unregisterCacheEntryListener

        public boolean unregisterCacheEntryListener​(CacheEntryListener<?,​?> cacheEntryListener)
        Description copied from interface: Cache
        Removes a call back listener.
        Specified by:
        unregisterCacheEntryListener in interface Cache<K,​V>
        Parameters:
        cacheEntryListener - the listener to remove
        Returns:
        true if the listener was present
      • invokeEntryProcessor

        public Object invokeEntryProcessor​(K key,
                                           Cache.EntryProcessor<K,​V> entryProcessor)
        Description copied from interface: Cache
        Passes the cache entry associated with K to be passed to the entry processor. All operations performed by the processor will be done atomically i.e. all The processor will perform the operations against
        Specified by:
        invokeEntryProcessor in interface Cache<K,​V>
        Parameters:
        key - the key to the entry
        entryProcessor - the processor which will process the entry
        Returns:
        an object
        See Also:
        Cache.EntryProcessor
      • getName

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

        public CacheManager getCacheManager()
        Description copied from interface: Cache
        Gets the CacheManager managing this cache.

        A cache can be in only one CacheManager.

        Specified by:
        getCacheManager in interface Cache<K,​V>
        Returns:
        the manager
      • unwrap

        public <T> T unwrap​(Class<T> cls)
        Description copied from interface: Cache
        Return an object of the specified type to allow access to the provider-specific API. If the provider's implementation does not support the specified class, the IllegalArgumentException is thrown.
        Specified by:
        unwrap in interface Cache<K,​V>
        Parameters:
        cls - he class of the object to be returned. This is normally either the underlying implementation class or an interface that it implements.
        Returns:
        an instance of the specified class
      • getMBean

        public CacheMXBean getMBean()
        Description copied from interface: Cache
        Get the MBean for this cache.
        Specified by:
        getMBean in interface Cache<K,​V>
        Returns:
        the MBean TODO: not sure this belongs here
      • start

        public void start()
        Description copied from interface: CacheLifecycle
        Notifies providers to start themselves.

        This method is called during the resource's start method after it has changed its status to alive. Cache operations are legal in this method.

        At the completion of this method invocation CacheLifecycle.getStatus() must return Status.STARTED.

        Specified by:
        start in interface CacheLifecycle
      • stop

        public void stop()
        Description copied from interface: CacheLifecycle
        Providers may be doing all sorts of exotic things and need to be able to clean up on stop.

        Cache operations are illegal after this method is called. A IllegalStateException will be

        Resources will change status to Status.STOPPED when this method completes.

        Stop must free any JVM resources used.

        Specified by:
        stop in interface CacheLifecycle
      • expire

        public void expire​(K key)
      • evict

        public void evict​(K key)
      • setCapacity

        public void setCapacity​(long capacity)
      • setEvictionAlgorithm

        public void setEvictionAlgorithm​(EvictionAlgorithm evictionAlgorithm)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object