redis命令简记
Redis Key-Value Pairs
SET key value
GET key
SET key value nx //如是新key,设置成功;已存在key,设置失败
SET key value xx //如是新key,设置失败;已存在key,修改成功
SET key value ex 10 //设置键与值,10秒后过期删除
INCR counter //counter增一
INCRBY counter 3 //counter增3
DECR counter counter减一
DECRBY counter 5 //counter减5
GETSET counter 50 //counter设置成50,返回counter没设置前的值
MSET a 10 b 20 c 30 //a设置10,b设置20,c设置30
MGET a b c //以数组形式返回a,b,c的值
EXISTS counter //存在counter返回1,否则0
DEL counter //删除counter
TYPE counter //返回counter的值类型
EXPIRE counter 5 //设置counter5秒后过期删除,即使redis关掉了。
TTL key //查看key离被过期删除还有多久
Redis Lists
LLEN b //b的长度
LPUSH abc 33 f3 //往左边加元素abc,33,f3
RPUSH abc 33 f3 //往右边加元素abc,33,f3
LRANGE mylist 0 -1 //输出mylist的所有元素,0表示起始位置,-1表示末尾位置
RPOP mylist //右边删除一个元素,并返回这个元素的值
LTRIM mylist 0 2 //超出这个范围的值都被删除
Blocking operations on lists
BRPOP tasks 5 //pop tasks右边元素,没有则最长等待5秒,5秒后还没有则返回
BRPOP tasks tas 5 //pop tasks右边元素,没有则pop tas右边元素,5秒后还没有则返回
RPOPLPUSH b b //pop b 右边元素,然后push到b左边去
BRPOPLPUSH b b 5 //pop b 右边元素,然后push到b左边去,没有则等待5秒后返回
Redis Hashes
hmset msg:102385 author yangzetao date 2019-5-25 content 'hello everyone!' //写入一条记录,键msg:102385 author是yangzetao date是2019-5-25,content是hello everyone!
hget msg:102385 date //获取键是msg:102385的date
hmget msg:102385 content date //获取键是msg:102385的content和date
hgetall msg:102385 //获取键是msg:102385记录的所有字段值
hincrby msg:102385 readcount 3 //键msg:102385字段readcount加3
Redis Sets(无序,可以进行并集,差集等运算)
sadd myset 1 2 3 //往myset添加1 2 3
smembers myset //输出myset所有元素
sismember myset 3 //3是否在myset中
sinter myset1 myset2 myset3 //交集
sdiff myset1 myset2 //差集
sunion myset1 myset2 myset4 //并集
sunionstore deck2 deck //deck2 和deck的并集,并将结果存到deck2中
SPOP deck1 //随机pop某个元素
scard deck1 //deck1的元素个数
srandmember deck 2 //随机输出deck中某两个元素
Redis Sorted Sets(asked for sorted elements take no effort,add an element perform an O(log(N) operation)
zadd hackers 1940 "Alan Kay" //add "Alan Kay"element to hackers,with a score 1940.
zrange hackers 0 -1 //return all elements of hackers with already sorted order.
zrevrange hackers 0 -1 //return all elements of hackers with reversed order.
zrange hackers 0 -1 withscores //return all elements and their score.
zrangebyscore hackers -inf 1950 //return elements whose score between infinity and 1950(inclusive).
zremrangebyscore hackers 1940 1960 //remove all the hackers born between 1940 and 1960 from the sorted set.And return number of removed elements.
zrank hackers "Anita Borg" //return the position of specific element in the sorted set.
zrevrank hackers "Anita Borg" //return the reversed position of specific element in the sorted set.
Lexicographical scores(like sorted sets with all identical scores)
Updating the score:leader boards
You can update the score of sorted set elements by zadd.
Bitmaps
setbit key 10 1 //set 10th number bit of key to 1,and return the key value before this operation
getbit key 10 //get the value of the 10th number bit of key.
bitop [operation(AND OR XOR NOT)] destkey key [key ...] //bits operation
bitcount key start end //performs population counting, reporting the number of bits set to 1. start specified the first byte to start count,end the last byte to count.
bitpos key 1 start end //finds the first bit having the specified value of 1. start and end are the same as above.

浙公网安备 33010602011771号