Class GuavaCache<K,V>

java.lang.Object
org.ametys.plugins.core.impl.cache.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 Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Cache<K,Optional<V>>
    the cache itself, using Guava implementation
    protected boolean
    True if the size of the cache can be computed, false otherwise (e.g., the cache store a component)
    protected I18nizableText
    Description of the cache
    protected Duration
    The length of time after an entry is created that it should be automatically
    protected String
    Id of the cache
    protected boolean
    True if the cache can be transmitted in sub-requests of DispatchGenerator, false otherwises.
    protected boolean
    True if the put or putAll have been called (even without values), false otherwises.
    protected I18nizableText
    Label of the cache
    protected long
    Maximum size of the cache in bytes
  • Constructor Summary

    Constructors
    Constructor
    Description
    GuavaCache(String id, I18nizableText label, I18nizableText description, long size, boolean computableSize, Duration duration, boolean isDispatchable)
    Implementation of AmetysCache with Guava library
  • Method Summary

    Modifier and Type
    Method
    Description
    Get the cache as a simple Map
    get(K key)
    Returns the value associated with key in this cache if it exist
    get(K key, Function<K,V> function)
    Returns the value associated with key in this cache if it exist, obtaining that value from given function if necessary and store it.
    Get the cache as a simple Map, filtered by filled values of the cache key
    Get all statistics about cache
    Get the description of cache
    Get the id of cache
    Get the label of cache
    long
    Get the max size allocated to the cache, in bytes.
    long
    Get the size the cache take in bytes
    long
    Get the number of values in cache
    boolean
    hasKey(K key)
    return true if key is present in cache
    void
    Discards any cached value for key key.
    void
    Discards all entries in the cache, without reseting statistics
    boolean
    Is this cache size computable
    boolean
    Can the cache be transmitted in sub-requests of DispatchGenerator
    boolean
    Is this cache filled at least once.
    void
    put(K key, V value)
    Associates value with key in this cache.
    void
    putAll(Map<K,V> map)
    Copies all of the mappings from the specified map to the cache.
    void
    Create or reset the whole cache (including statistics).
    The CACHE_RESET event is not notified by this method, but should be by callers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.ametys.core.cache.Cache

    toJSONMap
  • Field Details

    • _cache

      protected Cache<K,Optional<V>> _cache
      the cache itself, using Guava implementation
    • _id

      protected String _id
      Id of the cache
    • _label

      Label of the cache
    • _description

      Description of the cache
    • _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 Details

    • 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 Details

    • 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

      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
    • getNumberOfElements

      public long getNumberOfElements()
      Description copied from interface: Cache
      Get the number of values in cache
      Specified by:
      getNumberOfElements in interface Cache<K,V>
      Returns:
      the number of values in cache
    • 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>
    • getDescription

      Description copied from interface: Cache
      Get the description of cache
      Specified by:
      getDescription in interface Cache<K,V>
      Returns:
      the description of cache
    • getLabel

      Description copied from interface: Cache
      Get the label of cache
      Specified by:
      getLabel in interface Cache<K,V>
      Returns:
      the label of cache
    • 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,V> asMap()
      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,V> getAll(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