Redis common command operation

#linux是redis-cli
#普通环境
redis-cli.exe -h host -p port -a password
#集群环境,否则报:(error) MOVED 6918 127.0.0.1:6381
redis-cli.exe -c -h host -p port -a password

#参数说明
#host:远程redis服务器host
#port:远程redis服务端口
#password:远程redis服务密码(无密码的的话就不需要-a参数了)

#输入exit退出客户端

string command

Order explain
get key Get the value of the key
set key v Set the value of the key
del key delete key (applies to all types)
incr key Add 1 to the stored value and return the result after the operation
decr key Subtract 1 from the stored value and return the result of the operation
incrby key amout Add the integer amount and return the result of the operation
decrby key amout Subtract the integer amount and return the result after the operation
incrbyfloat key amout Add the floating point amount to the string binary, and return the result after the operation
append key v Append the value to the end of the current stored value of the key, and return the length of the string after the operation
getrange key start end Get the string from subscript start to end
setrange key offset v Treat the string as a binary bit string, and the value of the binary bit at offset offset in the bit string
getbit key offset

Treat the string as the number of bits with a value of 1 in the binary string. If the optional start offset and end offset are given, then only the bits in the range specified by the offset are counted.

bitop operation dest-key key-name [key-name …]

Perform any bitwise operation (bitwise operation) on one or more binary bit strings, or or, XOR, NOT, and put the calculation result in the dest-key

list command

Order explain
rpush key [v…] Add one or more to the right end of the list, return the length of the list
lpush key [v…] Add one or more to the left end of the list, return the length of the list
rpop key Remove and return the rightmost element, return the removed element
lpop key Removes and returns the leftmost element of the list, returns the removed element
lindex key size Returns the element whose subscript (offset) is size, starting from 0
lrange key start end Returns elements from start to end including start and end
ltrim key start end Only keep elements from start to end including start and end

hash command

Order explain 
hmget hkey key… get multiple values 
hmset hkey key v…  Set values ​​for multiple keys 
hdel hkey key…  remove multiple values ​​and return 
hlen hkey  return total quantity 
hexists hkey key  Check if the key exists in the hash 
hkeys hkey Get all keys in the hash
hvals hkey Get all values ​​in the hash
hgetall hkey get hash
hincrby hkey key increment Add integer increment to the value of key
hincrbyfloat hkey key increment Add a floating point increment to the value of the key

set command

Order explain
sadd key item … Add multiple, return the newly added number (existing ones are not counted)
srem key item. Removes multiple elements from a collection, returning the number of removed elements
sismember key item Check if the element item is in the collection
scard key Returns the total number of collections
smembers key return all elements
srandmember key cout Randomly return cout elements cout is a positive integer random element does not repeat, on the contrary may appear repeated
spop key Randomly removes an element and returns the removed element
smove key1 key2 item If key1 contains item, remove the item in key1 and add it to key2, return 1 if successful, return 0 if failed
difference operation sdiffstore newkey key key1… Put other elements that exist in the key collection but do not exist in the key1... collection into newkey (bite off the rest)
Cross operation sinter key… returns the intersection of all sets (returns what we have)
Cross operation sinterstore newkey key… Returns the intersection of multiple sets to generate set newkey
And calculate the sunion key... returns all elements we don't repeat 
And calculate sunion newkey key... Put the result in newkey

zset command

Order explain
zadd key score member … add multiple
zerm key memer… remove multiple
zcard key 返回所有成员
zincrby key incremnet member 将member成员的分值加上increment
zcount key min max 返回分值在 min和max中间的排名
zrank key member 返回成员member在集合中的排名
zscore key member 返回member的分值
zrange key start stop 返回介于两者之间的成员
zrevrank key-name member  返回有序集合里成员member的排名,成员按照分值从大到小排列 
zrevrange key-name start stop [withscores]  返回有序集合给定排名范围内的成员,成员按照分值从大到小排列 
zrangebyscore key min max [withscores] [limit offset count]  返回有序集合中,分值介于min和max之间的所有成员 
zrevrangebyscore key max min [withscores] [limit offset count]  获取有序集合中分值介于min和max之间的所有成员,并按照分值从大到小的顺序来返回它们 
zremrangebyrank key-name start stop  移除有序集合中排名介于start和stop之间的所有成员 
zremrangebyscore key-name min max 移除有序集合中分值介于min和max之间的所有成员
zinterstore dest-key key-count key [key ...] [weights weight[weight ...]] [aggregate sum|min|max] 对给定的有序集合执行类似于集合的交集运算
zunionstore dest-key key-count key [key ...] [weights weight[weight ...]] [aggregate sum|min|max] 对给定的有序集合执行类似于并集的运算

