3

【笔记】SpringBoot项目通过StringRedisTemplate操作Redis

 1 year ago
source link: https://loli.fj.cn/2023/02/23/SpringBoot%E9%A1%B9%E7%9B%AE%E9%80%9A%E8%BF%87StringRedisTemplate%E6%93%8D%E4%BD%9CRedis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】SpringBoot项目通过StringRedisTemplate操作Redis

2023-02-23

SpringBoot项目通过StringRedisTemplate操作Redis

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
@Autowired
private StringRedisTemplate stringRedisTemplate;

<key>:键
<value>:值

stringRedisTemplate.opsForValue().set("<key>", "<value>");
  • 返回被覆盖的值

<new_value>:新值

String old_value = stringRedisTemplate.opsForValue().getAndSet("<key>", "<new_value>");

指定过期时间

  • 达到过期时间会自动销毁

<num>:指定过期时间,单位由第四个参数决定
TimeUnit.SECONDS:以秒为单位

stringRedisTemplate.opsForValue().set("<key>", "<value>", <num>, TimeUnit.SECONDS);

不存在时新增数据

  • 如果键不存在则插入键值对;如果键存在则中止插入数据
  • 返回是否插入成功的布尔值
boolean result = stringRedisTemplate.opsForValue().setIfAbsent("<key>", "<value>");

批量插入数据

覆盖批量插入数据

  • 如果已经存在键,则直接覆盖,并继续完成所有数据的插入
Map<Sring, String> map = new HashMap<>();
stringRedisTemplate.opsForValue().multiSet(map);

不覆盖批量插入数据

  • 如果已经存在键,则中止插入,并回滚所有数据的插入
  • 返回是否插入成功的布尔值
Map<Sring, String> map = new HashMap<>();
boolean result = stringRedisTemplate.opsForValue().multiSetIfAbsent(map);
  • 通过指定键删除键值对
  • 返回是否删除成功的布尔值
boolean result = stringRedisTemplate.opsForValue().getOperations().delete("<key>");
  • 返回成功删除的数据个数
List<String> keys = new ArrayList<>();
long results = stringRedisTemplate.opsForValue().getOperations().delete(Arrays.asList("test1", "test2"));
  • 如果不存在返回null

<key>:通过键获取值

stringRedisTemplate.opsForValue().get("<key>");

批量查询数据

List<String> keys = new ArrayList<>();
List<String> values = stringRedisTemplate.opsForValue().multiGet(keys);

处理其它数据类型的值

处理字符串类型的值

获取字符串长度

long size = stringRedisTemplate.opsForValue().size("<key>");

向字符串末尾追加子串

  • 返回新字符串的长度

<str>:子串

Integer append = stringRedisTemplate.opsForValue().append("<key>", "<str>");

处理数值型的值

  • 如果没有指定键,则立即通过指定键创建键值对,值默认为0
  • 如果指定键对应的值为非数值,则报错

对数值自增1

stringRedisTemplate.opsForValue().increment("<key>");

对数值自减1

stringRedisTemplate.opsForValue().decrement("<key>");

处理集合型的值

获取集合的长度

long size = stringRedisTemplate.opsForList().size("<key>");

从集合中获取值

获取指定下标的值

<index>:指定下标

String value = stringRedisTemplate.opsForList().index("<key>", <index>);
获取指定下标区间的值

<index_start>:开始位置下标
<index_end>:结束位置下标

List<String> list = stringRedisTemplate.opsForList().range("<key>", <index_start>, <index_stop>);

在集合中追加数据

  • 返回新的集合长度

<str>:追加的集合子元素

在指定下标位置追加数据
  • 如果指定下标位置没有数据,则添加数据
  • 如果指定下标位置有数据,则覆盖数据
  • 如果下标超出,则报错

<index>:下标位置
<str>:追加的数据

stringRedisTemplate.opsForList().set("<key>", <index>, "<str>");
在集合头部追加数据
  • 如果键不存在,则立即创建键值对
