概述
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