Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of Spring Data Redis

Shulou Source: shulou.com Published: 2022-06-02 04:28:07 10月03日 Update

This article introduces you to the example analysis of Spring Data Redis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Spring Data Redis introduction

Spring-Data-Redis is a part of the Spring family, which provides access to Redis services through simple configuration in Spring applications, and highly encapsulates the underlying Redis development packages (Jedis, JRedis, Redisson). RedisTemplate provides a variety of Redis operations, exception handling and serialization, and supports publish and subscribe. Spring-Data-Redis provides the following features for jedis:

Automatic connection pool management, providing a highly encapsulated "RedisTemplate" class

A large number of api in jedis client are classified and encapsulated, and the same type of operation is encapsulated as operation API as follows:

ValueOperations: simple Kmurv operation

SetOperations: set type data operation

ZSetOperations: zset type data operation

HashOperations: data manipulation of hash type

ListOperations: data manipulation of list type

Brief introduction of Redis data structure

Redis can store the mapping between keys and five different types of data structures, namely String (string), List (list), Set (collection), Hash (hash), and Zset (ordered set).

RedisTemplate introduction

Spring encapsulates RedisTemplate objects to perform various operations on Redis, and it supports all Redis native api. RedisTemplate is located under the spring-data-redis package.

The structure of RedisTemplate in Spring code is as follows:

Org.springframework.data.redis.coreClass RedisTemplatejava.lang.Object org.springframework.data.redis.core.RedisAccessor org.springframework.data.redis.core.RedisTemplate

Type Parameters:

K

The type of Redis key in the template (usually String) such as: RedisTemplate Note: if there is no special case, do not define as RedisTemplate, otherwise according to the Richter scale replacement principle, it will cause type errors when used.

V

The type of Redis value in the template

StringRedisTemplate and RedisTemplate

The relationship between the two is that StringRedisTemplate inherits RedisTemplate.

The data of the two are not in common; that is, StringRedisTemplate can only manage the data in StringRedisTemplate, and RedisTemplate can only manage the data in RedisTemplate.

There are two kinds of serialization strategies adopted by SDR by default, one is the serialization strategy of String and the other is the serialization strategy of JDK. StringRedisTemplate defaults to the serialization strategy of String, and the saved key and value are serialized and saved using this policy. RedisTemplate defaults to the serialization strategy of JDK, and the saved key and value are serialized and saved using this policy.

Spring Data Redis use case

This is the end of the sample analysis on Spring Data Redis. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data type sequence policy encapsulation structure data structure management example analysis content more template height help support different good orderly special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker OPPO Reno NVidia MySQL