SpringBoot StringRedisTemplate(Redis)的使用

本文详细介绍了使用Java与SpringDataRedis进行字符串(string),集合(set),哈希(hash)和列表(list)操作的方法,包括获取/设置值、过期时间管理、批量操作以及各种特有功能如随机成员选取等。
摘要由CSDN通过智能技术生成
//获取key的过期时间
stringRedisTemplate.getExpire("name", TimeUnit.SECONDS);
//判断key是否存在
stringRedisTemplate.hasKey("name");
//给定一个指定的key,附加过期时间
stringRedisTemplate.expire("name", 20, TimeUnit.MINUTES);
//移除key的指定过期时间
stringRedisTemplate.boundValueOps("key").persist();

//string类型

//获取key的值
stringRedisTemplate.opsForValue().get("key");
//将值放入缓存,Duration类是用来表示时间段的,通常是以秒和纳秒为单位的时间跨度,ofMillis创建一个1分钟Duration对象
stringRedisTemplate.opsForValue().set("name", "maggie", Duration.ofMillis(1));
//批量添加key(键重复会覆盖)
Map<String, String> map = new HashMap<>();
map.put("age", "21");
map.put("phone", "12345");
stringRedisTemplate.opsForValue().multiSet(map);
//批量添加key-value,只有键不存在时,才会添加(存在key重复,则一条都不添加)
stringRedisTemplate.opsForValue().multiSetIfAbsent(map);

//set类型

//将数据放入set缓存
stringRedisTemplate.opsForSet().add("name", "zy", "zz", "yy");
//根据key获取值
stringRedisTemplate.opsForSet().members("name");
//获取变量中指定个数的元素(随机)
stringRedisTemplate.opsForSet().randomMembers("name", 2L);
//随机获取变量中的元素
stringRedisTemplate.opsForSet().randomMember("name");
//弹出变量中的元素(随机弹出一个元素,并从集合中移除)
stringRedisTemplate.opsForSet().pop("name");
//获取指定key中的集合长度
stringRedisTemplate.opsForSet().size("name");
//判断指定key中值是否存在
stringRedisTemplate.opsForSet().isMember("name", "maggie");
stringRedisTemplate.opsForSet().isMember("name", (Object) "maggie");
//转移变量的元素(将指定key的元素转移到另一个key中)
stringRedisTemplate.opsForSet().move("name", "maggie", "username");
//批量移除set缓存中的元素
stringRedisTemplate.opsForSet().remove("name", "1", "2");

//hash

//添加缓存
Map map1 = new HashMap();
map1.put("a","a");
map1.put("b","b");
map1.put("c","c");
stringRedisTemplate.opsForHash().putAll("name", map1);
//获取key值
stringRedisTemplate.opsForHash().entries("name");
//判断key有没有指定的hashKey
stringRedisTemplate.opsForHash().hasKey("name", "a");
//获取指定key的hashkey的值(如果key不存在,hashkey不存在 则返回null)
stringRedisTemplate.opsForHash().get("name", "a");
//获取指定hashkey的值的int(值的长度,一个中文三个长度)
stringRedisTemplate.opsForHash().lengthOfValue("name", "a");
//删除元素并返回受影响的行数
stringRedisTemplate.opsForHash().delete("name", "a", "b");
//删除指定的key
stringRedisTemplate.opsForHash().delete("name", "a");
//获取key下所有haskey字段
stringRedisTemplate.opsForHash().keys("name");
//获取指定hash下面的键值对数量
stringRedisTemplate.opsForHash().size("name");

//list

//在变量左边添加元素
stringRedisTemplate.opsForList().leftPush("name","maggie");
//获取集合指定位置的值
stringRedisTemplate.opsForList().index("name",1);
//获取指定区间的值
stringRedisTemplate.opsForList().range("name",1,2);
//把最后一个参数值放到指定集合的第一个出现参数的前面
stringRedisTemplate.opsForList().leftPush("name","zy","zz");
//向左边批量添加参数元素
stringRedisTemplate.opsForList().leftPushAll("name","a","b");
//向集合最右边添加元素
stringRedisTemplate.opsForList().rightPush("name","a");
//向集合右边批量添加元素
stringRedisTemplate.opsForList().rightPushAll("name","q","w","w");
//向已存在的集合中添加元素
stringRedisTemplate.opsForList().rightPushIfPresent("name","maggie");
//获取集合长度
stringRedisTemplate.opsForList().size("name");
//移除集合中的左边第一个元素
stringRedisTemplate.opsForList().leftPop("name");
//移除集合中左边元素在等待时间
stringRedisTemplate.opsForList().leftPop("name",Duration.ofMinutes(1));
//移除集合中右边的元素
stringRedisTemplate.opsForList().leftPop("name");
//移除集合中右边的元素在等待的时间里
stringRedisTemplate.opsForList().rightPop("name",Duration.ofMinutes(1));

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MaGgIeOo0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值