Class HierarchicalCache<IdType,ObjectType,EntryType extends CacheEntry<ObjectType>>

java.lang.Object
se.liu.ida.hefquin.federation.access.impl.cache.HierarchicalCache<IdType,ObjectType,EntryType>
Type Parameters:
IdType - key type used to identify cached objects
ObjectType - type of objects stored in the cache
EntryType - cache entry type used by underlying cache layers
All Implemented Interfaces:
AutoCloseable, Cache<IdType,ObjectType>

public class HierarchicalCache<IdType,ObjectType,EntryType extends CacheEntry<ObjectType>> extends Object implements Cache<IdType,ObjectType>
A cache composed of two cache layers arranged hierarchically.

Entries are written to both layers. Lookup operations first consult the primary cache layer (L1). If no entry is found, the secondary cache layer (L2) is consulted. Entries retrieved from L2 are promoted to L1 to improve access performance for subsequent requests.

Eviction and clear operations are applied to both cache layers.

All operations are synchronized on an internal lock, ensuring that interactions between the two cache layers are performed atomically with respect to other operations on the same HierarchicalCache instance.

  • Field Details

  • Constructor Details

    • HierarchicalCache

      public HierarchicalCache(Cache<IdType,ObjectType> l1, Cache<IdType,ObjectType> l2)
      Creates a hierarchical cache consisting of a primary and a secondary cache layer.
      Parameters:
      l1 - primary cache layer consulted first during lookups
      l2 - secondary cache layer consulted when entries are not found in the primary cache layer
  • Method Details

    • put

      public void put(IdType key, ObjectType value)
      Stores the given key-value pair in both cache layers.

      Existing entries associated with the key are replaced according to the behavior of the underlying cache layers.

      Specified by:
      put in interface Cache<IdType,ObjectType>
    • get

      public ObjectType get(IdType key)
      Retrieves the value associated with the given key.

      The primary cache layer is consulted first. If no entry is found, the secondary cache layer is consulted. Entries retrieved from the secondary cache layer are promoted to the primary cache layer before being returned.

      Specified by:
      get in interface Cache<IdType,ObjectType>
      Returns:
      the cached value, or null if no entry exists
    • evict

      public boolean evict(IdType key)
      Removes the entry associated with the given key from both cache layers.
      Specified by:
      evict in interface Cache<IdType,ObjectType>
      Returns:
      true if at least one cache layer removed an entry
    • evict

      public boolean evict(IdType key, ObjectType value)
      Removes entries associated with the given key if they currently map to the specified value.

      The operation is applied independently to each cache layer and behaves as if evict(key, value) were invoked directly on each layer. Consequently, an entry may be removed from some layers but not others if the layers use different representations or equality semantics for cached values.

      Specified by:
      evict in interface Cache<IdType,ObjectType>
      Returns:
      true if at least one cache layer removed an entry
    • isEmpty

      public boolean isEmpty()
      Returns true if both cache layers are empty.
      Specified by:
      isEmpty in interface Cache<IdType,ObjectType>
      Returns:
      true if neither cache layer contains any entries
    • clear

      public void clear()
      Removes all entries from both cache layers.
      Specified by:
      clear in interface Cache<IdType,ObjectType>
    • close

      public void close()
      Releases all resources associated with both cache layers.

      The close operation is propagated to the primary and secondary cache layers. After this method returns, the cache should not be used anymore.

      If a cache layer does not manage any external resources, its implementation of close() may perform no action.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Cache<IdType,ObjectType>