Interface CacheService<K,V>

Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values
All Known Implementing Classes:
BaseCacheService

public interface CacheService<K,V>
CacheService defines a contract for managing cache operations such as storing, retrieving, and removing cache entries. Implementations should handle the persistence and retrieval of cached data, including support for optional time-to-live (TTL).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all entries from the cache.
    void
    evict(K key)
    Removes the cache entry associated with the specified key.
    get(K key)
    Retrieves the value associated with the specified key from the cache, if present.
    default String
    Generates a formatted name for the class by splitting its simple name at camel case boundaries and joining the segments with underscores, then converting the resulting string to lowercase.
    void
    put(K key, V value)
    Stores a key-value pair in the cache.
    void
    put(K key, V value, Duration ttl)
    Stores a key-value pair in the cache with an optional time-to-live (TTL) duration.
  • Method Details

    • get

      Optional<V> get(K key)
      Retrieves the value associated with the specified key from the cache, if present.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      an Optional containing the value associated with the specified key, or an empty Optional if the key is not present in the cache
    • put

      void put(K key, V value)
      Stores a key-value pair in the cache. If the key already exists, the associated value is replaced with the new value.
      Parameters:
      key - the key with which the specified value is to be associated; must not be null
      value - the value to be associated with the specified key; may be null
    • put

      void put(K key, V value, Duration ttl)
      Stores a key-value pair in the cache with an optional time-to-live (TTL) duration. If the key already exists, the associated value is replaced with the new value, and the TTL is updated. The TTL specifies the duration for which the entry remains in the cache.
      Parameters:
      key - the key with which the specified value is to be associated; must not be null
      value - the value to be associated with the specified key; may be null
      ttl - the time-to-live duration for the cache entry; must not be null
    • evict

      void evict(K key)
      Removes the cache entry associated with the specified key. If the key does not exist in the cache, no action is taken.
      Parameters:
      key - the key of the cache entry to be removed; must not be null
    • clear

      void clear()
      Removes all entries from the cache. After this operation, the cache will be empty.
    • getName

      default String getName()
      Generates a formatted name for the class by splitting its simple name at camel case boundaries and joining the segments with underscores, then converting the resulting string to lowercase.
      Returns:
      the formatted name of the class