java.lang.Object
io.lettuce.core.support.caching.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:
MapclientCache = 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
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.lettuce.core.support.caching.CacheFrontend
CacheFrontend.ValueRetrievalException -
Method Summary
Modifier and Type Method Description voidaddInvalidationListener(Consumer<K> invalidationListener)voidclose()Closes this cache frontend and releases any system resources associated with it.static <K, V> CacheFrontend<K,V>create(CacheAccessor<K,V> cacheAccessor, StatefulRedisConnection<K,V> connection)Create a server-assisted Client side caching for the givenCacheAccessorandStatefulRedisConnection.static <K, V> CacheFrontend<K,V>enable(CacheAccessor<K,V> cacheAccessor, StatefulRedisConnection<K,V> connection, TrackingArgs tracking)Enable server-assisted Client side caching for the givenCacheAccessorandStatefulRedisConnection.Vget(K key)Return the value to which this cache maps the specified key.Vget(K key, Callable<V> valueLoader)Return the value to which this cache maps the specified key, obtaining that value fromvalueLoaderif necessary.
-
Method Details
-
enable
public static <K, V> CacheFrontend<K,V> enable(CacheAccessor<K,V> cacheAccessor, StatefulRedisConnection<K,V> connection, TrackingArgs tracking)Enable server-assisted Client side caching for the givenCacheAccessorandStatefulRedisConnection.Note that the
CacheFrontendis associated with a Redis connection. Make sure toclosethe 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 withCacheFrontendand must be closed throughCacheFrontend.close().tracking- the tracking parameters.- Returns:
- the
CacheFrontendfor value retrieval.
-
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 givenCacheAccessorandStatefulRedisConnection. This method expects that client key tracking is already configured.Note that the
CacheFrontendis associated with a Redis connection. Make sure toclosethe 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 withCacheFrontendand must be closed throughCacheFrontend.close().- Returns:
- the
CacheFrontendfor value retrieval.
-
close
public void close()Description copied from interface:CacheFrontendCloses 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCacheFrontend<K,V>- Specified by:
closein interfaceCloseable
-
addInvalidationListener
-
get
Description copied from interface:CacheFrontendReturn the value to which this cache maps the specified key.Note: This method does not allow for differentiating between a cached
nullvalue and no cache entry found at all.- Specified by:
getin interfaceCacheFrontend<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
nullitself), or alsonullif the cache contains no mapping for this key. - See Also:
CacheAccessor.get(Object),RedisCache.get(Object)
-
get
Description copied from interface:CacheFrontendReturn the value to which this cache maps the specified key, obtaining that value fromvalueLoaderif necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern. If thevalueLoaderthrows an exception, it is wrapped in aCacheFrontend.ValueRetrievalException- Specified by:
getin interfaceCacheFrontend<K,V>- Parameters:
key- the key whose associated value is to be returnedvalueLoader- 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.
-