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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

°Fuhb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值