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
Modifier and TypeClassDescriptionstatic enum
static enum
Configuration flag controlling storage of phantom keys (shadow copies) of expiring entities to read them later when publishingRedisKeyspaceEvent
. -
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ModifierConstructorDescriptionprotected
Default constructor.RedisKeyValueAdapter
(RedisOperations<?, ?> redisOps) Creates newRedisKeyValueAdapter
with defaultRedisMappingContext
and defaultRedisCustomConversions
.RedisKeyValueAdapter
(RedisOperations<?, ?> redisOps, RedisConverter redisConverter) Creates newRedisKeyValueAdapter
with specificRedisConverter
.RedisKeyValueAdapter
(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext) Creates newRedisKeyValueAdapter
with defaultRedisCustomConversions
.RedisKeyValueAdapter
(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext, CustomConversions customConversions) Creates newRedisKeyValueAdapter
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
clear()
boolean
long
byte[]
<T> T
void
deleteAllOf
(String keyspace) void
destroy()
<T> T
execute
(RedisCallback<T> callback) ExecuteRedisCallback
via underlyingRedisOperations
.<T> T
List<?>
<T> Iterable<T>
<T> List<T>
Get all elements for given keyspace.Get theRedisConverter
in use.boolean
void
void
setApplicationContext
(ApplicationContext applicationContext) void
setEnableKeyspaceEvents
(RedisKeyValueAdapter.EnableKeyspaceEvents enableKeyspaceEvents) Configure usage ofKeyExpirationEventMessageListener
.void
setKeyspaceNotificationsConfigParameter
(String keyspaceNotificationsConfigParameter) Configure the notify-keyspace-events property if not already set.void
setMessageListenerContainer
(RedisMessageListenerContainer messageListenerContainer) Configure aRedisMessageListenerContainer
to listen for Keyspace expiry events.void
setShadowCopy
(RedisKeyValueAdapter.ShadowCopy shadowCopy) Configure storage of phantom keys (shadow copies) of expiring entities.void
start()
void
stop()
byte[]
Convert given source to binary representation using the underlyingConversionService
.void
update
(PartialUpdate<?> update) Methods inherited from class org.springframework.data.keyvalue.core.AbstractKeyValueAdapter
count, find, find, getQueryEngine
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecution
Methods inherited from interface org.springframework.data.keyvalue.core.KeyValueAdapter
entries, exists
Methods inherited from interface org.springframework.context.SmartLifecycle
getPhase, isAutoStartup, stop
-
Constructor Details
-
RedisKeyValueAdapter
Creates newRedisKeyValueAdapter
with defaultRedisMappingContext
and defaultRedisCustomConversions
.- Parameters:
redisOps
- must not be null.
-
RedisKeyValueAdapter
Creates newRedisKeyValueAdapter
with 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 newRedisKeyValueAdapter
with specificRedisConverter
.- Parameters:
redisOps
- must not be null.redisConverter
- must not be null.
-
RedisKeyValueAdapter
protected RedisKeyValueAdapter()Default constructor.
-
-
Method Details
-
put
- Specified by:
put
in interfaceKeyValueAdapter
-
contains
- Specified by:
contains
in interfaceKeyValueAdapter
-
get
- Specified by:
get
in interfaceKeyValueAdapter
-
get
- Specified by:
get
in interfaceKeyValueAdapter
- Overrides:
get
in classAbstractKeyValueAdapter
-
delete
- Specified by:
delete
in interfaceKeyValueAdapter
-
delete
- Specified by:
delete
in interfaceKeyValueAdapter
- Overrides:
delete
in classAbstractKeyValueAdapter
-
getAllOf
- Specified by:
getAllOf
in interfaceKeyValueAdapter
-
getAllOf
- Specified by:
getAllOf
in 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:
deleteAllOf
in interfaceKeyValueAdapter
-
entries
- Specified by:
entries
in interfaceKeyValueAdapter
-
count
- Specified by:
count
in interfaceKeyValueAdapter
-
update
-
execute
ExecuteRedisCallback
via underlyingRedisOperations
.- Parameters:
callback
- must not be null.- See Also:
-
getConverter
Get theRedisConverter
in use.- Returns:
- never null.
-
clear
public void clear()- Specified by:
clear
in interfaceKeyValueAdapter
-
createKey
-
toBytes
Convert given source to binary representation using the underlyingConversionService
. -
setEnableKeyspaceEvents
Configure usage ofKeyExpirationEventMessageListener
.- Since:
- 1.8
-
setMessageListenerContainer
Configure aRedisMessageListenerContainer
to 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 aRedisMessageListenerContainer
afterafterPropertiesSet()
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 emptyString
or 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
public boolean isRunning() -
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Since:
- 1.8
- See Also:
-
start
public void start() -
stop
public void stop() -
destroy
- Specified by:
destroy
in interfaceDisposableBean
- Throws:
Exception
-
onApplicationEvent
- Specified by:
onApplicationEvent
in interfaceApplicationListener<RedisKeyspaceEvent>
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-