常用命令汇总

redis连接

#本地连接
redis-cli

#远程连接
redis-cli -h host -p port -a password

#验证密码是否正确
AUTH password

#打印字符串
ECHO message 

#查看服务是否运行
PING 

#关闭当前连接
QUIT 

#切换到指定的数据库
SELECT index

redis keys命令 

#删除已存在的key
DEL key

#序列化给定的key并返回序列化的值
DUMP key

#检查给定的key是否存在
EXISTS key 

#为key设置过期时间
EXPIRE key seconds 

#用时间戳的方式给key设置过期时间
EXPIRE key timestamp 

#设置key的过期时间以毫秒计
PEXPIRE key milliseconds 

#查找所有符合给定模式的key
KEYS pattern 

#将当前数据库的key移动到数据库db当中
MOVE key db 

#移除key的过期时间,key将持久保存
PERSIST key

#以毫秒为单位返回key的剩余过期时间
PTTL key

#以秒为单位,返回给定key的剩余生存时间
TTL key

#从当前数据库中随机返回一个key
RANDOMKEY

#修改key的名称
RENAME key newkey

#仅当newkey不存在时,将key改名为newkey
RENAMENX key newkey

#返回key所存储的值的类型
TYPE key

redis字符串命令

SET key value

GET key

#返回key中字符串值的子字符
GETRANGE key start end

#将给定key的值设为value,并返回key的旧值
GETSET key value

#对key所储存的字符串值,获取指定偏移量上的位
GETBIT KEY OFFSET

#获取一个或者多个给定key的值
MGET KEY1 KEY2

#对key所是存储的字符串值,设置或清除指定偏移量上的位
SETBIT KEY OFFSET VALUE

#将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)
SETEX key seconds value

#只有在 key 不存在时设置 key 的值
SETNX key value

#用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
SETRANGE key offset value

#返回 key 所储存的字符串值的长度
STRLEN key

#同时设置一个或多个 key-value 对
MSET key value [key value ...]

#同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
MSETNX key value [key value ...] 

#这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位
PSETEX key milliseconds value

#将 key 中储存的数字值增一
INCR key

#将 key 所储存的值加上给定的增量值(increment)
INCRBY key increment

#将 key 所储存的值加上给定的浮点增量值(increment)
INCRBYFLOAT key increment

#将 key 中储存的数字值减一
DECR key

#key 所储存的值减去给定的减量值(decrement) 
DECRBY key decrement

#如果 key 已经存在并且是一个字符串, APPEND 命令将 指定value 追加到改 key 原来的值(value)的末尾
APPEND key value

redis hash命令

#删除一个或多个哈希表字段
HDEL key field1 [field2] 

#查看哈希表 key 中,指定的字段是否存在
HEXISTS key field 

#获取存储在哈希表中指定字段的值
HGET key field 

#获取在哈希表中指定 key 的所有字段和值
HGETALL key 

#为哈希表 key 中的指定字段的整数值加上增量 increment
HINCRBY key field increment 

#为哈希表 key 中的指定字段的浮点数值加上增量 increment
HINCRBYFLOAT key field increment 

#获取所有哈希表中的字段
HKEYS key 

#获取哈希表中字段的数量
HLEN key 

#获取所有给定字段的值
HMGET key field1 [field2] 

#同时将多个 field-value (域-值)对设置到哈希表 key 中
HMSET key field1 value1 [field2 value2 ] 

#将哈希表 key 中的字段 field 的值设为 value 
HSET key field value 

#只有在字段 field 不存在时,设置哈希表字段的值
HSETNX key field value 

#获取哈希表中所有值
HVALS key 

