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

java.lang.Object
se.liu.ida.hefquin.federation.access.impl.cache.CacheLayer<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 internally
All Implemented Interfaces:
AutoCloseable, Cache<IdType,ObjectType>
Direct Known Subclasses:
ChronicleMapCache, MapDBCache

public class CacheLayer<IdType,ObjectType,EntryType extends CacheEntry<ObjectType>> extends Object implements Cache<IdType,ObjectType>
Generic implementation of a cache layer.

A cache layer stores objects identified by keys and delegates cache management behavior to configurable policies.

This class is designed to support thread-safe cache operations by synchronizing access to the underlying cache map. As a result, updates to the cache contents and interactions with the configured policies are performed atomically with respect to other cache operations.

Thread safety depends on subclasses providing a map instance that is used consistently as the synchronization monitor and on the configured policies behaving correctly when invoked under this synchronization scheme.

  • Field Details

  • Constructor Details

    • CacheLayer

      public CacheLayer(Map<IdType,EntryType> map, int capacity, CachePolicies<IdType,ObjectType,EntryType> policies)
      Creates a cache layer with the specified capacity and cache policies.

      The provided policies define how cache entries are created, invalidated, and selected for eviction. The cache capacity represents the maximum number of entries that may be stored simultaneously.

      Parameters:
      map - backing map used to store cache entries and synchronize cache operations
      capacity - maximum number of entries that can be stored
      policies - cache policies used by this cache layer
  • Method Details

    • put

      public void put(IdType key, ObjectType value)
      Inserts or updates a cache entry.

      If the key is not present, a new entry is added and the replacement policy is notified via entryWasAdded. If the key already exists, its value is replaced and the replacement policy is notified via entryWasRewritten.

      After insertion, the cache capacity is enforced. If the number of entries exceeds the configured capacity, one or more entries are evicted according to the replacement policy.

      Specified by:
      put in interface Cache<IdType,ObjectType>
      Parameters:
      key - cache key, must not be null
      value - object to cache, must not be null
      Throws:
      IllegalArgumentException - if key or value is null
    • get

      public final ObjectType get(IdType key)
      Retrieves the cached object associated with the given key.

      If the key is present, the corresponding entry is first validated using the configured invalidation policy. Invalid entries are automatically evicted and treated as cache misses.

      Successful lookups notify the replacement policy via entryWasRequested, allowing policies such as LRU to update their internal state.

      Specified by:
      get in interface Cache<IdType,ObjectType>
      Parameters:
      key - cache key
      Returns:
      the cached object if a valid entry exists, otherwise null
    • evict

      public boolean evict(IdType key)
      Removes the cache entry associated with the given key.

      If an entry is removed, the replacement policy is notified via entryWasEvicted so that any policy-specific state can be updated.

      Specified by:
      evict in interface Cache<IdType,ObjectType>
      Parameters:
      key - key of the entry to remove
      Returns:
      true if an entry was present and removed, false otherwise
    • evict

      public boolean evict(IdType key, ObjectType value)
      Removes the cache entry associated with the given key if the cached object equals the provided value.

      If an entry is removed, the replacement policy is notified via entryWasEvicted so that any policy-specific state can be updated.

      Specified by:
      evict in interface Cache<IdType,ObjectType>
      Parameters:
      key - key of the entry to remove
      value - expected value associated with the key
      Returns:
      true if an entry was present and removed, false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns whether the cache currently contains any entries.
      Specified by:
      isEmpty in interface Cache<IdType,ObjectType>
      Returns:
      true if the cache contains no entries, false otherwise
    • clear

      public void clear()
      Removes all cache entries and resets replacement policy state.

      After this method returns, the cache contains no entries and the replacement policy behaves as if it were newly created.

      Specified by:
      clear in interface Cache<IdType,ObjectType>