Package io.lettuce.core.support.caching
Class ClientSideCaching<K,V>
- 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 aCacheFrontend
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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.lettuce.core.support.caching.CacheFrontend
CacheFrontend.ValueRetrievalException
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addInvalidationListener(Consumer<K> invalidationListener)
void
close()
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 givenCacheAccessor
andStatefulRedisConnection
.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 givenCacheAccessor
andStatefulRedisConnection
.V
get(K key)
Return the value to which this cache maps the specified key.V
get(K key, Callable<V> valueLoader)
Return the value to which this cache maps the specified key, obtaining that value fromvalueLoader
if necessary.
-
-
-
Method Detail
-
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 givenCacheAccessor
andStatefulRedisConnection
.Note that the
CacheFrontend
is associated with a Redis connection. Make sure toclose
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 withCacheFrontend
and must be closed throughCacheFrontend.close()
.tracking
- the tracking parameters.- Returns:
- the
CacheFrontend
for 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 givenCacheAccessor
andStatefulRedisConnection
. This method expects that client key tracking is already configured.Note that the
CacheFrontend
is associated with a Redis connection. Make sure toclose
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 withCacheFrontend
and must be closed throughCacheFrontend.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 interfaceAutoCloseable
- Specified by:
close
in interfaceCacheFrontend<K,V>
- Specified by:
close
in interfaceCloseable
-
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 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
null
itself), or alsonull
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 fromvalueLoader
if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern. If thevalueLoader
throws an exception, it is wrapped in aCacheFrontend.ValueRetrievalException
- Specified by:
get
in 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.
-
-