Class AbstractRedisCache<C>
java.lang.Object
io.micronaut.configuration.lettuce.cache.AbstractRedisCache<C>
- Type Parameters:
C- – The native cache implementation
- All Implemented Interfaces:
io.micronaut.cache.Cache<C>, io.micronaut.cache.SyncCache<C>, AutoCloseable
- Direct Known Subclasses:
RedisCache, RedisConnectionPoolCache
public abstract class AbstractRedisCache<C>
extends Object
implements io.micronaut.cache.SyncCache<C>, AutoCloseable
An abstract class implementing SyncCache for the redis.
- Since:
- 5.3.0
- Author:
- Graeme Rocher, Kovalov Illia
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Longprotected final ExpirationAfterWritePolicystatic final Stringprotected final Longprotected final Stringprotected final io.micronaut.core.serialize.ObjectSerializerprotected final RedisCacheConfigurationprotected final io.micronaut.core.serialize.ObjectSerializer -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractRedisCache(DefaultRedisCacheConfiguration defaultRedisCacheConfiguration, RedisCacheConfiguration redisCacheConfiguration, io.micronaut.core.convert.ConversionService conversionService, io.micronaut.context.BeanLocator beanLocator) -
Method Summary
Modifier and TypeMethodDescriptionprotected CompletionStage<List<byte[]>> allKeys(io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[], byte[]> redisKeyCommands, io.lettuce.core.ScanCursor initialCursor, io.lettuce.core.ScanArgs args) Collect the keys matching the given pattern using the provided async commands.protected CompletionStage<List<byte[]>> allKeys(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection, io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern for the current connection.protected List<byte[]> allKeys(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern for the current connection.protected List<byte[]> allKeys(io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern using the provided sync commands.protected final <T> TexecuteInsert(Supplier<T> supplier) Execute a cache insert with the configured retry count.protected final <T> CompletionStage<T> executeInsertAsync(Supplier<? extends CompletionStage<T>> supplier) Execute an asynchronous cache insert with the configured retry count.protected final <T> TexecuteRead(Supplier<T> supplier) Execute a cache read with the configured retry count.protected final <T> CompletionStage<T> executeReadAsync(Supplier<? extends CompletionStage<T>> supplier) Execute an asynchronous cache read with the configured retry count.protected <T> Tget(byte[] key, io.micronaut.core.type.Argument<T> requiredType, Supplier<T> supplier, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> commands) <T> Optional<T> protected Stringprotected io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[], byte[]> getRedisKeyAsyncCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) protected io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> getRedisKeyCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) protected io.lettuce.core.api.async.RedisStringAsyncCommands<byte[], byte[]> getRedisStringAsyncCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) protected io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> getRedisStringCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) protected abstract <T> Optional<T> getValue(io.micronaut.core.type.Argument<T> requiredType, byte[] serializedKey) protected <K,T> Map <K, T> getValues(@NonNull Collection<K> keys, @NonNull io.micronaut.core.type.Argument<T> requiredType, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Resolve the values for the given keys and convert them to the required type.protected voidinvalidateValues(@NonNull Collection<?> keys, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Invalidate the values for the given keys.void<T> Optional<T> putIfAbsent(Object key, T value) protected <T> voidputValue(byte[] serializedKey, Optional<byte[]> serialized, ExpirationAfterWritePolicy policy, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, T value) protected abstract <T> voidputValue(byte[] serializedKey, T value) protected voidputValues(@NonNull Map<?, ?> values, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Cache the specified values in bulk.protected byte[]serializeKey(Object key) Serialize the key.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AutoCloseable
closeMethods inherited from interface io.micronaut.cache.Cache
getCacheInfo, getName, getNativeCacheMethods inherited from interface io.micronaut.cache.SyncCache
async, get, get, get, getExecutorService, invalidate, invalidateAll, putIfAbsent
-
Field Details
-
INVALID_REDIS_CONNECTION_MESSAGE
- See Also:
-
keySerializer
protected final io.micronaut.core.serialize.ObjectSerializer keySerializer -
valueSerializer
protected final io.micronaut.core.serialize.ObjectSerializer valueSerializer -
redisCacheConfiguration
-
expireAfterWritePolicy
-
expireAfterAccess
-
invalidateScanCount
-
keyPrefix
-
-
Constructor Details
-
AbstractRedisCache
protected AbstractRedisCache(DefaultRedisCacheConfiguration defaultRedisCacheConfiguration, RedisCacheConfiguration redisCacheConfiguration, io.micronaut.core.convert.ConversionService conversionService, io.micronaut.context.BeanLocator beanLocator)
-
-
Method Details
-
get
-
put
-
putIfAbsent
-
get
protected <T> T get(byte[] key, io.micronaut.core.type.Argument<T> requiredType, Supplier<T> supplier, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> commands) - Type Parameters:
T- concrete type- Parameters:
key-requiredType-supplier-commands-- Returns:
- value from the cache
-
getRedisStringAsyncCommands
protected io.lettuce.core.api.async.RedisStringAsyncCommands<byte[],byte[]> getRedisStringAsyncCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) - Parameters:
connection-- Returns:
- RedisStringAsyncCommands
-
getRedisKeyAsyncCommands
protected io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[],byte[]> getRedisKeyAsyncCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) - Parameters:
connection-- Returns:
- RedisKeyAsyncCommands
-
getRedisStringCommands
protected io.lettuce.core.api.sync.RedisStringCommands<byte[],byte[]> getRedisStringCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) - Parameters:
connection-- Returns:
- RedisStringCommands
-
getRedisKeyCommands
protected io.lettuce.core.api.sync.RedisKeyCommands<byte[],byte[]> getRedisKeyCommands(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection) - Parameters:
connection-- Returns:
- RedisKeyCommands
-
allKeys
protected List<byte[]> allKeys(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern for the current connection.- Parameters:
connection- The connectionredisKeyCommands- The redis key commandspattern- The pattern to match- Returns:
- A list of matching keys
-
allKeys
protected CompletionStage<List<byte[]>> allKeys(io.lettuce.core.api.StatefulConnection<byte[], byte[]> connection, io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern for the current connection.- Parameters:
connection- The connectionredisKeyCommands- The redis key commandspattern- The pattern to match- Returns:
- A stage that completes with the matching keys
-
allKeys
protected List<byte[]> allKeys(io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, byte[] pattern) Collect the keys matching the given pattern using the provided sync commands.- Parameters:
redisKeyCommands- The redis key commandspattern- The pattern to match- Returns:
- A list of matching keys
-
allKeys
protected CompletionStage<List<byte[]>> allKeys(io.lettuce.core.api.async.RedisKeyAsyncCommands<byte[], byte[]> redisKeyCommands, io.lettuce.core.ScanCursor initialCursor, io.lettuce.core.ScanArgs args) Collect the keys matching the given pattern using the provided async commands.- Parameters:
redisKeyCommands- The redis key commandsinitialCursor- The initial cursorargs- The scan arguments- Returns:
- A stage that completes with the matching keys
-
getValue
protected abstract <T> Optional<T> getValue(io.micronaut.core.type.Argument<T> requiredType, byte[] serializedKey) - Type Parameters:
T- concrete type- Parameters:
requiredType-serializedKey-- Returns:
- An Optional Value
-
putValue
protected abstract <T> void putValue(byte[] serializedKey, T value) - Type Parameters:
T- concrete type- Parameters:
serializedKey-value-
-
putValue
protected <T> void putValue(byte[] serializedKey, Optional<byte[]> serialized, ExpirationAfterWritePolicy policy, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands, T value) - Type Parameters:
T- concrete type- Parameters:
serializedKey-serialized-policy-redisStringCommands-redisKeyCommands-value-
-
getValues
protected <K,T> Map<K,T> getValues(@NonNull Collection<K> keys, @NonNull io.micronaut.core.type.Argument<T> requiredType, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Resolve the values for the given keys and convert them to the required type.- Type Parameters:
K- The type of the unserialized keyT- The concrete type- Parameters:
keys- The cache keysrequiredType- The required typeredisStringCommands- The redis string commandsredisKeyCommands- The redis key commands- Returns:
- An ordered map containing all requested keys in the original order
-
putValues
protected void putValues(@NonNull Map<?, ?> values, io.lettuce.core.api.sync.RedisStringCommands<byte[], byte[]> redisStringCommands, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Cache the specified values in bulk.- Parameters:
values- The values to storeredisStringCommands- The redis string commandsredisKeyCommands- The redis key commands
-
invalidateValues
protected void invalidateValues(@NonNull Collection<?> keys, io.lettuce.core.api.sync.RedisKeyCommands<byte[], byte[]> redisKeyCommands) Invalidate the values for the given keys.- Parameters:
keys- The keys to invalidateredisKeyCommands- The redis key commands
-
getKeysPattern
- Returns:
- The default keys pattern.
-
serializeKey
Serialize the key.- Parameters:
key- The key- Returns:
- bytes of the object
-
executeRead
Execute a cache read with the configured retry count.- Type Parameters:
T- The result type- Parameters:
supplier- The read operation- Returns:
- The operation result
-
executeInsert
Execute a cache insert with the configured retry count.- Type Parameters:
T- The result type- Parameters:
supplier- The insert operation- Returns:
- The operation result
-
executeReadAsync
protected final <T> CompletionStage<T> executeReadAsync(Supplier<? extends CompletionStage<T>> supplier) Execute an asynchronous cache read with the configured retry count.- Type Parameters:
T- The result type- Parameters:
supplier- The read operation- Returns:
- The operation stage
-
executeInsertAsync
protected final <T> CompletionStage<T> executeInsertAsync(Supplier<? extends CompletionStage<T>> supplier) Execute an asynchronous cache insert with the configured retry count.- Type Parameters:
T- The result type- Parameters:
supplier- The insert operation- Returns:
- The operation stage
-