RedisTemplate操作Redis

文章目录



在这里插入图片描述

在这里插入图片描述


RedisTemplateSpring Framework 提供的用于操作 Redis 数据库的模板类。通过 RedisTemplate,开发人员可以方便地使用 Spring 提供的 API 来对 Redis 进行操作,比如设置值、获取值、删除值等。RedisTemplate 封装了 Redis 连接、数据序列化、异常处理等操作,简化了与 Redis 的交互过程。

一般情况下,开发人员需要配置 Redis 连接池、序列化器等信息,并将 RedisTemplate 注入到需要使用 Redis 的 Bean 中,然后就可以通过 RedisTemplate 来进行相应的操作。

需要注意的是,RedisTemplate 是基于 Spring Data Redis 提供的操作 Redis 客户端的功能而构建的,可以通过 RedisTemplate 李进行操作 Redis 数据库的常用数据结构,如字符串、列表、集合、有序集合等。


1. String 命令

1.1 添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);

//2、通过BoundValueOperations设置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.MINUTES);

//3、通过ValueOperations设置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);

//4(SETNX + SETEX):这个key不存在执行 存在则不执行,多用于互斥锁
ops.setIfAbsent("key", "value", 10, TimeUnit.SECONDS)

1.2 设置过期时间(单独设置)

redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);

不建议使用单独设置过期时间的API, 可以使用 1.1 中的第一个演示,在设置值的同时设置过期时间.

1.3 获取缓存值

//1、通过redisTemplate设置值
String str1 = (String) redisTemplate.boundValueOps("StringKey").get();

//2、通过BoundValueOperations获取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();

//3、通过ValueOperations获取值
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");

1.4 删除key

Boolean result = redisTemplate.delete("key");

1.5 顺序递增

redisTemplate.boundValueOps("key").increment(1L);

该API会返回递增后的值. 如果KEY对应的值不存在会创建之并返回1

1.6 顺序递减

redisTemplate.boundValueOps("StringKey").increment(-3L);

1.7 常用的

ValueOperations ops = redisTemplate.opsForValue();

//	单独设置有效期(不推荐单独用)
ops.expire("StringKey",1,TimeUnit.MINUTES);

//	设置值 and 有效期(推荐这种)
ops.set("key", "value", 1, TimeUnit.MINUTES);

//	操作数值 增加 减少(INCR INCRBY)
ops.increment("key", 1);
ops.increment("key", -1);

//	(SETNX + SETEX):这个key不存在执行 存在则不执行,多用于互斥锁
ops.setIfAbsent("key", "value", 10, TimeUnit.SECONDS)

//	获取缓存值
ops.get("StringKey");

2. Hash命令

2.1 添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");

//2、通过BoundValueOperations设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");

//3、通过ValueOperations设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");

2.2 设置过期时间(单独设置)

HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );

注意:只能给大 KEY 设置过期时间, 小 KEY 不能拥有独立的过期时间.

2.3 添加一个Map集合

HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );

2.4 提取所有的小key

//1、通过redisTemplate获取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

2.5 提取所有的value值

//1、通过redisTemplate获取值
List values1 = redisTemplate.boundHashOps("HashKey").values();

//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
List values2 = hashKey.values();

//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
List values3 = hashOps.values("HashKey");

2.6 根据key提取value值

//1、通过redisTemplate获取
String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");

//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
String value2 = (String) hashKey.get("SmallKey");

//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
String value3 = (String) hashOps.get("HashKey", "SmallKey");

2.7 获取所有的键值对集合

//1、通过redisTemplate获取
Map entries = redisTemplate.boundHashOps("HashKey").entries();

//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Map entries1 = hashKey.entries();

//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries2 = hashOps.entries("HashKey");

2.8 删除

//删除小key
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
//删除大key
redisTemplate.delete("HashKey");

2.9 判断Hash中是否含有该值

Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

3. List命令

3.1 添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundListOps("listKey").leftPush("listLeftValue1");
redisTemplate.boundListOps("listKey").rightPush("listRightValue2");

//2、通过BoundValueOperations设置值
BoundListOperations listKey = redisTemplate.boundListOps("listKey");
listKey.leftPush("listLeftValue3");
listKey.rightPush("listRightValue4");

//3、通过ValueOperations设置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush("listKey", "listLeftValue5");
opsList.rightPush("listKey", "listRightValue6");

3.2 将List放入缓存

ArrayList<String> list = new ArrayList<>();
//	left
redisTemplate.boundListOps("listKey").leftPushAll(list);
//	right
redisTemplate.boundListOps("listKey").rightPushAll(list);

3.3 设置过期时间(单独设置)

