Class GuavaCache<K,​V>

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

    public class GuavaCache<K,​V>
    extends Object
    implements Cache<K,​V>
    Implementation of AmetysCache with Guava library
    • Field Detail

      • _size

        protected long _size
        Maximum size of the cache in bytes
      • _computableSize

        protected boolean _computableSize
        True if the size of the cache can be computed, false otherwise (e.g., the cache store a component)
      • _duration

        protected Duration _duration
        The length of time after an entry is created that it should be automatically
      • _isInitialized

        protected boolean _isInitialized
        True if the put or putAll have been called (even without values), false otherwises. become false again when the cache is reseted or if all values have been cleaned.
      • _isDispatchable

        protected boolean _isDispatchable
        True if the cache can be transmitted in sub-requests of DispatchGenerator, false otherwises.
    • Constructor Detail

      • GuavaCache

        public GuavaCache​(String id,
                          I18nizableText label,
                          I18nizableText description,
                          long size,
                          boolean computableSize,
                          Duration duration,
                          boolean isDispatchable)
        Implementation of AmetysCache with Guava library
        Parameters:
        id - id of the cache
        label - label of the cache
        description - description
        size - maximum size of the cache
        computableSize - true if the size of the cache can be computed
        duration - the length of time after an entry is created that it should be automatically removed
        isDispatchable - true if the cache can be transmitted in sub-requests of DispatchGenerator
    • Method Detail

      • resetCache

        public void resetCache()
        Description copied from interface: Cache
        Create or reset the whole cache (including statistics).
        The CACHE_RESET event is not notified by this method, but should be by callers.
        Specified by:
        resetCache in interface Cache<K,​V>
      • get

        public V get​(K key,
                     Function<K,​V> function)
              throws CacheException
        Description copied from interface: Cache
        Returns the value associated with key in this cache if it exist, obtaining that value from given function if necessary and store it.
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - the key to get the element
        function - the function to compute the value if key is not present
        Returns:
        V the value associated with key
        Throws:
        CacheException - throw CacheException if the key is null or invalid, or if failed to compute the new value
      • get

        public V get​(K key)
        Description copied from interface: Cache
        Returns the value associated with key in this cache if it exist
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - the key to get the element
        Returns:
        V the value linked to key
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: Cache
        Associates value with key in this cache. If the cache previously contained a value associated with key, the old value is replaced by value. The key can not be null.
        Specified by:
        put in interface Cache<K,​V>
        Parameters:
        key - the key to get the element
        value - the value associated with key
      • putAll

        public void putAll​(Map<K,​V> map)
        Description copied from interface: Cache
        Copies all of the mappings from the specified map to the cache.
        Specified by:
        putAll in interface Cache<K,​V>
        Parameters:
        map - map of key/values to be stored in the cache
      • getCacheStats

        public CacheStats getCacheStats()
        Description copied from interface: Cache
        Get all statistics about cache
        Specified by:
        getCacheStats in interface Cache<K,​V>
        Returns:
        statistic about the cache (hit, miss, evictions...)
      • getMemorySize

        public long getMemorySize()
                           throws CacheException
        Description copied from interface: Cache
        Get the size the cache take in bytes
        Specified by:
        getMemorySize in interface Cache<K,​V>
        Returns:
        the size of the cache if it's computable, return 0 otherwise
        Throws:
        CacheException - throw CacheException if memory size can not be computed
      • invalidate

        public void invalidate​(K key)
        Description copied from interface: Cache
        Discards any cached value for key key.
        Specified by:
        invalidate in interface Cache<K,​V>
        Parameters:
        key - the key to invalidate
      • invalidateAll

        public void invalidateAll()
        Description copied from interface: Cache
        Discards all entries in the cache, without reseting statistics
        Specified by:
        invalidateAll in interface Cache<K,​V>
      • getId

        public String getId()
        Description copied from interface: Cache
        Get the id of cache
        Specified by:
        getId in interface Cache<K,​V>
        Returns:
        the id of cache
      • getMaxSize

        public long getMaxSize()
        Description copied from interface: Cache
        Get the max size allocated to the cache, in bytes. This value can be set to Long.MAX_VALUE for infinite cache
        Specified by:
        getMaxSize in interface Cache<K,​V>
        Returns:
        the max size allocated to the cache
      • hasKey

        public boolean hasKey​(K key)
        Description copied from interface: Cache
        return true if key is present in cache
        Specified by:
        hasKey in interface Cache<K,​V>
        Parameters:
        key - the key to be checked
        Returns:
        true if key is present in cache
      • asMap

        public Map<K,​VasMap()
        Description copied from interface: Cache
        Get the cache as a simple Map
        Specified by:
        asMap in interface Cache<K,​V>
        Returns:
        the Map<K, V> representing the cache
      • isComputableSize

        public boolean isComputableSize()
        Description copied from interface: Cache
        Is this cache size computable
        Specified by:
        isComputableSize in interface Cache<K,​V>
        Returns:
        true if the cache size is computable
      • isInitialized

        public boolean isInitialized()
        Description copied from interface: Cache
        Is this cache filled at least once.
        Specified by:
        isInitialized in interface Cache<K,​V>
        Returns:
        true if the cache have been filled (put or putAll have been called even without values), false when the cache is new, reseted or if all values have been cleaned
      • isDispatchable

        public boolean isDispatchable()
        Description copied from interface: Cache
        Can the cache be transmitted in sub-requests of DispatchGenerator
        Specified by:
        isDispatchable in interface Cache<K,​V>
        Returns:
        true if the cache can be transmitted in sub-requests of DispatchGenerator
      • getAll

        public Map<K,​VgetAll​(AbstractCacheKey filterKey)
        Description copied from interface: Cache
        Get the cache as a simple Map, filtered by filled values of the cache key
        Specified by:
        getAll in interface Cache<K,​V>
        Parameters:
        filterKey - the key to get the element.
        Returns:
        the Map<K, V> representing the cache