Class RedisKeyValueAdapter
java.lang.Object
org.springframework.data.keyvalue.core.AbstractKeyValueAdapter
org.springframework.data.redis.core.RedisKeyValueAdapter
- All Implemented Interfaces:
EventListener, Aware, DisposableBean, InitializingBean, ApplicationContextAware, ApplicationListener<RedisKeyspaceEvent>, Lifecycle, Phased, SmartLifecycle, KeyValueAdapter
public class RedisKeyValueAdapter
extends AbstractKeyValueAdapter
implements InitializingBean, SmartLifecycle, ApplicationContextAware, ApplicationListener<RedisKeyspaceEvent>
Redis specific
Example
The
KeyValueAdapter implementation. Uses binary codec to read/write data from/to Redis. Objects
are stored in a Redis Hash using the value of RedisHash, the KeyspaceConfiguration or just
Class.getName() as a prefix. Example
@RedisHash("persons")
class Person {
@Id String id;
String name;
}
prefix ID
| |
V V
hgetall persons:5d67b7e1-8640-4475-beeb-c666fab4c0e5
1) id
2) 5d67b7e1-8640-4475-beeb-c666fab4c0e5
3) name
4) Rand al'Thor
The
KeyValueAdapter is not intended to store simple types such as String values.
Please use RedisTemplate for this purpose.- Since:
- 1.7
- Author:
- Christoph Strobl, Mark Paluch, Andrey Muchnik, John Blum
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic enumConfiguration flag controlling storage of phantom keys (shadow copies) of expiring entities to read them later when publishingRedisKeyspaceEvent. -
Field Summary
Fields inherited from interface SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRedisKeyValueAdapter(RedisOperations<?, ?> redisOps) Creates newRedisKeyValueAdapterwith defaultRedisMappingContextand defaultRedisCustomConversions.RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisConverter redisConverter) Creates newRedisKeyValueAdapterwith specificRedisConverter.RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext) Creates newRedisKeyValueAdapterwith defaultRedisCustomConversions.RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext, @Nullable CustomConversions customConversions) Creates newRedisKeyValueAdapter. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidclear()booleanlongbyte[]@Nullable Object<T> @Nullable TvoiddeleteAllOf(String keyspace) voiddestroy()<T> @Nullable Texecute(RedisCallback<T> callback) ExecuteRedisCallbackvia underlyingRedisOperations.@Nullable Object<T> @Nullable T<T> List<T> <T> List<T> Get all elements for given keyspace.Get theRedisConverterin use.booleanvoidvoidsetApplicationContext(ApplicationContext applicationContext) voidsetEnableKeyspaceEvents(RedisKeyValueAdapter.EnableKeyspaceEvents enableKeyspaceEvents) Configure usage ofKeyExpirationEventMessageListener.voidsetKeyspaceNotificationsConfigParameter(String keyspaceNotificationsConfigParameter) Configure the notify-keyspace-events property if not already set.voidsetMessageListenerContainer(RedisMessageListenerContainer messageListenerContainer) Configure aRedisMessageListenerContainerto listen for Keyspace expiry events.voidsetShadowCopy(RedisKeyValueAdapter.ShadowCopy shadowCopy) Configure storage of phantom keys (shadow copies) of expiring entities.voidstart()voidstop()byte[]Convert given source to binary representation using the underlyingConversionService.voidupdate(PartialUpdate<?> update) Methods inherited from class AbstractKeyValueAdapter
count, find, find, getQueryEngineMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ApplicationListener
supportsAsyncExecutionMethods inherited from interface KeyValueAdapter
entries, existsMethods inherited from interface SmartLifecycle
getPhase, isAutoStartup, isPauseable, stop
-
Constructor Details
-
RedisKeyValueAdapter
protected RedisKeyValueAdapter() -
RedisKeyValueAdapter
Creates newRedisKeyValueAdapterwith defaultRedisMappingContextand defaultRedisCustomConversions.- Parameters:
redisOps- must not be null.
-
RedisKeyValueAdapter
Creates newRedisKeyValueAdapterwith defaultRedisCustomConversions.- Parameters:
redisOps- must not be null.mappingContext- must not be null.
-
RedisKeyValueAdapter
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext, @Nullable CustomConversions customConversions) Creates newRedisKeyValueAdapter.- Parameters:
redisOps- must not be null.mappingContext- must not be null.customConversions- can be null.- Since:
- 2.0
-
RedisKeyValueAdapter
Creates newRedisKeyValueAdapterwith specificRedisConverter.- Parameters:
redisOps- must not be null.redisConverter- must not be null.
-
-
Method Details
-
put
-
contains
- Specified by:
containsin interfaceKeyValueAdapter
-
get
- Specified by:
getin interfaceKeyValueAdapter
-
get
- Specified by:
getin interfaceKeyValueAdapter- Overrides:
getin classAbstractKeyValueAdapter
-
delete
- Specified by:
deletein interfaceKeyValueAdapter
-
delete
- Specified by:
deletein interfaceKeyValueAdapter- Overrides:
deletein classAbstractKeyValueAdapter
-
getAllOf
- Specified by:
getAllOfin interfaceKeyValueAdapter
-
getAllOf
- Specified by:
getAllOfin interfaceKeyValueAdapter
-
getAllOf
Get all elements for given keyspace.- Parameters:
keyspace- the keyspace to fetch entities from.type- the desired target type.offset- index value to start reading.rows- maximum number or entities to return.- Returns:
- never null.
- Since:
- 2.5
-
deleteAllOf
- Specified by:
deleteAllOfin interfaceKeyValueAdapter
-
entries
- Specified by:
entriesin interfaceKeyValueAdapter
-
count
- Specified by:
countin interfaceKeyValueAdapter
-
update
-
execute
ExecuteRedisCallbackvia underlyingRedisOperations.- Parameters:
callback- must not be null.- See Also:
-
getConverter
-
clear
public void clear()- Specified by:
clearin interfaceKeyValueAdapter
-
createKey
-
toBytes
Convert given source to binary representation using the underlyingConversionService. -
setEnableKeyspaceEvents
Configure usage ofKeyExpirationEventMessageListener.- Since:
- 1.8
-
setMessageListenerContainer
Configure aRedisMessageListenerContainerto listen for Keyspace expiry events. The container can only be set when this bean hasn't been yetinitialized.- Parameters:
messageListenerContainer- the container to use.- Throws:
IllegalStateException- when trying to set aRedisMessageListenerContainerafterafterPropertiesSet()has been called to initialize a managed container instance.- Since:
- 2.7.2
-
setKeyspaceNotificationsConfigParameter
Configure the notify-keyspace-events property if not already set. Use an emptyStringor null to retain existing server settings.- Parameters:
keyspaceNotificationsConfigParameter- can be null.- Since:
- 1.8
-
setShadowCopy
Configure storage of phantom keys (shadow copies) of expiring entities.- Parameters:
shadowCopy- must not be null.- Since:
- 2.3
-
isRunning
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean- Since:
- 1.8
- See Also:
-
start
-
stop
-
destroy
- Specified by:
destroyin interfaceDisposableBean- Throws:
Exception
-
onApplicationEvent
- Specified by:
onApplicationEventin interfaceApplicationListener<RedisKeyspaceEvent>
-
setApplicationContext
- Specified by:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-