Interface Cache<K,​V>

  • Type Parameters:
    K - the type of the keys in cache
    V - the type of the values in cache
    All Known Implementing Classes:
    GuavaCache

    public interface Cache<K,​V>
    A cache represents a store of objects indexed with a key
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      Map<K,​V> asMap()
      Get the cache as a simple Map
      V get​(K key)
      Returns the value associated with key in this cache if it exist
      V 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.
      Map<K,​V> getAll​(AbstractCacheKey key)
      Get the cache as a simple Map, filtered by filled values of the cache key
      CacheStats getCacheStats()
      Get all statistics about cache
      I18nizableText getDescription()
      Get the description of cache
      String getId()
      Get the id of cache
      I18nizableText getLabel()
      Get the label of cache
      long getMaxSize()
      Get the max size allocated to the cache, in bytes.
      long getMemorySize()
      Get the size the cache take in bytes
      long getNumberOfElements()
      Get the number of values in cache
      boolean hasKey​(K key)
      return true if key is present in cache
      void invalidate​(K key)
      Discards any cached value for key key.
      void invalidateAll()
      Discards all entries in the cache, without reseting statistics
      boolean isComputableSize()
      Is this cache size computable
      boolean isDispatchable()
      Can the cache be transmitted in sub-requests of DispatchGenerator
      boolean isInitialized()
      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 resetCache()
      Create or reset the whole cache (including statistics).
      The CACHE_RESET event is not notified by this method, but should be by callers.
      default Map<String,​Object> toJSONMap​(org.slf4j.Logger logger)
      Get the definition in JSON Map
    • Method Detail

      • get

        V get​(K key,
              Function<K,​V> function)
        throws CacheException
        Returns the value associated with key in this cache if it exist, obtaining that value from given function if necessary and store it.
        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

        V get​(K key)
        Returns the value associated with key in this cache if it exist
        Parameters:
        key - the key to get the element
        Returns:
        V the value linked to key
      • getAll

        Map<K,​VgetAll​(AbstractCacheKey key)
        Get the cache as a simple Map, filtered by filled values of the cache key
        Parameters:
        key - the key to get the element.
        Returns:
        the Map<K, V> representing the cache
      • put

        void put​(K key,
                 V value)
        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.
        Parameters:
        key - the key to get the element
        value - the value associated with key
      • putAll

        void putAll​(Map<K,​V> map)
        Copies all of the mappings from the specified map to the cache.
        Parameters:
        map - map of key/values to be stored in the cache
      • hasKey

        boolean hasKey​(K key)
        return true if key is present in cache
        Parameters:
        key - the key to be checked
        Returns:
        true if key is present in cache
      • invalidate

        void invalidate​(K key)
        Discards any cached value for key key.
        Parameters:
        key - the key to invalidate
      • invalidateAll

        void invalidateAll()
        Discards all entries in the cache, without reseting statistics
      • resetCache

        void resetCache()
        Create or reset the whole cache (including statistics).
        The CACHE_RESET event is not notified by this method, but should be by callers.
      • getCacheStats

        CacheStats getCacheStats()
        Get all statistics about cache
        Returns:
        statistic about the cache (hit, miss, evictions...)
      • getMemorySize

        long getMemorySize()
                    throws CacheException
        Get the size the cache take in bytes
        Returns:
        the size of the cache if it's computable, return 0 otherwise
        Throws:
        CacheException - throw CacheException if memory size can not be computed
      • getMaxSize

        long getMaxSize()
        Get the max size allocated to the cache, in bytes. This value can be set to Long.MAX_VALUE for infinite cache
        Returns:
        the max size allocated to the cache
      • getNumberOfElements

        long getNumberOfElements()
        Get the number of values in cache
        Returns:
        the number of values in cache
      • getId

        String getId()
        Get the id of cache
        Returns:
        the id of cache
      • isComputableSize

        boolean isComputableSize()
        Is this cache size computable
        Returns:
        true if the cache size is computable
      • asMap

        Map<K,​VasMap()
        Get the cache as a simple Map
        Returns:
        the Map<K, V> representing the cache
      • toJSONMap

        default Map<String,​ObjecttoJSONMap​(org.slf4j.Logger logger)
        Get the definition in JSON Map
        Parameters:
        logger - Logger to log an error when the size cannot be computed
        Returns:
        the JSON for the cache
      • isInitialized

        boolean isInitialized()
        Is this cache filled at least once.
        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

        boolean isDispatchable()
        Can the cache be transmitted in sub-requests of DispatchGenerator
        Returns:
        true if the cache can be transmitted in sub-requests of DispatchGenerator