redis基本操作
Posted 何波安的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis基本操作相关的知识,希望对你有一定的参考价值。
Redis的字符串以及过期时间操作
> set username heboan # 设置值 OK > get username #获取值 "heboan"
> set sayhi ‘hello world‘ OK > get sayhi "hello world"
> del username (integer) 1 > get username (nil) 127.0.
#方法1 > set username heboan EX 10 #10秒后过期 OK #方法2 > setex username 10 heboan OK #方法3 > set username heboan OK > expire username 10 (integer) 1
查看当前redis的所有key
> keys *
redis的列表操作
> lpush books python php (integer) 2 > lpush books java (integer) 3 > lrange books 0 -1 #查看此列表所有元素 1) "java" 2) "php" 3) "python" #["java", "php", "pyton"]
> rpush books c++ golang (integer) 5 > lrange books 0 -1 1) "java" 2) "php" 3) "python" 4) "c++" 5) "golang" #["java", "php", "python", "c++", "golang"]
#移除并返回左边的第一个元素 > lpop books "java" #移除并返回右边的第一个元素 > rpop books "golang"
127.0.0.1:6379> lrange books 0 -1 1) "php" 2) "python" 3) "c++" > llen books (integer) 3
> lindex books 0 "php" > lindex books 1 "python" > lindex books 2 "c++" > lindex books 3 #超过返回空 (nil)
> lrange books 0 -1 1) "php" 2) "php" 3) "golang" 4) "python" 5) "c++" 6) "python" 7) "php" 8) "java" #count > 0:从表头开始向表尾搜索,移除与value相等的元素,数量为count #删除2个php > lrem books 2 php (integer) 2 > lrange books 0 -1 1) "golang" 2) "python" 3) "c++" 4) "python" 5) "php" 6) "java" #count < 0:从表尾开始向表头搜索,移除与 value相等的元素,数量为count的绝对值 #删除1个python > lrem books -1 python (integer) 1 > lrange books 0 -1 1) "golang" 2) "python" 3) "c++" 4) "php" 5) "java" #count = 0:移除表中所有与value 相等的值
redis的集合操作
集合和列表相比:1、集合不拥有重复的元素 2、集合是无序的,没有位置关系
> sadd school_1 qinghua beida (integer) 2
> smembers school_1 1) "beida" 2) "qinghua"
> sadd school_1 fudan (integer) 1 smembers school_1 1) "beida" 2) "fudan" 3) "qinghua" > srem school_1 fudan #删除元素fudan (integer) 1 > smembers school_1 1) "beida" 2) "qinghua"
> smembers school_1 1) "beida" 2) "qinghua" > scard school_1 (integer) 2
#交集就是取出集合之间共同拥有的元素 > smembers school_1 1) "beida" 2) "qinghua" > smembers school_2 1) "fudan" 2) "qinghua" 3) "wangyi" > sinter school_1 school_2 1) "qinghua"
#并集就是取出集合的所有元素,重复的元素只保留1个,因为集合不能有重复的元素 > smembers school_1 1) "beida" 2) "qinghua" > smembers school_2 1) "fudan" 2) "qinghua" > sunion schoo_1 school_2 1) "fudan" 2) "wangyi" 3) "qinghua"
> smembers school_1 1) "beida" 2) "qinghua" > smembers school_2 1) "fudan" 2) "qinghua" 3) "wangyi" > sdiff school_1 school2 #把school_2中不含有schoo_1中的元素去掉 1) "beida" 2) "qinghua" > sdiff school_2 school1 #把school_1中不含有schoo_2中的元素去掉 1) "fudan" 2) "wangyi" 3) "qinghua"
redis的哈希操作
..
以上是关于redis基本操作的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程