This is the 13th day of my participation in the August More Text Challenge.More challenges in August


Spring Data Redis is a Spring integrated Redis access library. Spring Data Redis eliminates redundant boilerplate code that applications need to interact with Redis and provides a simple way to invoke it. Spring Data Redis makes it as easy for applications to interact with Redis as it is for Spring Data Jpa to interact with databases.

Client

Spring Data Redis integrates both Lettuce and Jedis to access the client of Redis.

  • Lettuce

The oracle is implemented based on Netty and is thread-safe when accessing concurrently from multiple threads. It is also scalable, that is, when there are not enough connection instances, you can add them as needed

  • Jedis

Jedis is directly connected to Redis Server, which is not thread-safe in multi-threaded environments, unless physical connections are added to each Jedis instance using connection pooling

RedisConnection

Spring Data Redis uses the RedisConnectionFactory to create a secure RedisConnection. RedisConnectionFactory supports redis for connecting single nodes, Sentinel mode redis, and cluster mode Redis. In addition, RedisConnectionFactory (connection factory) also inherited PersistenceExceptionTranslator supports abnormal conversion, For example, the runtime exception thrown by the persistence framework is converted to the corresponding exception in the DataAccessException hierarchy.

The easiest way to use a RedisConnectionFactory is to configure the appropriate connector through the IoC container and inject it as it is used

Template tool

Spring Data Redis provides two template utility classes: RedisTemplate and StringRedisTemplate. These two classes encapsulate commonly used Redis commands, and you only need to call the corresponding methods to complete the access to Redis. Both classes also provide callback methods for specific access requirements.

RedisTemplate and StringRedisTemplate provide the same functionality, but differ in how they are serialized. RedisTemplate supports multiple serialization methods, while StringRedisTemplate only supports StringRedisSerializer

RedisTemplate

Operation interface

interface describe
Key operations (redis operations for various data types)
GeoOperations Operations of the Geo type
HashOperations Hash operations
HyperLogLogOperations Operations related to HyperLogLog
ListOperations Operations of the list type
SetOperations Operations related to the set type
ValueOperations String operations
ZSetOperations Related operations of zset type
StreamOperations Operations related to Stream
Key binding operation (returns the utility class that operates on the Key after binding the Key)
BoundGeoOperations Bind the operations provided by the key of type GEO
BoundHashOperations Bind operations provided by keys of the hash type
BoundKeyOperations Bind operations provided by keys of the hash type
BoundStreamOperations Bind the actions provided by the key of type Stream
BoundSetOperations Bind the operation provided by a key of type set
BoundValueOperations Bind the operations provided by a string key
BoundZSetOperations Bind the actions provided by the key of type Zset

Execute the command

The execute related methods in RedisTemplate and StringRedisTemplate provide a callback RedisCallback parameter, and RedisCallback runs the application to operate on Redis directly through the Redis command.

When StringRedisTemplate is used, the callback receives an instance of StringRedisConnection

serialization

RedisTemplateSerialization inkey,value hashKey hashValue. inRedisTemplateThere arekeySerializer,valueSerializer,hashKeySerializer,hashValueSerializerThe attributes representkey,value hashKey hashValueAs shown in the figure below:

Spring Data RedisProvided by defaultbyteArray,java,json,stringType serialization. Can be achieved byRedisSerializerCorresponding method to obtain

Publish/subscribe messages

The publish/subscribe of Reids is real-time, meaning that subscribers can only receive messages published by publishers after the subscription, but not messages published by subscribers before the subscription

news

Messages can be published in two ways: redisconnect #publish or RedisTemplate#convertAndSend. The difference between these two approaches makes RedisConnection require raw data (byte arrays), and RedisTemplate allows arbitrary objects to be passed in as messages

Subscribe to news

At the receiving end, you can subscribe to one or more channels by channel name or by using pattern matching channels. In the Spring Data Redis subscribe message is mainly used in RedisMessageListenerContainer and MessageListener

  • MessageListener: Message listener

Each time a new message arrives, the callback is invoked and the user code is run through the onMessage method. This interface allows access not only to the actual messages, but also to the channels that have been passed and to subscribe to the patterns used to match the channels, if any

MessageListener has a default implementation class, MessageListenerAdapter, through which message processing can be delegated to the methods of other objects

  • RedisMessageListenerContainer: message listener container

RedisMessageListenerContainer is configured to accept messages from Redis and forward the message to a MessageListener processing.

RedisMessageListenerContainer allow multiple MessageListener sharing a connection and a single thread, even if they subscribe to a different channel

RedisMessageListenerContainer allow change the runtime configuration, so that you can add or delete a MessageListener when application is running, without having to restart.

RedisMessageListenerContainer using delay subscribe method, used only when needed to reconnect. If all the MessageListener unsubscribe, RedisMessageListenerContainer will automatically perform cleanup, and release the thread.

The transaction

Redis provides transaction support through watch, unwatch, multi, exec, and discard commands. These operations have corresponding methods in the RedisTemplate. RedisTemplate is not guaranteed to run all operations in a transaction using the same connection. You can use the execute related method if you want to run all operations in a transaction using the same connection.

By default, RedisTemplate opens the transaction and open transaction can use setEnableTransactionSupport (true) explicitly enable the transaction.

Pipelining

Redis provides support for pipes that send multiple commands to the server in succession and then read the response uniformly. Using pipes is a great way to improve performance. Execute (RedisCallback

action, Boolean exposeConnection, Boolean pipeline) can be used to pass the pipeline parameter true to open the pipeline operation. You can also use the executePielined method to perform an operation in a pipe using RedisCallback or SessionCallback and return the result