Class ReadThroughCache<K,V>

java.lang.Object
com.loomcache.client.cache.ReadThroughCache<K,V>
Type Parameters:
K - key type
V - value type

public class ReadThroughCache<K,V> extends Object
Read-through cache that implements the cache-aside pattern.

When a cache miss occurs, the CacheLoader is called to fetch the value. Multiple concurrent requests for the same key are coalesced — only one loader call is made, and all threads block on the same CompletableFuture result.

  • Constructor Details

    • ReadThroughCache

      public ReadThroughCache(NearCache<K,V> nearCache, CacheLoader<K,V> loader)
      Create a new read-through cache.
      Parameters:
      nearCache - the underlying near cache (must not be null)
      loader - the cache loader for cache misses (must not be null)
      Throws:
      NullPointerException - if any parameter is null
  • Method Details

    • get

      public V get(K key) throws Exception
      Get a value from the cache, loading it if necessary.

      If multiple threads request the same missing key simultaneously, only one will call the loader; others will wait on the same future.

      Parameters:
      key - the key to retrieve
      Returns:
      the cached value
      Throws:
      Exception - if the loader fails
    • getAll

      public Map<K,V> getAll(Collection<K> keys) throws Exception
      Get multiple values, loading any missing ones efficiently.
      Parameters:
      keys - the keys to retrieve
      Returns:
      a map of keys to values (may be partial if some keys are not found)
      Throws:
      Exception - if any loader call fails
    • refresh

      public V refresh(K key) throws Exception
      Refresh a specific key by forcing a reload from the source.
      Parameters:
      key - the key to refresh
      Returns:
      the refreshed value
      Throws:
      Exception - if the loader fails
    • refreshAll

      public void refreshAll()
      Refresh all entries by clearing the near cache.
    • getStats

      public NearCache.NearCacheStats getStats()
      Get underlying near cache statistics.