long size = stringRedisTemplate.opsForList().leftPush("<key>", "<str>");
long size = stringRedisTemplate.opsForList().leftPushAll("<key>", "<str_1>", "<str_2>");
只有当集合存在时才追加
long size = stringRedisTemplate.opsForList().leftPushIfPresent("<key>", "<str>");
在集合尾部追加数据
  • 如果键不存在,则立即创建键值对
long size = stringRedisTemplate.opsForList().rightPush("<key>", "<str>");
long size = stringRedisTemplate.opsForList().rightPushAll("<key>", "<str_1>", "<str_2>");
只有当集合存在时才追加
long size = stringRedisTemplate.opsForList().rightPushIfPresent("<key>", "<str>");

从集合中移除数据

  • 移除后返回被移除的数据
  • 如果移除后集合为空,则直接删除键值对
从集合头部移除数据
String result = stringRedisTemplate.opsForList().leftPop("<key>");
指定等待时间
  • 指定等待时间,在等待时间内如果出现数据则移除数据,如果没出现数据则无操作
String result = stringRedisTemplate.opsForList().leftPop("<key>", <num>, TimeUnit.SECONDS);
从集合尾部移除数据
String result = stringRedisTemplate.opsForList().rightPop("<key>");
指定等待时间
  • 指定等待时间,在等待时间内如果出现数据则移除数据,如果没出现数据则无操作
String result = stringRedisTemplate.opsForList().rightPop("<key>", <num>, TimeUnit.SECONDS);
移除指定的值
  • 移除与指定字符串相同的值

<str>:用于作对比的字符串
<num>:如果出现多个相同的值,指定移除哪个

>0:移除从左到右数,第<num>次出现相同的值
<0:移除从右到左数,第<num>次出现相同的值
=0:移除所有出现相同的值

long size = stringRedisTemplate.opsForList().remove("<key>", <num>, "<str>");
  • 从集合尾部移除并追加在另一个集合头部
  • 返回这个被转移的数据
String result = stringRedisTemplate.opsForList().rightPopAndLeftPush("<str_1>", "<str_2>");

截取指定长度的集合

  • 根据指定的下标截取集合的长度,多出来的数据将被移除

<index_start>:开始位置下标
<index_end>:结束位置下标

stringRedisTemplate.opsForList().trim("<key>", <index_start>, <index_end>);

处理键值对型的值

新增键值对

stringRedisTemplate.opsForHash().put("<key>","<value_k>","<value_v>");
Map<String, String> map = new HashMap<>();
map.put("<value_k_1>", "value_v_1");
map.put("<value_k_2>", "value_v_2");
stringRedisTemplate.opsForHash().putAll("<key>", map);
不覆盖新增
  • 如果键存在,则新增不存在的键值对
  • 如果键不存在,则创建新的键值对
  • 返回是否成功新增
boolean result = stringRedisTemplate.opsForHash().putIfAbsent("<key>","<value_k>","<value_v>");

获取键值对

Map<String, Map<String, String>> result = stringRedisTemplate.opsForHash().entries("<key>");

获取键值对的值

  • 如果值不存在返回null
String result = stringRedisTemplate.opsForHash().get("<key>", "<value_k>");

获取键值对的键

Set<String> keys = stringRedisTemplate.opsForHash().keys("<key>");

获取键值对长度

long size = stringRedisTemplate.opsForHash().size("<key>");

将键值对的键自增

  • 键值对的键必须是数值型,否则报错

<step>:自增的步长

long size = stringRedisTemplate.opsForHash().increment("<key>", "<key-k>", <step>);

批量获取指定键的值

List<String> values = stringRedisTemplate.opsForHash().multiGet("<key>", Arrays.asList("<key_k_1>", "<key_k_2>"));

根据指定键批量删除键值对

  • 返回删除成功的键值对个数
long result = stringRedisTemplate.opsForHash().delete("<key>", "<key_k_1>", "<key_k_2>");

CSDN——阿靖哦


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK