Class ClientSideCaching<K,​V>

  • Type Parameters:
    K - Key type.
    V - Value type.
    All Implemented Interfaces:
    CacheFrontend<K,​V>, Closeable, AutoCloseable

    public class ClientSideCaching<K,​V>
    extends Object
    implements CacheFrontend<K,​V>
    Utility to provide server-side assistance for client-side caches. This is a CacheFrontend that represents a two-level cache backed by a client-side and a Redis cache. For example:
    
     Map clientCache = new ConcurrentHashMap<>();
    
     StatefulRedisConnection<String, String> connection = redisClient.connect();
    
     CacheFrontend<String, String> frontend = ClientSideCaching.enable(CacheAccessor.forMap(clientCache), connection,
             TrackingArgs.Builder.enabled());
    
     String value = frontend.get(key);
     
    Since:
    6.0
    Author:
    Mark Paluch
    • Method Detail

      • create

        public static <K,​V> CacheFrontend<K,​V> create​(CacheAccessor<K,​V> cacheAccessor,
                                                                  StatefulRedisConnection<K,​V> connection)
        Create a server-assisted Client side caching for the given CacheAccessor and StatefulRedisConnection. This method expects that client key tracking is already configured.

        Note that the CacheFrontend is associated with a Redis connection. Make sure to close the frontend object to release the Redis connection after use.

        Type Parameters:
        K - Key type.
        V - Value type.
        Parameters:
        cacheAccessor - the accessor used to interact with the client-side cache.
        connection - the Redis connection to use. The connection will be associated with CacheFrontend and must be closed through CacheFrontend.close().
        Returns:
        the CacheFrontend for value retrieval.
      • close

        public void close()
        Description copied from interface: CacheFrontend
        Closes this cache frontend and releases any system resources associated with it. If the frontend is already closed then invoking this method has no effect.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface CacheFrontend<K,​V>
        Specified by:
        close in interface Closeable
      • addInvalidationListener

        public void addInvalidationListener​(Consumer<K> invalidationListener)
      • get

        public V get​(K key)
        Description copied from interface: CacheFrontend
        Return the value to which this cache maps the specified key.

        Note: This method does not allow for differentiating between a cached null value and no cache entry found at all.

        Specified by:
        get in interface CacheFrontend<K,​V>
        Parameters:
        key - the key whose associated value is to be returned.
        Returns:
        the value to which this cache maps the specified key (which may be null itself), or also null if the cache contains no mapping for this key.
        See Also:
        CacheAccessor.get(Object), RedisCache.get(Object)
      • get

        public V get​(K key,
                     Callable<V> valueLoader)
        Description copied from interface: CacheFrontend
        Return the value to which this cache maps the specified key, obtaining that value from valueLoader if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern. If the valueLoader throws an exception, it is wrapped in a CacheFrontend.ValueRetrievalException
        Specified by:
        get in interface CacheFrontend<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        valueLoader - the value loader that is used to obtain the value if the client-side cache and Redis cache are not associated with a value.
        Returns:
        the value to which this cache maps the specified key.