redisTemplate.boundValueOps("listKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("listKey",1,TimeUnit.MINUTES);

3.4 获取List缓存全部内容(起始索引,结束索引)

List listKey1 = redisTemplate.boundListOps("listKey").range(0, 10); 

3.5 从左或从右弹出一个元素

//从左侧弹出一个元素
String listKey2 = (String) redisTemplate.boundListOps("listKey").leftPop();  
//从右侧弹出一个元素
String listKey3 = (String) redisTemplate.boundListOps("listKey").rightPop(); 

3.6 根据索引查询元素

String listKey4 = (String) redisTemplate.boundListOps("listKey").index(1);

3.7 获取List缓存的长度

Long size = redisTemplate.boundListOps("listKey").size();

3.8 根据索引修改List中的某条数据(key,索引,值)

redisTemplate.boundListOps("listKey").set(3L,"listLeftValue3");

3.9 移除N个值为value(key,移除个数,值)

redisTemplate.boundListOps("listKey").remove(3L,"value");

4. Set命令

4.1 添加Set缓存(值可以是一个,也可是多个)

//1、通过redisTemplate设置值
redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");

//2、通过BoundValueOperations设置值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
setKey.add("setValue1", "setValue2", "setValue3");

//3、通过ValueOperations设置值
SetOperations setOps = redisTemplate.opsForSet();
setOps.add("setKey", "SetValue1", "setValue2", "setValue3");

4.2 设置过期时间(单独设置)

redisTemplate.boundValueOps("setKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("setKey",1,TimeUnit.MINUTES);

4.3 根据key获取Set中的所有值

//1、通过redisTemplate获取值
Set set1 = redisTemplate.boundSetOps("setKey").members();

//2、通过BoundValueOperations获取值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
Set set2 = setKey.members();

//3、通过ValueOperations获取值
SetOperations setOps = redisTemplate.opsForSet();
Set set3 = setOps.members("setKey");

4.4 根据value从一个set中查询,是否存在

Boolean isEmpty = redisTemplate.boundSetOps("setKey").isMember("setValue2");

4.5 获取Set缓存的长度

Long size = redisTemplate.boundSetOps("setKey").size();

4.6 移除指定的元素

Long result1 = redisTemplate.boundSetOps("setKey").remove("setValue1");

4.7 移除指定的key

Boolean result2 = redisTemplate.delete("setKey");

4.8 交集 并集 差集

//	交集 返回存储在 “key1” 和 “key2” 中的集合的交集。
Set<Object> intersect = redisTemplate.opsForSet().intersect("key1", "key2");
//	并集 返回存储在 “key1” 和 “key2” 中的集合的并集。
Set<Object> union = redisTemplate.opsForSet().union("key1", "key2");
//	差集 返回存储在 “key1” 中但不在 “key2” 中的集合,即差集。
Set<Object> difference = redisTemplate.opsForSet().difference("key1", "key2");

5. ZSet命令

5.1 向集合中插入元素,并设置分数

//1、通过redisTemplate设置值
redisTemplate.boundZSetOps("zSetKey").add("zSetVaule", 100D);

//2、通过BoundValueOperations设置值
BoundZSetOperations zSetKey = redisTemplate.boundZSetOps("zSetKey");
zSetKey.add("zSetVaule", 100D);

//3、通过ValueOperations设置值
ZSetOperations zSetOps = redisTemplate.opsForZSet();
zSetOps.add("zSetKey", "zSetVaule", 100D);

5.2 向集合中插入多个元素,并设置分数

DefaultTypedTuple<String> p1 = new DefaultTypedTuple<>("zSetVaule1", 2.1D);
DefaultTypedTuple<String> p2 = new DefaultTypedTuple<>("zSetVaule2", 3.3D);
redisTemplate.boundZSetOps("zSetKey").add(new HashSet<>(Arrays.asList(p1,p2)));

5.3 按照排名先后(从小到大)打印指定区间内的元素, -1为打印全部

Set<String> range = redisTemplate.boundZSetOps("zSetKey").range(0, -1);

5.4 获得指定元素的分数

Double score = redisTemplate.boundZSetOps("zSetKey").score("zSetVaule");

5.5 返回集合内的成员个数

Long size = redisTemplate.boundZSetOps("zSetKey").size();

5.6 返回集合内指定分数范围的成员个数(Double类型)

Long COUNT = redisTemplate.boundZSetOps("zSetKey").count(0D, 2.2D);

5.7 返回集合内元素在指定分数范围内的排名(从小到大)

Set byScore = redisTemplate.boundZSetOps("zSetKey").rangeByScore(0D, 2.2D);

5.8 带偏移量和个数,(key,起始分数,最大分数,偏移量,个数)

Set<String> ranking2 = redisTemplate.opsForZSet().rangeByScore("zSetKey", 0D, 2.2D 1, 3);

5.9 返回集合内元素的排名,以及分数(从小到大)

Set<TypedTuple<String>> tuples = redisTemplate.boundZSetOps("zSetKey").rangeWithScores(0L, 3L);
  for (TypedTuple<String> tuple : tuples) {
      System.out.println(tuple.getValue() + " : " + tuple.getScore());
  }

5.10 返回指定成员的排名

//从小到大
Long startRank = redisTemplate.boundZSetOps("zSetKey").rank("zSetVaule");
//从大到小
Long endRank = redisTemplate.boundZSetOps("zSetKey").reverseRank("zSetVaule");

5.11 从集合中删除指定元素

redisTemplate.boundZSetOps("zSetKey").remove("zSetVaule");

5.12 删除指定索引范围的元素(Long类型)

redisTemplate.boundZSetOps("zSetKey").removeRange(0L,3L);

5.13 删除指定分数范围内的元素(Double类型)

redisTemplate.boundZSetOps("zSetKey").removeRangeByScorssse(0D,2.2D);

5.14 为指定元素加分(Double类型)

Double score = redisTemplate.boundZSetOps("zSetKey").incrementScore("zSetVaule",1.1D);



在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值