日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot使用StringRedisTemplate操作Redis字符串

發(fā)布時(shí)間:2025/3/19 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot使用StringRedisTemplate操作Redis字符串 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

概述

StringRedisTemplate繼承自RedisTemplate<String, String>,當(dāng)操作對象都是String類型的時(shí)候,就使用StringRedisTemplate即可。

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

常用操作

set/get

//設(shè)置key-value stringRedisTemplate.opsForValue().set("name", "xiaoming"); System.out.println("name:" + stringRedisTemplate.opsForValue().get("name")); //設(shè)置key-value,攜帶過期時(shí)間 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

  • 批量設(shè)置key-value,或者批量獲取key值
//批量設(shè)置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

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

setIfPresent

  • 設(shè)置key,只有key存在才生效,否則無效
stringRedisTemplate.opsForValue().set("name", "xiaoming"); //此時(shí)name已存在,所以設(shè)置無效 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
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的SpringBoot使用StringRedisTemplate操作Redis字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。