五种数据类型: 字符串(String) 字符串列表(list) 有序字符串集合(sorted set) 哈希(hash) 字符串集合(set) Key定义的注意点: 不要过长,不要过短,统一的命名规范
2. Redis数据结构之字符串类型
赋值 127.0.0.1:6379> set name andrew OK 取值 127.0.0.1:6379> get name "andrew" 获取并设置值 127.0.0.1:6379> getset name tony "andrew" 127.0.0.1:6379> get name "tony" 删除 127.0.0.1:6379> del name (integer)1 127.0.0.1:6379> get name (nil) 自增incr,假如没有定义则默认0,假如非数值类型则报错 127.0.0.1:6379> incr n1 (integer) 1 127.0.0.1:6379> get n1 "1" 127.0.0.1:6379> set n2 2 OK 127.0.0.1:6379> incr n2 (integer) 3 127.0.0.1:6379> get n2 "3" 127.0.0.1:6379> set n3 andrew OK 127.0.0.1:6379> incr n3 (error) ERR value is not an integer or out of range 自减decr 127.0.0.1:6379> get n2 "3" 127.0.0.1:6379> decr n2 (integer) 2 127.0.0.1:6379> decr n4 (integer) -1 指定增量值incrby 127.0.0.1:6379> get n4 "-1" 127.0.0.1:6379> incrby n4 8 (integer) 7 指定减量值decrby 127.0.0.1:6379> get n4 "7" 127.0.0.1:6379> decrby n4 5 (integer) 2 追加字符串append 假如没定义,直接赋值 127.0.0.1:6379> append s1 str (integer) 3 127.0.0.1:6379> get s1 "str" 127.0.0.1:6379> append s1 abc (integer) 6 127.0.0.1:6379> get s1 "strabc" 删除多个 127.0.0.1:6379> keys * 1) "n1" 2) "n2" 3) "s1" 4) "n3" 5) "n4" 127.0.0.1:6379> del n1 n2 n3 n4 (integer) 4 127.0.0.1:6379> keys * 1) "s1"
3. Redis数据结构之哈希类型
存储Hash String key和String Value的Map容器,每一个Hash可以存储4294967295个键值对 存储Hash常用命令: 赋值和取值 127.0.0.1:6379> hset h1 username andrew (integer) 1 127.0.0.1:6379> hset h1 password 123456 (integer) 1 127.0.0.1:6379> hget h1 username "andrew" 127.0.0.1:6379> hget h1 password "123456" 赋值和取值(多个字段一起) 127.0.0.1:6379> hmset h2 username tony password 654321 OK 127.0.0.1:6379> hmget h2 username 1) "tony" 127.0.0.1:6379> hmget h2 password 1) "654321" 127.0.0.1:6379> hmget h2 username password 1) "tony" 2) "654321" 获取所有 127.0.0.1:6379> hgetall h2 1) "username" 2) "tony" 3) "password" 4) "654321" hdel删除属性,可以一次删除一个或者多个 127.0.0.1:6379> hdel h2 username password (integer) 2 127.0.0.1:6379> hgetall h2 (empty list or set) hincrby增加数字 127.0.0.1:6379> hset h1 age 20 (integer) 1 127.0.0.1:6379> hincrby h1 age 5 (integer) 25 hexists判断字段是否存在 1表示存在 0表示不存在 127.0.0.1:6379> hexists h1 age (integer) 1 127.0.0.1:6379> hexists h1 age2 (integer) 0 hlen获取hash属性个数 127.0.0.1:6379> hlen h1 (integer) 3 127.0.0.1:6379> hlen h2 (integer) 0 hkeys获取所有属性名称 127.0.0.1:6379> hkeys h1 1) "username" 2) "password" 3) "age" 127.0.0.1:6379> hkeys h2 (empty list or set) hvals获取所有属性值 127.0.0.1:6379> hvals h1 1) "andrew" 2) "123456" 3) "25" 127.0.0.1:6379> hvals h2 (empty list or set)
4. Redis数据结构之List类型
存储list: ArrayList使用数组方式 LinkedList使用双向链接方式 双向链接表中增加数据 双向链接表中删除数据 存储list常用命令 两端添加、两端弹出、扩展命令 lpush方式添加,从左边开始添加 127.0.0.1:6379> lpush l1 a b c d (integer) 4 127.0.0.1:6379> lpush l1 1 2 3 4 (integer) 8 lrange获取指定方位的集合元素 从第1个开始到倒数第一个,也就是最后一个,也就是所有数据 127.0.0.1:6379> lrange l1 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "d" 6) "c" 7) "b" 8) "a" 获取从第1个到第7个集合元素 127.0.0.1:6379> lrange l1 0 6 1) "4" 2) "3" 3) "2" 4) "1" 5) "d" 6) "c" 7) "b" rpush从右端开始添加(一般人比较习惯这种方式) 127.0.0.1:6379> rpush l2 a b c d (integer) 4 127.0.0.1:6379> rpush l2 1 2 3 4 (integer) 8 127.0.0.1:6379> lrange l2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "1" 6) "2" 7) "3" 8) "4" 127.0.0.1:6379> lrange l2 0 6 1) "a" 2) "b" 3) "c" 4) "d" 5) "1" 6) "2" 7) "3" lpop左侧弹出集合元素 rpop右侧弹出集合元素 127.0.0.1:6379> rpop l2 "a" 127.0.0.1:6379> lrange l2 0 -1 1) "b" 2) "c" 3) "d" 4) "1" 5) "2" 6) "3" 7) "4" 127.0.0.1:6379> rpop l2 "4" 127.0.0.1:6379> lrange l2 0 -1 1) "b" 2) "c" 3) "d" 4) "1" 5) "2" 6) "3" llen查看元素个数 127.0.0.1:6379> llen l2 (integer) 6 lpushx 集合头部插入元素 127.0.0.1:6379> lpushx l2 xx (integer) 7 127.0.0.1:6379> lrange l2 0 -1 1) "xx" 2) "b" 3) "c" 4) "d" 5) "1" 6) "2" 7) "3" rpushx 集合尾部插入元素 127.0.0.1:6379> rpushx l2 yy (integer) 8 127.0.0.1:6379> lrange l2 0 -1 1) "xx" 2) "b" 3) "c" 4) "d" 5) "1" 6) "2" 7) "3" 8) "yy" lpush集合头部插入多个元素 127.0.0.1:6379> lpush l2 a1 a2 (integer) 10 127.0.0.1:6379> lrange l2 0 -1 1) "a2" 2) "a1" 3) "xx" 4) "b" 5) "c" 6) "d" 7) "1" 8) "2" 9) "3" 10) "yy" 127.0.0.1:6379> rpush l2 a3 a4 (integer) 12 127.0.0.1:6379> lrange l2 0 -1 1) "a2" 2) "a1" 3) "xx" 4) "b" 5) "c" 6) "d" 7) "1" 8) "2" 9) "3" 10) "yy" 11) "a3" 12) "a4" lrem 从指定方向删除指定个数的指定元素 先加点数据搞个新集合l3 127.0.0.1:6379> lpush l3 1 3 2 3 2 1 2 1 3 (integer) 9 127.0.0.1:6379> lrange l3 0 -1 1) "3" 2) "1" 3) "2" 4) "1" 5) "2" 6) "3" 7) "2" 8) "3" 9) "1" 从左边开始删除2个1 127.0.0.1:6379> lrem l3 2 1 (integer) 2 127.0.0.1:6379> lrange l3 0 -1 1) "3" 2) "2" 3) "2" 4) "3" 5) "2" 6) "3" 7) "1" 从右边开始删除2个3 127.0.0.1:6379> lrem l3 -2 3 (integer) 2 127.0.0.1:6379> lrange l3 0 -1 1) "3" 2) "2" 3) "2" 4) "2" 5) "1" 删除所有2 127.0.0.1:6379> lrem l3 0 2 (integer) 3 127.0.0.1:6379> lrange l3 0 -1 1) "3" 2) "1" lset 设置集合指定索引的值 127.0.0.1:6379> lrange l1 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "d" 6) "c" 7) "b" 8) "a" 索引从0开始 127.0.0.1:6379> lset l1 3 xxxx OK 127.0.0.1:6379> lrange l1 0 -1 1) "4" 2) "3" 3) "2" 4) "xxxx" 5) "d" 6) "c" 7) "b" 8) "a" linsert 在集合里插入指定元素 在xxxx元素之前插入aa 127.0.0.1:6379> linsert l1 before xxxx aa (integer) 9 127.0.0.1:6379> lrange l1 0 -1 1) "4" 2) "3" 3) "2" 4) "aa" 5) "xxxx" 6) "d" 7) "c" 8) "b" 9) "a" 在xxxx元素之后插入bb 127.0.0.1:6379> linsert l1 after xxxx bb (integer) 10 127.0.0.1:6379> lrange l1 0 -1 1) "4" 2) "3" 3) "2" 4) "aa" 5) "xxxx" 6) "bb" 7) "d" 8) "c" 9) "b" 10) "a" rpoplpush 把A集合尾部元素弹出并插入到B集合头部 127.0.0.1:6379> rpush l4 a b c (integer) 3 127.0.0.1:6379> rpush l5 1 2 3 (integer) 3 127.0.0.1:6379> lrange l4 0 -1 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> lrange l5 0 -1 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> rpoplpush l4 l5 "c" 127.0.0.1:6379> lrange l4 0 -1 1) "a" 2) "b" 127.0.0.1:6379> lrange l5 0 -1 1) "c" 2) "1" 3) "2" 4) "3"
5. Redis数据结构之Set类型
存储Set和List类型不同的是,Set集合中不允许出现重复的元素 Set可包含的最大元素数量是4294967295 存储set常用命令: 添加/删除元素 获取集合中的元素 集合中的差集运算 集合中的交集运算 集合中的并集元算 扩展命令 sadd key member [member ...] 添加set元素 127.0.0.1:6379> sadd set1 a b c (integer) 3 添加三个元素 smembers key 查看指定key集合元素 127.0.0.1:6379> smembers set1 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> sadd set1 a d e (integer) 2 127.0.0.1:6379> smembers set1 1) "c" 2) "d" 3) "b" 4) "a" 5) "e" 继续添加元素发现重复元素不再添加srem key member [member ...] 删除元素 127.0.0.1:6379> srem set1 a d (integer) 2 127.0.0.1:6379> smembers set1 1) "b" 2) "c" 3) "e" 删除两个元素 sismember key member 判断某个元素是否存在,返回1表示存在,返回0表示不存在。 127.0.0.1:6379> sismember set1 a (integer) 0 127.0.0.1:6379> sismember set1 b (integer) 1 sdiff计算差集 127.0.0.1:6379> sadd set2 a b c (integer) 3 127.0.0.1:6379> sadd set3 b c d e (integer) 4 127.0.0.1:6379> sdiff set2 set3 1) "a" 127.0.0.1:6379> sdiff set3 set2 1) "d" 2) "e" 集合的顺序不同,结果不一样,根据前者参考 sinter计算交集 127.0.0.1:6379> sinter set2 set3 1) "c" 2) "b" sunion计算并集 127.0.0.1:6379> sunion set2 set3 1) "e" 2) "a" 3) "b" 4) "c" 5) "d" scard计算元素总数 127.0.0.1:6379> smembers set1 1) "b" 2) "c" 3) "e" 127.0.0.1:6379> scard set1 (integer) 3 srandmember随机取一个元素 127.0.0.1:6379> srandmember set1 "c" 127.0.0.1:6379> srandmember set1 "e" sdiffstore把差集结果存储到新集合中 127.0.0.1:6379> smembers set2 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> smembers set3 1) "c" 2) "e" 3) "d" 4) "b" 127.0.0.1:6379> sdiffstore r1 set2 set3 (integer) 1 127.0.0.1:6379> smembers r1 1) "a" sinterstore把交集结果存储到新集合中 127.0.0.1:6379> sinterstore r2 set2 set3 (integer) 2 127.0.0.1:6379> smembers r2 1) "c" 2) "b" sunionstore把并集结果存储到新集合中 127.0.0.1:6379> sunionstore r3 set2 set3 (integer) 5 127.0.0.1:6379> smembers r3 1) "e" 2) "a" 3) "b" 4) "c" 5) "d" 存储Set使用场景 跟踪一些唯一性数据 用于维护数据对象之间的关联关系
6. Redis数据结构之sorted-set类型
存储Sorted-Set Sorted-Set和Set的区别 Sorted-Set中的成员在集合中的位置是有序的 存储Sorted-set常用命令 添加元素 获得元素 删除元素 范围查询 扩展命令 zadd添加元素,里面包括评分和值 127.0.0.1:6379> zadd sort1 5 a 4 b 6 c (integer) 3 添加集合sort1 元素是a,b,c 评分分别是5,4,6 集合里的排序是根据评分从小到大排序的; zrange是查找元素 -1代表是最后一个 127.0.0.1:6379> zrange sort1 0 -1 1) "b" 2) "a" 3) "c" 继续添加元素,这里分两种情况 假如次元素集合里面已经有了,则覆盖 继续添加b此时评分改成7 127.0.0.1:6379> zadd sort1 7 b (integer) 0 通过zscore获取b的评分 127.0.0.1:6379> zscore sort1 b "7" 发现已经修改了; 127.0.0.1:6379> zrange sort1 0 -1 1) "a" 2) "c" 3) "b" 添加的元素不在集合里,则添加进去 127.0.0.1:6379> zadd sort1 9 d (integer) 1 127.0.0.1:6379> zrange sort1 0 -1 1) "a" 2) "c" 3) "b" 4) "d" 删除元素zrem 127.0.0.1:6379> zrem sort1 b (integer) 1 127.0.0.1:6379> zrange sort1 0 -1 1) "a" 2) "c" 3) "d" zcard 查看集合里的元素个数 127.0.0.1:6379> zcard sort1 (integer) 3 withscores把评分也显示出来 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "a" 2) "5" 3) "c" 4) "6" 5) "d" 6) "9" zrevrange 降序排列 127.0.0.1:6379> zrevrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "c" 4) "6" 5) "a" 6) "5" 再加两个元素 127.0.0.1:6379> zadd sort1 10 e 5 f (integer) 2 zremrangebyrank根据排名来删除元素删除3个 127.0.0.1:6379> zremrangebyrank sort1 0 2 (integer) 3 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "e" 4) "10" 再添加元素 127.0.0.1:6379> zadd sort1 11 f 16 g 18 h (integer) 3 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "e" 4) "10" 5) "f" 6) "11" 7) "g" 8) "16" 9) "h" 10) "18" zremrangebyscore根据具体评分范围来删除元素 127.0.0.1:6379> zremrangebyscore sort1 10 16 (integer) 3 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "h" 4) "18" 再添加元素 127.0.0.1:6379> zadd sort1 20 i 23 j 30 k (integer) 3 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "h" 4) "18" 5) "i" 6) "20" 7) "j" 8) "23" 9) "k" 10) "30" zrangebyscore根据评分范围来查找元素 127.0.0.1:6379> zrangebyscore sort1 18 23 withscores 1) "h" 2) "18" 3) "i" 4) "20" 5) "j" 6) "23" limit限定查找起始 类似分页 127.0.0.1:6379> zrangebyscore sort1 18 23 withscores limit 0 2 1) "h" 2) "18" 3) "i" 4) "20" zincrby给指定元素加分 127.0.0.1:6379> zincrby sort1 5 h "23" 127.0.0.1:6379> zrange sort1 0 -1 withscores 1) "d" 2) "9" 3) "i" 4) "20" 5) "h" 6) "23" 7) "j" 8) "23" 9) "k" 10) "30" zcount查找指定评分范围的元素个数 127.0.0.1:6379> zcount sort1 20 23 (integer) 3 Sorted-Set使用场景:大型在线游戏积分排行榜,构建索引数据
7. Redis之Keys通用操作
keys * 显示所有key 127.0.0.1:6379> keys * 1) "sort1" 2) "l2" 3) "set2" 4) "r1" 5) "h1" 6) "n2" 7) "l3" 8) "r2" 9) "s1" 10) "set3" 11) "set1" 12) "r3" 13) "n" 14) "n3" 15) "nn" 16) "l1" 查找所有以s开头的key 用s*,*代表任意字符 127.0.0.1:6379> keys s* 1) "sort1" 2) "set2" 3) "s1" 4) "set3" 5) "set1" 查找所有s开头 后面紧跟任意一个字符的key 127.0.0.1:6379> keys s? 1) "s1" del删除key 127.0.0.1:6379> del n2 n3 nn (integer) 3 exists判断key是否存在,1表示存在,0表示不存在。 127.0.0.1:6379> exists n2 (integer) 0 127.0.0.1:6379> exists l1 (integer) 1 get获取元素 127.0.0.1:6379> get n "1" rename对key重命名 127.0.0.1:6379> rename n n2 OK 127.0.0.1:6379> get n (nil) 127.0.0.1:6379> get n2 "1" expire设置n2有效时间 127.0.0.1:6379> expire n2 120 (integer) 1 ttl查看剩余时间 127.0.0.1:6379> ttl n2 (integer) 116 type显示类型 127.0.0.1:6379> type n2 string 127.0.0.1:6379> type l1 list 127.0.0.1:6379> type s1 string 127.0.0.1:6379> type sort1 zset