Interface SimpleCache

All Known Subinterfaces:
Cache
All Known Implementing Classes:
AbstractSimpleCache, ExternalCache, InMemoryCache

public interface SimpleCache
A cache is a storage mechanism to store intermediate data got from any subsystems or from external services in order to retrieve them fastly and hence more efficiently. A cache is dedicated to boost performance and to simplify the scalability.

This interface defines a simple cache that only provides usual methods expected for a cache.

Since:
11/09/13
Author:
Yohann Chastagnier
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Object value)
    Adds a value and generate a unique key to retrieve later the value.
    void
    Clear the content of the cache.
    <T> T
    computeIfAbsent(Object key, Class<T> classType, Supplier<T> valueSupplier)
    Gets a typed element mapped with the specified key from the cache or computes it with the specified value supplier if no such key exists.
    get(Object key)
    Gets an element from the cache.
    <T> T
    get(Object key, Class<T> classType)
    Gets a typed element from the cache.
    default boolean
    has(Object key)
    Is this cache contains an object mapped with the specified key?
    void
    put(Object key, Object value)
    Puts a value for a given key.
    Removes an element from the cache and return it.
    <T> T
    remove(Object key, Class<T> classType)
    Removes a typed element from the cache and return it.
  • Method Details

    • clear

      void clear()
      Clear the content of the cache.
    • get

      Gets an element from the cache.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      Returns:
      the object mapped with the key or null if no there is no object mapped with the specified key.
    • has

      default boolean has(Object key)
      Is this cache contains an object mapped with the specified key? It uses the get(Object) method for doing.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      Returns:
      true if there is an mapped with the key. False otherwise.
      See Also:
    • get

      @Nullable <T> T get(Object key, Class<T> classType)
      Gets a typed element from the cache. Null is returned if an element exists for the given key but the object doesn't satisfy the expected type.
      Type Parameters:
      T - the concrete type of the object to get.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      classType - the class type the instance to get as to satisfy.
      Returns:
      the object mapped with the key or null if either there is no object mapped with the specified key or the object doesn't satisfy the expected class type.
    • computeIfAbsent

      @Nullable <T> T computeIfAbsent(Object key, Class<T> classType, Supplier<T> valueSupplier)
      Gets a typed element mapped with the specified key from the cache or computes it with the specified value supplier if no such key exists. If an element exists for the given key but the object type doesn't match the expected one, a new computation is performed. If the element returned by the computation is null, then nothing is put into the cache. The value supplier should not modify this cache during computation.
      Type Parameters:
      T - the concrete type of the object to get.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      classType - the class of the instance to get.
      valueSupplier - the function that will computes the data to put into the cache.
      Returns:
      the object mapped with the key or null if there is no object to map with the specified key.
    • remove

      Object remove(Object key)
      Removes an element from the cache and return it.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      Returns:
      the object removed from the cache.
    • remove

      <T> T remove(Object key, Class<T> classType)
      Removes a typed element from the cache and return it. Null is returned if an element exists for the given key but the object type doesn't correspond.
      Type Parameters:
      T - the concrete type of the object to remove.
      Parameters:
      key - the key with which the object to get is mapped in the cache.
      classType - the class of the instance to remove.
      Returns:
      the object removed from the cache.
    • add

      String add(Object value)
      Adds a value and generate a unique key to retrieve later the value. After 12 hours without be used the value is trashed.
      Parameters:
      value - the object to add in the cache.
      Returns:
      the key with which the added object is mapped in the cache.
    • put

      void put(Object key, Object value)
      Puts a value for a given key. After 12 hours without be used the value is trashed.
      Parameters:
      key - the key with which the object to put has to be mapped
      value - the object to put in the cache.