# Class ModernSearchCache

java.lang.Object
com.university.bookstore.search.ModernSearchCache

All Implemented Interfaces:
  `AutoCloseable`

public class ModernSearchCache extends Object implements AutoCloseable
Modern high-performance cache implementation with advanced features. Features: - Time-based and size-based eviction - Async loading with CompletableFuture - Statistics tracking - Warm-up and refresh capabilities - Thread-safe operations

Since:
  2024-09-15
Version:
  4.0
Author:
  Navid Mohaghegh

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final record`
  `ModernSearchCache.CacheStats`
  Cache statistics record.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ModernSearchCache()`
  Creates a new modern cache with default settings.
  `ModernSearchCache(int maxSize, Duration ttl, Duration idleTime)`
  Creates a new modern cache with custom settings.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `boolean`
  `containsKey(String key)`
  Checks if a key is cached.
  `List<Material>`
  `get(String key, Function<String, List<Material>> loader)`
  Gets a value from the cache or loads it if not present.
  `CompletableFuture<List<Material>>`
  `getAsync(String key, Function<String, CompletableFuture<List<Material>>> asyncLoader)`
  Gets a value from the cache or loads it asynchronously.
  `ModernSearchCache.CacheStats`
  `getStats()`
  Gets cache statistics.
  `boolean`
  `invalidate(String key)`
  Invalidates a cache entry.
  `void`
  `invalidateAll()`
  Invalidates all cache entries.
  `int`
  `invalidateIf(Predicate<String> predicate)`
  Invalidates entries matching a predicate.
  `boolean`
  `isEmpty()`
  Checks if the cache is empty.
  `void`
  `put(String key, List<Material> value)`
  Puts a value in the cache.
  `CompletableFuture<List<Material>>`
  `refresh(String key, Function<String, List<Material>> loader)`
  Refreshes a cache entry asynchronously.
  `void`
  `resetStats()`
  Resets cache statistics.
  `int`
  `size()`
  Gets the current cache size.
  `CompletableFuture<Void>`
  `warmUp(Collection<String> keys, Function<String, List<Material>> loader)`
  Warms up the cache with predefined keys.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ModernSearchCache

    public ModernSearchCache()
    Creates a new modern cache with default settings.

  - ### ModernSearchCache

    public ModernSearchCache(int maxSize, Duration ttl, Duration idleTime)
    Creates a new modern cache with custom settings.
    Parameters:
      `maxSize` - maximum number of entries
      `ttl` - time to live for entries
      `idleTime` - maximum idle time before eviction

- ## Method Details

  - ### get

    public List<Material> get(String key, Function<String, List<Material>> loader)
    Gets a value from the cache or loads it if not present.
    Parameters:
      `key` - the cache key
      `loader` - function to load the value if not cached
    Returns:
      the cached or loaded value

  - ### getAsync

    public CompletableFuture<List<Material>> getAsync(String key, Function<String, CompletableFuture<List<Material>>> asyncLoader)
    Gets a value from the cache or loads it asynchronously.
    Parameters:
      `key` - the cache key
      `asyncLoader` - async function to load the value
    Returns:
      CompletableFuture with the result

  - ### put

    public void put(String key, List<Material> value)
    Puts a value in the cache.
    Parameters:
      `key` - the cache key
      `value` - the value to cache

  - ### invalidate

    public boolean invalidate(String key)
    Invalidates a cache entry.
    Parameters:
      `key` - the key to invalidate
    Returns:
      true if an entry was removed

  - ### invalidateAll

    public void invalidateAll()
    Invalidates all cache entries.

  - ### invalidateIf

    public int invalidateIf(Predicate<String> predicate)
    Invalidates entries matching a predicate.
    Parameters:
      `predicate` - the predicate to test keys
    Returns:
      number of entries invalidated

  - ### refresh

    public CompletableFuture<List<Material>> refresh(String key, Function<String, List<Material>> loader)
    Refreshes a cache entry asynchronously.
    Parameters:
      `key` - the key to refresh
      `loader` - the loader function
    Returns:
      CompletableFuture with the refreshed value

  - ### warmUp

    public CompletableFuture<Void> warmUp(Collection<String> keys, Function<String, List<Material>> loader)
    Warms up the cache with predefined keys.
    Parameters:
      `keys` - keys to warm up
      `loader` - the loader function
    Returns:
      CompletableFuture that completes when warm-up is done

  - ### size

    public int size()
    Gets the current cache size.
    Returns:
      number of cached entries

  - ### isEmpty

    public boolean isEmpty()
    Checks if the cache is empty.
    Returns:
      true if no entries are cached

  - ### containsKey

    public boolean containsKey(String key)
    Checks if a key is cached.
    Parameters:
      `key` - the key to check
    Returns:
      true if the key is cached and not expired

  - ### getStats

    public ModernSearchCache.CacheStats getStats()
    Gets cache statistics.
    Returns:
      current cache statistics

  - ### resetStats

    public void resetStats()
    Resets cache statistics.

  - ### close

    public void close()
    Specified by:
      `close` in interface `AutoCloseable`