#迭代哈希表中的键值对
HSCAN key cursor [MATCH pattern] [COUNT count] 

redis列表命令

#移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
BLPOP key1 [key2 ] timeout 

#移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
BRPOP key1 [key2 ] timeout 

#从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
BRPOPLPUSH source destination timeout 

#通过索引获取列表中的元素
LINDEX key index 

#在列表的元素前或者后插入元素
LINSERT key BEFORE|AFTER pivot value 

#获取列表长度
LLEN key 

#移出并获取列表的第一个元素
LPOP key 

#将一个或多个值插入到列表头部
LPUSH key value1 [value2] 

#将一个值插入到已存在的列表头部
LPUSHX key value 

#获取列表指定范围内的元素
LRANGE key start stop 

#移除列表元素
LREM key count value 

#通过索引设置列表元素的值
LSET key index value 

#对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除
LTRIM key start stop 

#移除并获取列表最后一个元素
RPOP key 

#移除列表的最后一个元素,并将该元素添加到另一个列表并返回
RPOPLPUSH source destination 

#在列表中添加一个或多个值
RPUSH key value1 [value2] 

#为已存在的列表添加值
RPUSHX key value 

redis集合命令

#向集合添加一个或多个成员
SADD key member1 [member2] 

#获取集合的成员数
SCARD key 

#返回给定所有集合的差集
SDIFF key1 [key2] 

#返回给定所有集合的差集并存储在 destination 中
SDIFFSTORE destination key1 [key2] 

#返回给定所有集合的交集
SINTER key1 [key2] 

#返回给定所有集合的交集并存储在 destination 中
SINTERSTORE destination key1 [key2] 

#判断 member 元素是否是集合 key 的成员
SISMEMBER key member 

#返回集合中的所有成员
SMEMBERS key 

#将 member 元素从 source 集合移动到 destination 集合
SMOVE source destination member 

#移除并返回集合中的一个随机元素
SPOP key 

#返回集合中一个或多个随机数
SRANDMEMBER key [count] 

#移除集合中一个或多个成员
SREM key member1 [member2] 

#返回所有给定集合的并集
SUNION key1 [key2] 

#所有给定集合的并集存储在 destination 集合中
SUNIONSTORE destination key1 [key2] 

#迭代集合中的元素
SSCAN key cursor [MATCH pattern] [COUNT count] 

redis有序集合命令 

#向有序集合添加一个或多个成员,或者更新已存在成员的分数
ZADD key score1 member1 [score2 member2] 

#获取有序集合的成员数
ZCARD key 

#计算在有序集合中指定区间分数的成员数
ZCOUNT key min max 

#有序集合中对指定成员的分数加上增量 increment
ZINCRBY key increment member 

#计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
ZINTERSTORE destination numkeys key [key ...] 

#在有序集合中计算指定字典区间内成员数量
ZLEXCOUNT key min max 

#通过索引区间返回有序集合成指定区间内的成员
ZRANGE key start stop [WITHSCORES] 

#通过字典区间返回有序集合的成员
ZRANGEBYLEX key min max [LIMIT offset count] 

#通过分数返回有序集合指定区间内的成员
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 

#返回有序集合中指定成员的索引
ZRANK key member 

#移除有序集合中的一个或多个成员
ZREM key member [member ...] 

#移除有序集合中给定的字典区间的所有成员
ZREMRANGEBYLEX key min max 

#移除有序集合中给定的排名区间的所有成员
ZREMRANGEBYRANK key start stop 

#移除有序集合中给定的分数区间的所有成员
ZREMRANGEBYSCORE key min max 

#返回有序集中指定区间内的成员,通过索引,分数从高到底
ZREVRANGE key start stop [WITHSCORES] 

#返回有序集中指定分数区间内的成员,分数从高到低排序
ZREVRANGEBYSCORE key max min [WITHSCORES] 

#返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
ZREVRANK key member 

#返回有序集中,成员的分数值
ZSCORE key member 

#计算给定的一个或多个有序集的并集,并存储在新的 key 中
ZUNIONSTORE destination numkeys key [key ...] 

#迭代有序集合中的元素(包括元素成员和元素分值)
ZSCAN key cursor [MATCH pattern] [COUNT count] 

