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>, org.springframework.data.keyvalue.core.KeyValueAdapter

public class RedisKeyValueAdapter extends org.springframework.data.keyvalue.core.AbstractKeyValueAdapter implements InitializingBean, ApplicationContextAware, ApplicationListener<RedisKeyspaceEvent>
Redis specific 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
  • Constructor Details

  • Method Details

    • put

      public Object put(Object id, Object item, String keyspace)
      Specified by:
      put in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • contains

      public boolean contains(Object id, String keyspace)
      Specified by:
      contains in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • get

      @Nullable public Object get(Object id, String keyspace)
      Specified by:
      get in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • get

      @Nullable public <T> T get(Object id, String keyspace, Class<T> type)
      Specified by:
      get in interface org.springframework.data.keyvalue.core.KeyValueAdapter
      Overrides:
      get in class org.springframework.data.keyvalue.core.AbstractKeyValueAdapter
    • delete

      public Object delete(Object id, String keyspace)
      Specified by:
      delete in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • delete

      public <T> T delete(Object id, String keyspace, Class<T> type)
      Specified by:
      delete in interface org.springframework.data.keyvalue.core.KeyValueAdapter
      Overrides:
      delete in class org.springframework.data.keyvalue.core.AbstractKeyValueAdapter
    • getAllOf

      public List<?> getAllOf(String keyspace)
      Specified by:
      getAllOf in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • getAllOf

      public <T> Iterable<T> getAllOf(String keyspace, Class<T> type)
      Specified by:
      getAllOf in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • getAllOf

      public <T> List<T> getAllOf(String keyspace, Class<T> type, long offset, int rows)
      Get all elements for given keyspace.
      Type Parameters:
      T -
      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

      public void deleteAllOf(String keyspace)
      Specified by:
      deleteAllOf in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • entries

      public CloseableIterator<Map.Entry<Object,Object>> entries(String keyspace)
      Specified by:
      entries in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • count

      public long count(String keyspace)
      Specified by:
      count in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • update

      public void update(PartialUpdate<?> update)
    • execute

      @Nullable public <T> T execute(RedisCallback<T> callback)
      Execute RedisCallback via underlying RedisOperations.
      Parameters:
      callback - must not be null.
      Returns:
      See Also:
    • getConverter

      public RedisConverter getConverter()
      Get the RedisConverter in use.
      Returns:
      never null.
    • clear

      public void clear()
      Specified by:
      clear in interface org.springframework.data.keyvalue.core.KeyValueAdapter
    • createKey

      public byte[] createKey(String keyspace, String id)
    • toBytes

      public byte[] toBytes(Object source)
      Convert given source to binary representation using the underlying ConversionService.
      Parameters:
      source -
      Returns:
      Throws:
      ConverterNotFoundException
    • setEnableKeyspaceEvents

      public void setEnableKeyspaceEvents(RedisKeyValueAdapter.EnableKeyspaceEvents enableKeyspaceEvents)
      Parameters:
      enableKeyspaceEvents -
      Since:
      1.8
    • setMessageListenerContainer

      public void setMessageListenerContainer(RedisMessageListenerContainer messageListenerContainer)
      Configure a RedisMessageListenerContainer to listen for Keyspace expiry events. The container can only be set when this bean hasn't been yet initialized.
      Parameters:
      messageListenerContainer - the container to use.
      Throws:
      IllegalStateException - when trying to set a RedisMessageListenerContainer after afterPropertiesSet() has been called to initialize a managed container instance.
      Since:
      2.7.2
    • setKeyspaceNotificationsConfigParameter

      public void setKeyspaceNotificationsConfigParameter(String keyspaceNotificationsConfigParameter)
      Configure the notify-keyspace-events property if not already set. Use an empty String or null to retain existing server settings.
      Parameters:
      keyspaceNotificationsConfigParameter - can be null.
      Since:
      1.8
    • setShadowCopy

      public void setShadowCopy(RedisKeyValueAdapter.ShadowCopy shadowCopy)
      Configure storage of phantom keys (shadow copies) of expiring entities.
      Parameters:
      shadowCopy - must not be null.
      Since:
      2.3
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface InitializingBean
      Since:
      1.8
      See Also:
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface DisposableBean
      Throws:
      Exception
    • onApplicationEvent

      public void onApplicationEvent(RedisKeyspaceEvent event)
      Specified by:
      onApplicationEvent in interface ApplicationListener<RedisKeyspaceEvent>
    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Throws:
      BeansException