preface
In the previous article, we explained how to use Redis to solve the problem of session sharing in a cluster environment. Session sharing is implemented by integrating Redis. Redis dependencies have been introduced and Session sharing is implemented by springBoot configuration. Next, we will use the RedisTemplate provided by SpringBoot to manipulate Redis.
Injection RedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
Copy the code
Here I’ve injected a StringRedisTemplate, which is equivalent to RedisTemplate<String,String>. We can also customize a RedisTemplate as follows:
@Configuration public class RedisTemplateConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); / / configure the connection factory template. SetConnectionFactory (factory);returntemplate; }}Copy the code
In my opinion, there is no need to customize a RedisTemplate unless there is some serialization requirement. This article is based on the default StringRedisTemplate.
Sets/gets the value
We can set a value with opsForValue().set(k,v) to get the value
@Test
public void testsetAndGetString() {
redisTemplate.opsForValue().set("name"."happyjava");
String name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
Copy the code
Execution Result:
Check the values on the Redis database as follows:
Sets the value and expiration time at the same time
The opsForValue().set method also supports setting the expiration time corresponding to the key
@Test
public void testSetWithExpireTime() {
redisTemplate.opsForValue().set("name2"."happyjava2", 10, TimeUnit.SECONDS);
String name2 = redisTemplate.opsForValue().get("name2");
System.out.println(name2);
}
Copy the code
Execution Result:
happyjava2
Copy the code
Gets the expiration time of the key
We can get the key expiration time using the redistemplate.getexpire method
@Test
public void testSetWithExpireTime() {
redisTemplate.opsForValue().set("name2"."happyjava2", 10, TimeUnit.SECONDS);
String name2 = redisTemplate.opsForValue().get("name2");
System.out.println(name2);
Long expire = redisTemplate.getExpire("name2", TimeUnit.SECONDS);
System.out.println(expire);
}
Copy the code
The result is as follows:
happyjava2
9
Copy the code
Set the expiration time of the key
We can set the expiration time of the key using the redistemplate.expire method
@Test
public void testSetExpire() {
redisTemplate.expire("name",120,TimeUnit.SECONDS);
Long expire = redisTemplate.getExpire("name", TimeUnit.SECONDS);
System.out.println(expire);
}
Copy the code
I set name to be non-expired, so I set an expiration time for it. The result is as follows:
119
Copy the code
getAndSet
We can get the current value by using the opsForValue().getAndSet method and then set a new value.
@Test
public void test() {
redisTemplate.opsForValue().set("name"."123456");
String name = redisTemplate.opsForValue().getAndSet("name"."happyjava3");
System.out.println(name);
name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
Copy the code
The following output is displayed:
123456
happyjava3
Copy the code
Append additional
Through redisTemplate. OpsForValue (). Append method may add content.
@Test
public void test() {
redisTemplate.opsForValue().append("name"."append");
String name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
Copy the code
Here we append a string “append” to the previous name key, and the output is as follows:
happyjava3append
Copy the code
Since the increase
Increment is a very common method in Redis, which is often used to implement counters. We can use redisTemplate. OpsForValue (.) increment method to realize the increase
@Test
public void test() {
Long count = redisTemplate.opsForValue().increment("count");
System.out.println(count);
Long count1 = redisTemplate.opsForValue().increment("count"11); System.out.println(count1); }Copy the code
If the key does not exist, it increments from 0 by default. We can also set the size of the incremented value.
Since the reduction of
We can use redisTemplate. OpsForValue (). Decrement method to implement the decrement
@Test
public void test() {
Long count = redisTemplate.opsForValue().decrement("count");
System.out.println(count);
Long count1 = redisTemplate.opsForValue().decrement("count", 10);
System.out.println(count1);
}
Copy the code
Set if exists/set if not
SetIfAbsent: Set this parameter if it does not exist.
And the expiration time can be set through an overloading method, which is important to implement a distributed lock based on.
SetIfPresent: If it exists, the value is set.
@Test
public void test() {
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("name"."happy");
System.out.println(aBoolean);
Boolean aBoolean1 = redisTemplate.opsForValue().setIfPresent("name"."happy2");
System.out.println(aBoolean1);
}
Copy the code
Because the name value already exists, the expected output of this code is false true.
conclusion
Common operations on the Redis String data structure are described here. Other data structures are explained further in the following sections.