发布订阅命令 

#订阅一个或多个符合给定模式的频道。
PSUBSCRIBE pattern [pattern ...] 

#查看订阅与发布系统状态。
PUBSUB subcommand [argument [argument ...]] 

#将信息发送到指定的频道
PUBLISH channel message 

#退订所有给定模式的频道
PUNSUBSCRIBE [pattern [pattern ...]] 

#订阅给定的一个或多个频道的信息
SUBSCRIBE channel [channel ...] 

#指退订给定的频道
UNSUBSCRIBE [channel [channel ...]] 

示例:
redis 127.0.0.1:6379> SUBSCRIBE redisChat

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "redisChat"
3) (integer) 1

现在,我们先重新开启个 redis 客户端,然后在同一个频道 redisChat 发布两次消息,订阅者就能接收到消息。
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"

(integer) 1

# 订阅者的客户端会显示如下消息
1) "message"
2) "redisChat"
3) "Redis is a great caching technique"

redis事务命令

#取消事务,放弃执行事务块内的所有命令
DISCARD 

#执行所有事务块内的命令
EXEC

#标记一个事务块的开始
MULTI 

#取消 WATCH 命令对所有 key 的监视
UNWATCH 

#监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断
WATCH key [key ...] 

redis脚本命令

#执行 Lua 脚本
EVAL script numkeys key [key ...] arg [arg ...] 

#执行 Lua 脚本
EVALSHA sha1 numkeys key [key ...] arg [arg ...] 

#查看指定的脚本是否已经被保存在缓存当中
SCRIPT EXISTS script [script ...] 

#从脚本缓存中移除所有脚本
SCRIPT FLUSH 

#杀死当前正在运行的 Lua 脚本
SCRIPT KILL 

#将脚本 script 添加到脚本缓存中,但并不立即执行这个脚本
SCRIPT LOAD script 

服务器命令

#异步执行一个 AOF(AppendOnly File) 文件重写操作
BGREWRITEAOF 

#在后台异步保存当前数据库的数据到磁盘
BGSAVE 

#关闭客户端连接
CLIENT KILL [ip:port] [ID client-id] 

#获取连接到服务器的客户端连接列表
CLIENT LIST 

#获取连接的名称
CLIENT GETNAME 

#在指定时间内终止运行来自客户端的命令
CLIENT PAUSE timeout 

#设置当前连接的名称
CLIENT SETNAME connection-name 

#获取集群节点的映射数组
CLUSTER SLOTS 

#获取 Redis 命令详情数组
COMMAND 

#获取 Redis 命令总数
COMMAND COUNT 

#获取给定命令的所有键
COMMAND GETKEYS 

#返回当前服务器时间
TIME 

#获取指定 Redis 命令描述的数组
COMMAND INFO command-name [command-name ...] 

#获取指定配置参数的值
CONFIG GET parameter 

#对启动 Redis 服务器时所指定的 redis.conf 配置文件进行改写
CONFIG REWRITE 

#修改 redis 配置参数,无需重启
CONFIG SET parameter value 

#重置 INFO 命令中的某些统计数据
CONFIG RESETSTAT 

#返回当前数据库的 key 的数量
DBSIZE 

#获取 key 的调试信息
DEBUG OBJECT key 

#让 Redis 服务崩溃
DEBUG SEGFAULT 

#删除所有数据库的所有key
FLUSHALL 

#删除当前数据库的所有key
FLUSHDB 

#获取 Redis 服务器的各种信息和统计数值
INFO [section] 

#返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 UNIX 时间戳格式表示
LASTSAVE 

#实时打印出 Redis 服务器接收到的命令,调试用
MONITOR 

#返回主从实例所属的角色
ROLE 

#同步保存数据到硬盘
SAVE 

#异步保存数据到硬盘,并关闭服务器
SHUTDOWN [NOSAVE] [SAVE] 

#将当前服务器转变为指定服务器的从属服务器(slave server)
SLAVEOF host port 

#管理 redis 的慢日志
SLOWLOG subcommand [argument] 

#用于复制功能(replication)的内部命令
SYNC 

Guess you like

Origin blog.csdn.net/vcit102/article/details/131589247