List
LPUSH key value1 [value2] 将一个或多个值插入到列表头部
127.0.0.1:6379> LPUSH myls1 1
(integer) 1
127.0.0.1:6379> LPUSH myls1 2
(integer) 2
127.0.0.1:6379> LRANGE myls1 0 -1
"2"
"1"
LPOP key 移出并获取列表的第一个元素
127.0.0.1:6379> LPOP myls1
"2"
127.0.0.1:6379> LRANGE myls1 0 -1
- "1"
LRANGE key start stop 获取列表指定范围内的元素
LPUSHX key value 将一个值插入到已存在的列表头部
127.0.0.1:6379> LPUSHX myls2 1 2 3
(integer) 0
127.0.0.1:6379> LRANGE myls2 0 -1
(empty array)
LLEN key 获取列表长度
127.0.0.1:6379> LLEN myls1
(integer) 1
LINSERT key BEFORE|AFTER pivot value 在列表的元素前或者后插入元素
127.0.0.1:6379> LINSERT myls1 after 1 0
(integer) 10
127.0.0.1:6379> LRANGE myls1 0 -1
"9"
"8"
"7"
"6"
"5"
"4"
"3"
"2"
"1"
"0"
LINDEX key index 通过索引获取列表中的元素
127.0.0.1:6379> LINDEX myls1 3
"6"
127.0.0.1:6379> LINDEX myls1 0
"9"
LSET key index value 通过索引设置列表元素的值
127.0.0.1:6379> LSET myls1 0 99
OK
127.0.0.1:6379> LRANGE myls1 0 -1
"99"
"8"
"7"
"6"
"5"
"4"
"3"
"2"
"1"
"0"
LREM key count value 移除列表元素
127.0.0.1:6379> LREM myls1 1 99
(integer) 1
127.0.0.1:6379> LRANGE myls1 0 -1
"8"
"7"
"6"
"5"
"4"
"3"
"2"
"1"
"0"
LTRIM key start stop 对一个列表进行修剪,就是让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除
127.0.0.1:6379> LTRIM myls1 0 7
OK
127.0.0.1:6379> LRANGE myls1 0 -1
"8"
"7"
"6"
"5"
"4"
"3"
"2"
"1"
BLPOP key1 [key2 ] timeout 移出并获取列表的第一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
127.0.0.1:6379> BLPOP myls1 60
"myls1"
"8"
127.0.0.1:6379> BLPOP myls1 60
"myls1"
"7"
127.0.0.1:6379> BLPOP myls1 60
"myls1"
"6"
BRPOP key1 [key2 ] timeout 移出并获取列表的最后一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
127.0.0.1:6379> BRPOPLPUSH myls1 myls2 60
"1"
127.0.0.1:6379> LRANGE myls1 0 -1
"9"
"8"
"7"
"6"
"5"
"4"
"3"
"2"
127.0.0.1:6379> LRANGE myls2 0 -1
- "1"
Set
SADD key member1 [member2] 向集合添加一个或多个成员
SMEMBERS key 返回集合中的所有成员
127.0.0.1:6379> SADD myset1 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> SMEMBERS myset1
"1"
"2"
"3"
"4"
"5"
"6"
"7"
SCARD key 获取集合的成员数
127.0.0.1:6379> SCARD myset1
(integer) 7
SRANDMEMBER key [count] 返回集合中一个或多个随机数
127.0.0.1:6379> SRANDMEMBER myset1
"3"
127.0.0.1:6379> SRANDMEMBER myset1 3
"6"
"4"
"1"
SISMEMBER key member 判断 member 元素是否是集合 key 的成员
127.0.0.1:6379> SISMEMBER myset1 11
(integer) 0
127.0.0.1:6379> SISMEMBER myset1 4
(integer) 1
SREM key member1 [member2] 移除集合中一个或多个成员
127.0.0.1:6379> SREM myset1 1 2
(integer) 2
127.0.0.1:6379> SMEMBERS myset1
"3"
"4"
"5"
"6"
"7"
SDIFF key1 [key2] 返回给定所有集合的差集
127.0.0.1:6379> SMEMBERS myset2
"0"
"1"
"2"
"3"
"4"
"8"
"9"
127.0.0.1:6379> SDIFF myset1 myset2
"5"
"6"
"7"
SDIFFSTORE destination key1 [key2] 返回给定所有集合的差集并存储在 destination 中
127.0.0.1:6379> SDIFFSTORE myset3 myset1 myset2
(integer) 3
127.0.0.1:6379> SMEMBERS myset3
"5"
"6"
"7"
SINTER key1 [key2] 返回给定所有集合的交集
127.0.0.1:6379> SINTER myset1 myset2
"3"
"4"
SUNION key1 [key2] 返回所有给定集合的并集
127.0.0.1:6379> SUNION myset1 myset2
"0"
"1"
"2"
"3"
"4"
"5"
"6"
"7"
"8"
"9"
SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合
127.0.0.1:6379> SMOVE myset2 myset1 8
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
"3"
"4"
"5"
"6"
"7"
"8"
SPOP key 移除并返回集合中的一个随机元素
127.0.0.1:6379> SPOP myset2
"1"
127.0.0.1:6379> SMEMBERS myset2
"0"
"2"
"3"
"4"
"9"
#sscan扫描myset1,从0开始,每次返回4个数量(不保证数量一定是4个)
127.0.0.1:6379> SSCAN myset1 0 count 4
"0"
- "3"
"4"
"5"
"6"
"7"
"8"
Zset
ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合指定区间内的成员
127.0.0.1:6379> ZADD myzset1 10 zs 20 ls 30 ww
(integer) 3
127.0.0.1:6379> ZRANGE myzset1 0 -1
"zs"
"ls"
"ww"
127.0.0.1:6379> ZRANGE myzset1 0 -1 withscores
"zs"
"10"
"ls"
"20"
"ww"
"30"
ZCARD key 获取有序集合的成员数
ZCOUNT key min max 计算在有序集合中指定区间分数的成员数
ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
127.0.0.1:6379> ZCARD myzset1
(integer) 3
127.0.0.1:6379> zcount myzset1 10 20
(integer) 2
127.0.0.1:6379> ZINCRBY myzset1 10 zs
"20"
127.0.0.1:6379> ZINCRBY myzset1 -10 zs
"10"
ZLEXCOUNT key min max 在有序集合中计算指定字典区间内成员数量
127.0.0.1:6379> zrange myzset1 0 -1
"a"
"b"
"c"
"d"
"e"
"zs"
"ls"
"ww"
127.0.0.1:6379> ZLEXCOUNT myzset1 [a [c
(integer) 3
ZRANGEBYLEX key min max [LIMIT offset count] 通过字典区间返回有序集合的成员
127.0.0.1:6379> ZRANGEBYLEX myzset1 (a (d
"b"
"c"
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 通过分数返回有序集合指定区间内的成员
127.0.0.1:6379> ZRANGEBYSCORE myzset1 (1 (30
"b"
"c"
"d"
"e"
"zs"
"ls"
ZRANK key member 返回有序集合中指定成员的索引
127.0.0.1:6379> ZRANK myzset1 zs
(integer) 5
127.0.0.1:6379> ZRANK myzset1 ls
(integer) 6
ZREM key member [member ...] 移除有序集合中的一个或多个成员
127.0.0.1:6379> zrem myzset1 a b
(integer) 2
ZREMRANGEBYLEX key min max 移除有序集合中给定的字典区间的所有成员
127.0.0.1:6379> ZREVRANGEBYSCORE myzset1 (50 (10
"ww"
"ls"
ZSCORE key member 返回有序集中,成员的分数值
127.0.0.1:6379> ZSCORE myzset1 ls
"20"
Hash
HSET key field value 将哈希表 key 中的字段 field 的值设为 value
HGET key field 获取存储在哈希表中指定字段的值
HGETALL key 获取在哈希表中指定 key 的所有字段和值
127.0.0.1:6379> HSET myhash1 name zs age 18 gender male
(integer) 3
127.0.0.1:6379> hget myhash1 name
"zs"
127.0.0.1:6379> hget myhash1 age
"18"
127.0.0.1:6379> HGETALL myhash1
"name"
"zs"
"age"
"18"
"gender"
"male"
HEXISTS key field 查看哈希表 key 中,指定的字段是否存在
127.0.0.1:6379> HEXISTS myhash1 name
(integer) 1
127.0.0.1:6379> HEXISTS myhash1 height
(integer) 0
HSETNX key field value 只有在字段 field 不存在时,设置哈希表字段的值
HKEYS key 获取所有哈希表中的字段
HVALS key 获取哈希表中所有值
HLEN key 获取哈希表中字段的数量
127.0.0.1:6379> HSETNX myhash1 height 160
(integer) 1
127.0.0.1:6379> HSETNX myhash1 name ls
(integer) 0
127.0.0.1:6379> HKEYS myhash1
"name"
"age"
"gender"
"height"
127.0.0.1:6379> HVALS myhash1
"zs"
"18"
"male"
"160"
127.0.0.1:6379> HLEN myhash1
(integer) 4
HMGET key field1 [field2] 获取所有给定字段的值
127.0.0.1:6379> HMGET myhash1 name age
"zs"
"18"
127.0.0.1:6379> HMSET myhash1 class 001 sid 111
OK
HINCRBY key field increment 为哈希表 key 中的指定字段的整数值加上增量 increment
127.0.0.1:6379> HINCRBY myhash1 age -1
(integer) 17
HDEL key field1 [field2] 删除一个或多个哈希表字段
HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对
127.0.0.1:6379> HDEL myhash1 sid
(integer) 1
127.0.0.1:6379> HSCAN myhash1 0 count 4
"0"
- "name"
"zs"
"age"
"17"
"gender"
"male"
"height"
"160"
"class"
"001"
Bitmaps
1、setbit
这个命令用于设置Bitmaps中某个偏移量的值(0或1),offset偏移量从0开始。格式如下:
setbit <key> <offset> <value>
2、getbit
这个命令用于获取Bitmaps中某个偏移量的值。格式为:
getbit <key> <offset>
3、bitcount
这个命令用于统计字符串被设置为1的bit数。一般情况下,给定的整个字符串都会被进行计数,通过指定额外的 start 或 end 参数,可以让计数只在特定的位上进行。start 和 end 参数的设置,都可以使用负数值:比如 -1 表示最后一个位,而 -2 表示倒数第二个位,start、end 是指bit组的字节的下标数,二者皆包含。格式如下:
bitcount <key> [start end]
将1变为3
127.0.0.1:6379> SET mybit1 1234567890
OK
127.0.0.1:6379> SETBIT mybit1 6 1
(integer) 0
127.0.0.1:6379> GET mybit1
"3234567890"
统计zhansan一周几次没来上课,1为来了,0为没来
setbit zhansan 第x天 比特值
127.0.0.1:6379> setbit zhansan 0 1
(integer) 0
127.0.0.1:6379> setbit zhansan 2 1
(integer) 0
127.0.0.1:6379> setbit zhansan 3 1
(integer) 0
127.0.0.1:6379> setbit zhansan 4 1
(integer) 0
最后统计7天内比特值为1的数量
127.0.0.1:6379> bitcount zhansan 0 7 bit
(integer) 4
4、bitop
这个命令是一个复合操作, 它可以做多个Bitmaps的and(交集) 、 or(并集) 、 not(非) 、 xor(异或) 操作并将结果保存在destkey中。格式如下:
bitop and(or/not/xor) <destkey> [key...]
tmp 00010000
tmp2 00111000
and(交集) 、 or(并集) 、 not(非) 、 xor(异或)
127.0.0.1:6379> bitop and tmp3 tmp tmp2
(integer) 1
127.0.0.1:6379> getbit tmp3 0
(integer) 0
127.0.0.1:6379> getbit tmp3 1
(integer) 0
127.0.0.1:6379> getbit tmp3 2
(integer) 0
127.0.0.1:6379> getbit tmp3 3
(integer) 0
127.0.0.1:6379> getbit tmp3 4
(integer) 1
127.0.0.1:6379> getbit tmp3 5
(integer) 0
127.0.0.1:6379> getbit tmp3 6
(integer) 0
127.0.0.1:6379> getbit tmp3 7
(integer) 0
HyperLogLog
PFADD key element [element ...] 添加指定元素到 HyperLogLog 中
PFCOUNT key [key ...] 返回给定 HyperLogLog 的基数估算值
PFMERGE destkey sourcekey [sourcekey ...] 将多个 HyperLogLog 合并为一个 HyperLogLog
127.0.0.1:6379> PFADD myhypher1 a b c d e f
(integer) 1
127.0.0.1:6379> PFADD myhypher2 1 3 5 7 9
(integer) 1
127.0.0.1:6379> PFCOUNT myhypher1
(integer) 6
127.0.0.1:6379> PFCOUNT myhypher2
(integer) 5
127.0.0.1:6379> PFMERGE myhypher1 myhypher1 myhypher2
OK
127.0.0.1:6379> PFCOUNT myhypher1
(integer) 11
Geospatial
geoadd key longitude latitude member [longitude latitude member...] 添加地理位置(经度,纬度,名称)
geopos key member [member...] 获得指定地区的坐标值
geodist key member1 member2 [m|km|ft|mi] 获取两个位置之间的直线距离
georadius key longitude latitude radius [m|km|ft|mi] 以给定的经纬度为中心,找出某一半径内的元素
127.0.0.1:6379> GEOADD beijing 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> GEOADD china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> GEOADD china:city 106.50 29.53 chongqing 116.38 39.90 beijing 114.05 22.52 shenzhen
(integer) 3
127.0.0.1:6379> GEOPOS china:city shanghai
- "121.47000163793563843"
"31.22999903975783553"
127.0.0.1:6379> GEOPOS china:city beijing chongqing km
- "116.38000041246414185"
"39.90000009167092543"
- "106.49999767541885376"
"29.52999957900659211"
(nil)
127.0.0.1:6379> GEORADIUS china:city 110 30 100 km
(empty array)
127.0.0.1:6379> GEORADIUS china:city 110 30 1000 km
"chongqing"
"shenzhen"