SpringBoot使用StringRedisTemplate操作Redis字符串


概述

StringRedisTemplate繼承自RedisTemplate<String, String>,當操作對象都是String類型的時候,就使用StringRedisTemplate即可。

  • 引入依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

常用操作

set/get

//設置key-value
stringRedisTemplate.opsForValue().set("name", "xiaoming");
System.out.println("name:" + stringRedisTemplate.opsForValue().get("name"));
//設置key-value,攜帶過期時間
stringRedisTemplate.opsForValue().set("nameWithExpire", "xiaodong", Duration.ofSeconds(2));
System.out.println("nameWithExpire:" + stringRedisTemplate.opsForValue().get("nameWithExpire"));
Thread.sleep(3000);   //線程等待3秒,讓key過期
System.out.println("nameWithExpire:" + stringRedisTemplate.opsForValue().get("nameWithExpire"));
  • 輸出如下:
    name:xiaoming
    nameWithExpire:xiaodong
    nameWithExpire:null

multiSet、multiGet

  • 批量設置key-value,或者批量獲取key值
//批量設置key-value
Map<String, String> map = new HashMap<>();
map.put("name", "xiaoming");
map.put("age", "22");
map.put("sex", "男");
stringRedisTemplate.opsForValue().multiSet(map);
//批量獲取key集合的值
List<String> keyList = new ArrayList<>();
keyList.add("name");
keyList.add("age");
keyList.add("sex");
System.out.println(stringRedisTemplate.opsForValue().multiGet(keyList));
  • 輸出如下:
    [xiaoming, 22, 男]

append

  • 追加字符串,跟java的append類似用法
stringRedisTemplate.opsForValue().setIfAbsent("name", "xiaoming");
stringRedisTemplate.opsForValue().append("name", " like reading");
System.out.println(stringRedisTemplate.opsForValue().get("name"));
  • 輸出如下:
    xiaoming like reading

setIfAbsent

  • 設置key,只有key不存在才生效,否則無效
//預設數據
stringRedisTemplate.opsForValue().set("name", "xiaoming");
//設置key-value,如果key不存在才生效
stringRedisTemplate.opsForValue().setIfAbsent("name", "xiaoming_new");
System.out.println("setIfAbsent:" + stringRedisTemplate.opsForValue().get("name"));
  • 輸出如下:
    setIfAbsent:xiaoming

setIfPresent

  • 設置key,只有key存在才生效,否則無效
stringRedisTemplate.opsForValue().set("name", "xiaoming");
//此時name已存在,所以設置無效
stringRedisTemplate.opsForValue().setIfPresent("name", "xiaoming_new");
System.out.println("setIfPresent:" + stringRedisTemplate.opsForValue().get("name"));
//name2不存在
stringRedisTemplate.opsForValue().setIfPresent("name2", "xiaoming_new2");
System.out.println("setIfPresent:" + stringRedisTemplate.opsForValue().get("name2"));
  • 輸出如下:
    setIfPresent:xiaoming_new
    setIfPresent:null

increment、decrement

  • 遞增、遞減
//初始化age=10
stringRedisTemplate.opsForValue().set("age", "10");
//遞增+1
stringRedisTemplate.opsForValue().increment("age");
System.out.println("increment:" + stringRedisTemplate.opsForValue().get("age"));
//遞增+10
stringRedisTemplate.opsForValue().increment("age", 10);
System.out.println("increment:" + stringRedisTemplate.opsForValue().get("age"));
//遞減-1
stringRedisTemplate.opsForValue().decrement("age");
System.out.println("decrement:" + stringRedisTemplate.opsForValue().get("age"));
//遞減-10
stringRedisTemplate.opsForValue().decrement("age", 10);
System.out.println("decrement:" + stringRedisTemplate.opsForValue().get("age"));
  • 輸出如下:
    increment:11
    increment:21
    decrement:20
    decrement:10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM