首页 > 其他分享 >关于ReadisTemplate的Api

关于ReadisTemplate的Api

时间:2022-10-22 01:22:05浏览次数:40  
标签:String ValueOperations HashKey Api 关于 key ReadisTemplate setKey redisTemplate

ValueOperations:简单K-V操作

SetOperations:set类型数据操作

ZSetOperations:zset类型数据操作

HashOperations:针对map类型的数据操作

ListOperations:针对list类型的数据操作

通过bound封装指定的key

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);
————————————————
//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");

基本的API

redisTemplate.delete(keys);
//指定key的失效时间
redisTemplate.expire(key,time,TimeUnit.MINUTES);
//根据key获取过期时间
Long expire = redisTemplate.getExpire(key);
//判断key是否存在
redisTemplate.hasKey(key);
//顺序递增
redisTemplate.boundValueOps("StringKey").increment(3L);
//顺序递减
redisTemplate.boundValueOps("StringKey").increment(-3L);

关于Hashl类型的操作

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");
添加一个Map集合
HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap);
提取Hash中的小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");
根据Hash的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");
获取所有的键值对集合
//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");
删除
//删除小key
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
//删除大key
redisTemplate.delete("HashKey");
判断Hash中是否含有该值
Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

Set类型相关操作

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");
根据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");
获取Set的长度
Long size = redisTemplate.boundSetOps("setKey").size();

标签:String,ValueOperations,HashKey,Api,关于,key,ReadisTemplate,setKey,redisTemplate
From: https://www.cnblogs.com/xuzhidong/p/16815175.html

相关文章

  • 关于sleep和定时器
    平时使用sleep多一些,如缓冲满了,等一会再送。while(缓冲满了){sleep(MS)};某个任务,20毫秒执行一次, while(TRUE){ 做任务(用了1毫秒),sleep(18,19毫秒)};几乎很少使用定时器;也感觉不出......
  • tf.api
    API列表Dataset基础使用tf.data.Dataset.from_tensor_slices这个api构建Dataset在这个Dataset上具体调用repeat(重复多少次),batch,interleave,map,shuffle,li......
  • Opengl___API解读
    glLoadIdentity()转换为没有进行矩阵变换的状态。即对角线为1的单位矩阵。OpenGL函数思考-glLoadIdentityglPushMatrix()和glPopMatrix()glPushMatrix();对之前的矩阵......
  • 关于负数补码为什么原码是按位取反再+1
     8位下,求123和-123的补码。8位补码表示的值为-128-127。[-123]补码=[-01111011]补码=2^8+(-01111011)......
  • 一个关于树的有趣结论
    从这里看到的.假设我们有一棵\(n\)个点的树(边权都为\(1\)),我们随便选取一个点\(r\)作为根.记\(d_u\)为点\(u\)的度数,\(\text{dis}(u,v)\)为\(u\)到......
  • 微服务架构学习与思考(11):开源 API 网关02-以 Java 为基础的 API 网关详细介绍
    微服务架构学习与思考(11):开源API网关02-以Java为基础的API网关详细介绍上一篇关于网关的文章:微服务架构学习与思考(10):微服务网关和开源API网关01-以Nginx为......
  • Apifox
    1、Apifox定位Apifox=Postman+Swagger+Mock+JMeterApifox是API文档、API调试、APIMock、API自动化测试一体化协作平台。通过一套系统、一份数据,解决多个......
  • 关于SX1262IMLTRT、SX1261、SX1268选型指南分析对比
    今天这个篇幅我们继续来探讨下LoRa领域的相关芯片,Semtech公司推出的SX1261/62/68系列的芯片,不知道各位朋友有没有了解过这系列芯片,一开始推出市场,就是打着特殊的LoRa调制方......
  • 关于LoRa常见问题解答
    前面的文章中,小编有写过LORA技术低功耗广域网络,在大大小小的行业活动中,一直都是物联网的一个热门话题。LoRa主要在全球免费频段运行,包括433、868、915MHz等。后来有很多......
  • .net core 配置Swagger 摆脱PostMan,你值得拥有这样的api调试方式
    废话不多说直接来看第一步:安装nuget包:Swashbuckle.AspNetCore.Swagger            Swashbuckle.AspNetCore.SwaggerGen         ......