------------------------------------------ 字符串基本设置 追加 127.0.0.1:6379> keys * 1) "age" 2) "key1" 127.0.0.1:6379> exists key1 # 是否存在 (integer) 1 127.0.0.1:6379> append key1 "hello" # 追加字符串 (integer) 7 127.0.0.1:6379> get key1 "v1hello" 127.0.0.1:6379> strlen key1 # 查看key得长度 (integer) 7 127.0.0.1:6379> append key1 ",jsxs" #追加字符串 (integer) 12 127.0.0.1:6379> keys * 1) "age" 2) "key1" 127.0.0.1:6379> append jsxs aaa #追加字符串,假如说key值不存在,那么就添加此字符串 (integer) 3 127.0.0.1:6379> keys * 1) "age" 2) "jsxs" 3) "key1" ------------------------------------------ 自增变量 127.0.0.1:6379> set views 0 # 初始浏览量为0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views #字符串自增1 (integer) 1 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> decr views #字符串自减1 (integer) 1 127.0.0.1:6379> decr views (integer) 0 127.0.0.1:6379> INCRBY views 10 # 设置自增10 (integer) 10 127.0.0.1:6379> INCRBY views 10 (integer) 20 127.0.0.1:6379> DECRBY views 5 # 自减5 (integer) 15 ------------------------------------------ 字符串范围 range 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set key "hello,jsxs" OK 127.0.0.1:6379> get key "hello,jsxs" 127.0.0.1:6379> GETRANGE key 0 3 # 截取字符串 [0,3] "hell" 127.0.0.1:6379> GETRANGE key 0 -1 # 获得全部字符串得信息 "hello,jsxs" ------------------------------------------ 替换指定位置得字符串 127.0.0.1:6379> get key2 "abcdsefs" 127.0.0.1:6379> SETRANGE key2 1 xx # 替换key2值中 index为1开始的数据 为xx (integer) 8 127.0.0.1:6379> get key2 "axxdsefs" ------------------------------------------ setex (set with expire) | 存在的前提下设置过期时间 setnx (set if not exist) | 不存在的前提下再设置 (分布式锁中会常常使用) 127.0.0.1:6379> SETEX key3 30 hello # 假如存在设置过期时间 OK 127.0.0.1:6379> ttl key3 # 查看秒数 (integer) 23 127.0.0.1:6379> SETNX mykey redis #假如不存在就设置 0失败 1成功 (integer) 1 127.0.0.1:6379> keys * 1) "key" 2) "mykey" 3) "key2" 127.0.0.1:6379> ttl key3 (integer) -2 127.0.0.1:6379> setnx mykey mongodb # 假如不存在就设置 0失败 (integer) 0 127.0.0.1:6379> get mykey "redis" ------------------------------------------ 批量设置键值对 | 批量获取键值对 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3 # 批量设置键值对 OK 127.0.0.1:6379> keys * 1) "k3" 2) "k2" 3) "k1" 127.0.0.1:6379> MGET k1 k2 k3 #批量获取键值对 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> MSETNX k1 v1 k4 v4 #批量设置setnx 遵从原子性 (integer) 0 127.0.0.1:6379> keys * 1) "k3" 2) "k2" 3) "k1" ------------------------------------------ 对象 set user:1{name:jsxs,age:3} # 设置一个user:1对象 值为json字符串来保存一个对象 这里的key是一个巧妙地设计: user:{id}:{files},如此设计再redis中是完全ok了. 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> mset user:1:name jsxs user:1:age 26 OK 127.0.0.1:6379> MGET user:1:name user:1:age 1) "jsxs" 2) "26" ------------------------------------------ getset #先get然后set 127.0.0.1:6379> getset db mysql #getset先get后set (nil) 127.0.0.1:6379> get db "mysql" 127.0.0.1:6379> getset db moguodb "mysql" 127.0.0.1:6379> get db "moguodb"
String中的value除了是字符串还可以是数字,它更加适合字符串存储,应用场景举例:
- 计数器
- 统计多单位的数量
- 粉丝数
- 对象存储缓存
3、List (列表)
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。如下Redis中List是可以进行双端操作的,所以命令也就分为了LXXX和RLLL两类。
头部是左边,头部的索引值为0.尾部是右边,尾部的索引值是list.size
栈: 只在一边进和出。 队列: 一边进另一边出。
在redis里面我们呢可以把List玩成 栈和队列。所有地list命令都是以 L 开头地。
------------------------------------------ 向头部插入一个值和向尾部插入一个值 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> LPUSH list one # 将一个值或多个值,从头部插进去 (integer) 1 127.0.0.1:6379> LPUSH list two (integer) 2 127.0.0.1:6379> LPUSH list three (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 #通过区间获得所有值 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> LRANGE list 0 1 1) "three" 2) "two" 127.0.0.1:6379> RPUSH list four # 向尾部插入一个值 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "four" 127.0.0.1:6379> ------------------------------------------ 移除一个值 127.0.0.1:6379> LRANGE list 0 -1 # 通过范围获得所有值 1) "three" 2) "two" 3) "one" 4) "four" 127.0.0.1:6379> LPOP list # 移除左边地第一个 "three" 127.0.0.1:6379> RPOP list #移除右边地第一个 "four" 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" ------------------------------------------ Lindex 通过游标获取值 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" 127.0.0.1:6379> LINDEX list 1 # 查看游标为1的值 "one" 127.0.0.1:6379> LINDEX list 0 #查看游标为0的值 "two" ------------------------------------------ Llen 查看链表的长度 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> LPUSH list one # 向key为list的值放进一个值 (integer) 1 127.0.0.1:6379> LPUSH list two (integer) 2 127.0.0.1:6379> LPUSH list three (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> Llen list # 查看指定链表的长度 (integer) 3 ------------------------------------------ 移除指定的值 127.0.0.1:6379> Llen list (integer) 3 127.0.0.1:6379> LREM list 1 two # 移除最近添加的1个two (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "one" 127.0.0.1:6379> LPUSH list three (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "three" 3) "one" 127.0.0.1:6379> LREM list 2 three # 移除最近添加的2个three (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "one" ------------------------------------------ 截取一个值的范围 Ltrim -》保留 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> LPUSH mylist "hello" (integer) 1 127.0.0.1:6379> LPUSH mylist "hello1" (integer) 2 127.0.0.1:6379> LPUSH mylist "hello13" (integer) 3 127.0.0.1:6379> LRANGE mylist 0 -1 1) "hello13" 2) "hello1" 3) "hello" 127.0.0.1:6379> LPUSH mylist "hello12" (integer) 4 127.0.0.1:6379> LTRIM mylist 0 1 # 保留从0到1的字段 OK 127.0.0.1:6379> LRANGE mylist 0 -1 1) "hello12" 2) "hello13" ------------------------------------------ repoplpush #移除列表的最后一个元素并让其添加到一个新列表中 127.0.0.1:6379> LPUSH list "hello" (integer) 1 127.0.0.1:6379> LPUSH list "hello1" (integer) 2 127.0.0.1:6379> LPUSH list "hello2" (integer) 3 127.0.0.1:6379> LPUSH list "hello3" (integer) 4 127.0.0.1:6379> RPOPLPUSH list myotherlist # 移除最右边的元素并移动到新的位置 "hello" 127.0.0.1:6379> LRANGE list 0 -1 1) "hello3" 2) "hello2" 3) "hello1" 127.0.0.1:6379> LRANGE myotherlist 0 -1 1) "hello" ------------------------------------------ lset # 向指定列表的指定位置替换成某个值,前提是存在这个索引 127.0.0.1:6379> EXISTS list (integer) 0 127.0.0.1:6379> lset list 0 item # 如果不存在索引就会报错 (error) ERR no such key 127.0.0.1:6379> lpush list hello1 (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "hello1" 127.0.0.1:6379> lset list 0 item # 向0索引处替换成某个值 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "item" 127.0.0.1:6379> lset list 1 item2 (error) ERR index out of range ------------------------------------------ linsert #向指定元素前/或元素后 添加指定元素 127.0.0.1:6379> LPUSH list 0 (integer) 1 127.0.0.1:6379> LPUSH list 1 (integer) 2 127.0.0.1:6379> LPUSH list 2 (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "2" 2) "1" 3) "0" 127.0.0.1:6379> LINSERT list before "2" hello #向指定元素前加入某个值 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "hello" 2) "2" 3) "1" 4) "0" 127.0.0.1:6379> LINSERT list after "2" hello #向指定元素后加入某个值 (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "hello" 2) "2" 3) "hello" 4) "1" 5) "0"
小结:
- list实际上是一个链表,before after , left, right都可以插入值
- 如果key不存在,则创建新的链表
- 如果key存在,则表示新增内容
- 如果移除了所有值,那么list就变成了空链表,也代表不存在
- 对于链表,在两边插入或者改动值,效率最高!修改中间元素,效率相对较低
4、Set (集合)
set中的值是无序不重复集合。
127.0.0.1:6379> sadd myset "hello" # set 集合中添加元素 (integer) 1 127.0.0.1:6379> sadd myset "jsxs" (integer) 1 127.0.0.1:6379> sadd myset "love jsxs" (integer) 1 127.0.0.1:6379> SMEMBERS myset # 查看set的所有制 1) "love jsxs" 2) "jsxs" 3) "hello" 127.0.0.1:6379> SISMEMBER myset hello # 判断一个值是否在-集合中 (integer) 1 127.0.0.1:6379> SISMEMBER myset jsd (integer) 0 ------------------------------------------ # scard 查看链表的个数 127.0.0.1:6379> sadd myset "love jsxs" # 不能重复,重复报0 (integer) 0 127.0.0.1:6379> sadd myset "love jsxs2" # (integer) 1 127.0.0.1:6379> SCARD myset #查看链表的个数 (integer) 4 ------------------------------------------ 移除某一个元素 srem 127.0.0.1:6379> SCARD myset (integer) 4 127.0.0.1:6379> srem myset hello # 移除某一个元素 (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "love jsxs2" 2) "love jsxs" 3) "jsxs" ------------------------------------------ 随机数: 随机选取一个set值 127.0.0.1:6379> SMEMBERS myset 1) "love jsxs2" 2) "love jsxs" 3) "jsxs" 127.0.0.1:6379> SRANDMEMBER myset #随机抽选出一个元素 "jsxs" 127.0.0.1:6379> SRANDMEMBER myset "jsxs" 127.0.0.1:6379> SRANDMEMBER myset "jsxs" 127.0.0.1:6379> SRANDMEMBER myset "love jsxs2" 127.0.0.1:6379> SRANDMEMBER myset "jsxs" 127.0.0.1:6379> SRANDMEMBER myset "love jsxs2" 127.0.0.1:6379> SRANDMEMBER myset "love jsxs" 127.0.0.1:6379> SRANDMEMBER myset "jsxs" 127.0.0.1:6379> SRANDMEMBER myset 2 #随机抽选处指定元素的个数 1) "love jsxs2" 2) "jsxs" ------------------------------------------ 随机移除一个set值 127.0.0.1:6379> clear 127.0.0.1:6379> SMEMBERS myset 1) "love jsxs2" 2) "love jsxs" 3) "jsxs" 127.0.0.1:6379> SPOP myset #随机移除一个set值 "jsxs" 127.0.0.1:6379> SPOP myset "love jsxs2" 127.0.0.1:6379> SMEMBERS (error) ERR wrong number of arguments for 'smembers' command 127.0.0.1:6379> SMEMBERS myset 1) "love jsxs" ------------------------------------------ 将一个值移动到另一个set 集合中 127.0.0.1:6379> sadd myset "hello" (integer) 1 127.0.0.1:6379> sadd myset "hello1" (integer) 1 127.0.0.1:6379> sadd myset "hello2" (integer) 1 127.0.0.1:6379> sadd myset "hello3" (integer) 1 127.0.0.1:6379> sadd myset2 "0" (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "hello3" 2) "hello1" 3) "hello2" 4) "hello" 127.0.0.1:6379> SMEMBERS myset2 1) "0" 127.0.0.1:6379> SMOVE myset myset2 "hello" #将myset集合中的set2移动到myset2 (integer) 1 127.0.0.1:6379> SMEMBERS myset2 1) "0" 2) "hello" 127.0.0.1:6379> SMEMBERS myset 1) "hello3" 2) "hello1" 3) "hello2" ------------------------------------------ 微博,B站,共同关注(并集) 127.0.0.1:6379> sadd key1 a (integer) 1 127.0.0.1:6379> sadd key1 b (integer) 1 127.0.0.1:6379> sadd key1 c (integer) 1 127.0.0.1:6379> sadd key2 c (integer) 1 127.0.0.1:6379> sadd key2 d (integer) 1 127.0.0.1:6379> sadd key2 e (integer) 1 127.0.0.1:6379> SDIFF key1 key2 # 差集 1) "b" 2) "a" 127.0.0.1:6379> SINTER key1 key2 #交集 1) "c" 127.0.0.1:6379> SUNION key1 key2 #并集 1) "d" 2) "c" 3) "a" 4) "b" 5) "e"
微博,A用户将所有关注的人放在一个set集合中!将他的粉丝也放在一个集合中,实现共同关注模块,二度好友**(六度分隔理论-推荐好友)**。