首页 > 数据库 >redisTemplate String增删改查

redisTemplate String增删改查

时间:2023-03-01 10:03:25浏览次数:65  
标签:operations set String 改查 expire key wcLock redisTemplate


文章目录

  • ​​加锁​​
  • ​​expire​​
  • ​​获取值​​
  • ​​例子代码​​
  • ​​附录​​
  • ​​其他​​
  • ​​让锁过期,如何操作​​

加锁

@ResponseBody
@RequestMapping("/lock")
public String lock(){
ValueOperations operations = stringRedisTemplate.opsForValue();
String wcLock = (String)operations.get(WC_LOCK);

if(!StringUtils.isEmpty(wcLock)){

logger.info("wcLock已经存在");
return "wcLock已经存在";
}else{
// key value timeout 时间单位
operations.set(WC_LOCK,"1",60, TimeUnit.SECONDS);
return "wcLock设置锁成功";
}


}

注:超期时间要 >0 否则会报错。

expire

@ResponseBody
@RequestMapping("/expire")
public String expire(){
ValueOperations operations = stringRedisTemplate.opsForValue();

operations.set(WC_LOCK,"1",1L, TimeUnit.SECONDS);
return "expire success";
}

获取值

@ResponseBody
@RequestMapping("/get")
public String get(String string){
ValueOperations operations = stringRedisTemplate.opsForValue();
String wcLock = (String)operations.get(string);
Long expire = operations.getOperations().getExpire(string); // 过期时间的获取
logger.info("key: {},timeout is: {}",expire);
return wcLock;

}

例子代码

代码:

@RestController
@RequestMapping("")
public class LoginController {

@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/login")
public String login(HttpServletRequest request){

ValueOperations valueOperations = stringRedisTemplate.opsForValue();

valueOperations.set("shanghai","shanghai"); // 新增
valueOperations.set("hebei","shijiazhuang2"); // 存在的话就是修改
valueOperations.get("hebei");
Set<String> keys = stringRedisTemplate.keys("*"); // keys * 查看所有
for (String key:
keys) {
System.out.println(keys);
}
stringRedisTemplate.delete("one"); // 删除
return "fail";
}
}

附录

Key Type Operations:

接口

描述

GeoOperations

Redis geospatial operations like ​​GEOADD​​​, ​​GEORADIUS​​,…​)

HashOperations

Redis hash operations

HyperLogLogOperations

Redis HyperLogLog operations like (​​PFADD​​​, ​​PFCOUNT​​,…​)

ListOperations

Redis list operations

SetOperations

Redis set operations

ValueOperations

Redis string (or value) operations

ZSetOperations

Redis zset (or sorted set) operations

Key Bound Operations:

接口

描述

BoundGeoOperations

Redis key bound geospatial operations.

BoundHashOperations

Redis hash key bound operations

BoundKeyOperations

Redis key bound operations

BoundListOperations

Redis list key bound operations

BoundSetOperations

Redis set key bound operations

BoundValueOperations

Redis string (or value) key bound operations

BoundZSetOperations

Redis zset (or sorted set) key bound operations

官网文档地址:
​​​https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/#redis:template​

其他

让锁过期,如何操作

1、直接删除该redis缓存。
2、手动设置超时时间1秒,马上就过期了。


标签:operations,set,String,改查,expire,key,wcLock,redisTemplate
From: https://blog.51cto.com/u_7341513/6092934

相关文章

  • (转)GoLang之标准库strings包
    原文:https://blog.csdn.net/weixin_52690231/article/details/123593614?ops_request_misc=&request_id=&biz_id=102&utm_term=golang%20strings%20%E5%8C%85%20%E8%AF%A6%......
  • (转)一文了解 Go 标准库 strings 常用函数和方法
    原文:https://blog.csdn.net/weixin_44604586/article/details/128104981?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYu......
  • 使用StringRedisTemplate实现redis分布式锁
    背景:单个接口可能同时被多个用户调用,但是每个用户使用的数据都是不一样,因此需要使用分布式锁解决数据减少了没有即使减少的问题使用的指令来自的edis的setnx命令,setnx(k,v......
  • 字符串中的slice(),substr(),substring()三种提取字符串的方法总结
    1、slice()slice()方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。语法:str.slice(start,end),截取str从start到end的所有字符(包含起始位置,不包含结束位置)说明:st......
  • C++string大小写转换
    #include<iostream>#include<string>#include<algorithm>usingnamespacestd;intmain(){stringstr="ancdANDG";cout<<"转换前的字符串:"<<str......
  • geotools:LineString转SimpleFeature
    publicstaticList<SimpleFeature>toSimpleFeature(List<SimpleFeature>simpleFeatureList){List<SimpleFeature>resultList=newArrayList<>();......
  • 数据的增删改查
    1、数据的增(Insert):数据的增也就是数据的插入,基本语法如下: InsertIntoTable_Name(column1,column2,column3,…)Values(值1,值2,值3,…);比如我们向St......
  • android string.xml文件中的整型和string型代替
    在android的开发中,经常会遇见一句话,比如“我今年23岁了”;这个23需要在程序中生成,但是遇到一个问题,这完整的一句话是一个TextView中的,而不是三个textView拼接成的,而且是引用......
  • [转]Redis干货 | 五种常用类型之String字符串详解
    一.背景说明小白:伟哥,java中String是最常用类型,Redis中也是吗?伟哥:差不多,我给你稍微讲一下。二.数据类型依据Redis官网,目前Redis数据类型共计九种。具体整理如下:......
  • TypeScript String(字符串)
    TypeScriptString(字符串)String对象用于处理文本(字符串)。语法vartxt=newString("string");或者更简单方式:vartxt="string";String对象属性下表列出了Stri......