[13] springboot fast learning. String data structure of the operation redis

Foreword

In the previous article, explaining the use of redis solve the problem cluster environment session shared [fast learning springboot] 11. integration redis realize session sharing , there has been introduced redis related to dependence, and by springboot configuration to achieve the session sharing. Here, we have to operate redis by RedisTemplate springboot provided.

Injection RedisTemplate

@Autowired
private StringRedisTemplate redisTemplate;
复制代码

Here I 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<>();
 // 配置连接工厂
 template.setConnectionFactory(factory);
 return template;
 }
​
}
复制代码

I think, there is no need to customize a RedisTemplate, said that unless there are some requirements on serialization. Explain this article are based on the default StringRedisTemplate.

Set / get value

We can). Set (k, v) method to set a value opsForValue (). Get (k) by opsForValue (acquisition value method

@Test
public void testsetAndGetString() {
 redisTemplate.opsForValue().set("name", "happyjava");
 String name = redisTemplate.opsForValue().get("name");
 System.out.println(name);
}
复制代码

Results of the:

Check the value of the database redis, as follows:

Set value and simultaneously set the expiration time

opsForValue (). set provided further support simultaneous 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);
}
复制代码

Results of the:

happyjava2
复制代码

Gets the key expiration time

We can get key method by redisTemplate.getExpire expiration time

@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);
}
复制代码

Execution results are as follows:

happyjava2
9
复制代码

Key expiration time set

We can set an expiration time key method by redisTemplate.expire

@Test
public void testSetExpire() {
 redisTemplate.expire("name",120,TimeUnit.SECONDS);
 Long expire = redisTemplate.getExpire("name", TimeUnit.SECONDS);
 System.out.println(expire);
}
复制代码

Before setting up the name of a non-expired, here gives it an expiration time. Execution results are as follows:

119
复制代码

getAndSet

We can (). GetAndSet method to get value at this time by opsForValue, 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);
}
复制代码

Output:

123456
happyjava3
复制代码

append additional

By redisTemplate.opsForValue (). Append method may additional content.

 @Test
 public void test() {
 redisTemplate.opsForValue().append("name","append");
 String name = redisTemplate.opsForValue().get("name");
 System.out.println(name);
 }
复制代码

Here added a string "append" to the key before the name, the output results are as follows:

happyjava3append
复制代码

Increment

Redis increment is in a very common method, often used this method to implement counter. We can). Increment by increment method to achieve redisTemplate.opsForValue (

 @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);
 }
复制代码

If the key does not exist, the default increment from zero, we can also set the size of the increment value.

Decrement

We can achieve decrement by redisTemplate.opsForValue (). Decrement method

 @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);
 }
复制代码

It is set if there is / are provided if there is no

setIfAbsent: If not, then Settings.

And you can set the expiration time by overloaded methods, this method is very important, you can implement a distributed lock based on this method.

setIfPresent: If there is, then Settings.

 @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);
 }
​
复制代码

Because the values ​​before the name already exists, the expected output of the code is false true.

to sum up

Used herein describes the operation redis string data structures. The following disturbances in other data structures for further explanation.

Reproduced in: https: //juejin.im/post/5d05e7b0f265da1bcd37d67e

Guess you like

Origin blog.csdn.net/weixin_33682719/article/details/93183804