Class ModernSearchCache
java.lang.Object
com.university.bookstore.search.ModernSearchCache
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionstatic final recordCache statistics record. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptionvoidclose()booleancontainsKey(String key) Checks if a key is cached.Gets a value from the cache or loads it if not present.Gets a value from the cache or loads it asynchronously.getStats()Gets cache statistics.booleaninvalidate(String key) Invalidates a cache entry.voidInvalidates all cache entries.intinvalidateIf(Predicate<String> predicate) Invalidates entries matching a predicate.booleanisEmpty()Checks if the cache is empty.voidPuts a value in the cache.Refreshes a cache entry asynchronously.voidResets cache statistics.intsize()Gets the current cache size.Warms up the cache with predefined keys.
-
Constructor Details
-
ModernSearchCache
public ModernSearchCache()Creates a new modern cache with default settings. -
ModernSearchCache
-
-
Method Details
-
get
-
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 keyasyncLoader- async function to load the value- Returns:
- CompletableFuture with the result
-
put
-
invalidate
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
-
refresh
-
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 uploader- 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
Checks if a key is cached.- Parameters:
key- the key to check- Returns:
- true if the key is cached and not expired
-
getStats
Gets cache statistics.- Returns:
- current cache statistics
-
resetStats
public void resetStats()Resets cache statistics. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-