Usage details of Spring Data Redis

Posted by limitedreality on Sat, 18 Dec 2021 01:48:07 +0100

Introduction to Spring data redis

Spring data redis is a part of the spring family. It provides access to redis services through simple configuration in srping applications, and highly encapsulates the underlying development packages of reids (Jedis, JRedis, and RJC)

About key

1. Delete a single key

//Delete key
public void delete(String key){
redisTemplate.delete(key);
}

2. Delete multiple key s

//Delete multiple key s
public void deleteKey (String ...keys){
redisTemplate.delete(keys);
}

3. Specifies the expiration time of the key

//Specifies the expiration time of the key
public void expire(String key,long time){
redisTemplate.expire(key,time,TimeUnit.MINUTES);
}

4. Get expiration time according to key

//Get expiration time according to key
public long getExpire(String key){
Long expire = redisTemplate.getExpire(key);
return expire;
}

5. Determine whether the key exists

//Determine whether the key exists
public boolean hasKey(String key){
return redisTemplate.hasKey(key);
}

ops

Various operations of Redis's five data structures include:

opsForValue

redisTemplate.opsForValue(): operation string

Access data

//Store data
this.redisTemplate.opsForValue().set("key1", "value1");
//Get data
String val = this.redisTemplate.opsForValue().get("key1");
//Set expiration time
this.redisTemplate.opsForValue().set("key2", "value2",5, TimeUnit.HOURS);

opsForHash

redisTemplate.opsForHash(): Operation hash

opsForList

redisTemplate.opsForList(): operation list

opsForSet

redisTemplate.opsForSet(): operation set

opsForZSet

redisTemplate.opsForZSet(): operation zset

"bound"

You can encapsulate the specified key through bound, and then perform a series of operations without specifying the key again "explicitly".
BoundValueOperations
BoundSetOperations
BoundListOperations
BoundSetOperations
BoundHashOperations

Sequential increment

redisTemplate.boundValueOps("StringKey").increment(3L);

Sequential decrement

redisTemplate.boundValueOps("StringKey").increment(-3L);

string

Three operations set the cache

//1. Set the value through redisTemplate
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);

//2. Set values through BoundValueOperations
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.MINUTES);

//3. Setting values through ValueOperations
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);

Three operations to get cache

//1. Set the value through redisTemplate
String str1 = (String) redisTemplate.boundValueOps("StringKey").get();

//2. Get value through BoundValueOperations
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();

//3. Get value through ValueOperations
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");

Hash

Three settings cache

//1. Set the value through redisTemplate
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");

//2. Set values through BoundValueOperations
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");

//3. Setting values through ValueOperations
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");

Three ways to get key cache

//1. Get the value through redisTemplate
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

//2. Get value through BoundValueOperations
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

//3. Get value through ValueOperations
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

See here for details, to be continued

serialize

For "serialization / deserialization" of data, a variety of selectable policies (RedisSerializer) are provided

JdkSerializationRedisSerializer

JdkSerializationRedisSerializer: the access scenario of POJO objects. The JDK serialization mechanism is used to serialize POJO classes through ObjectInputStream/ObjectOutputStream. Finally, the byte sequence will be stored in redis server. Is currently the most commonly used serialization strategy.

StringRedisSerializer

StringRedisSerializer: in the scenario where the Key or value is a string, encode the byte sequence of the data into a string according to the specified charset, which is a direct encapsulation of "new String(bytes, charset)" and "string.getBytes(charset)". Is the most lightweight and efficient strategy.

JacksonJsonRedisSerializer

jackson jsonredisserializer: the jackson json tool provides the conversion capability between JavaBeans and json. It can serialize pojo instances into json format and store them in redis, or convert json format data into pojo instances. Because the jackson tool needs to explicitly specify the Class type when serializing and deserializing, this strategy is slightly complicated to encapsulate. [jackson mapper ASL tool support required]

Use details

yml configuration

# Redis server connection port
spring.redis.port=6379
# Redis server address
spring.redis.host=127.0.0.1
# Redis database index (0 by default)
spring.redis.database=0
# Redis server connection password (blank by default)
spring.redis.password=
# Maximum number of connections in the connection pool (negative value indicates no limit)
spring.redis.jedis.pool.max-active=8
# Maximum blocking wait time of connection pool (negative value indicates no limit)
spring.redis.jedis.pool.max-wait=-1ms
# Maximum free connections in the connection pool
spring.redis.jedis.pool.max-idle=8
# Minimum free connections in connection pool
spring.redis.jedis.pool.min-idle=0
# Connection timeout (MS)
spring.redis.timeout=5000ms

Topics: Java Redis Spring