1.通用命令
通用命令不分数据类型,都可以使用,常见的命令有
-
KEYS pattern
:查看符合pattern的所有key,比如? * [ae] [^a] [a-b]
等常见正则。不建议在生产环境上使用。redis:6379> MSET firstname Jack lastname Stuntman age 35 "OK" redis:6379> KEYS *name* 1) "lastname" 2) "firstname" redis:6379> KEYS a?? 1) "age" redis:6379> KEYS * 1) "age" 2) "lastname" 3) "firstname"
-
DEL
:删除指定的key,可以接收多个key值redis:6379> SET key1 "Hello" "OK" redis:6379> SET key2 "World" "OK" redis:6379> DEL key1 key2 key3 (integer) 2
-
EXISTS
:判断key是否存在,可以接收多个key值redis:6379> SET key1 "Hello" "OK" redis:6379> EXISTS key1 (integer) 1 redis:6379> EXISTS nosuchkey (integer) 0 redis:6379> SET key2 "World" "OK" redis:6379> EXISTS key1 key2 nosuchkey (integer) 2
-
EXPIRE
:给一个key设置有效期,到期时该key会被自动删除。7.0.0版本EXPIRE
有四个可选参数:- NX:仅当有效期过期时才设置
- XX:有效期没过期时才设置
- GT:新的有效期大于当前有效期才设置
- LT:新的有效期小于当前有效期才设置
redis:6379> SET mykey "Hello" "OK" redis:6379> EXPIRE mykey 10 (integer) 1 redis:6379> TTL mykey (integer) 10 redis:6379> SET mykey "Hello World" "OK" redis:6379> TTL mykey (integer) -1 redis:6379> EXPIRE mykey 10 XX (integer) 0 redis:6379> TTL mykey (integer) -1 redis:6379> EXPIRE mykey 10 NX (integer) 1 redis:6379> TTL mykey (integer) 10
-
TTL
:查看一个key的剩余有效期redis:6379> SET mykey "Hello" "OK" redis:6379> EXPIRE mykey 10 (integer) 1 redis:6379> TTL mykey (integer) 10
2.String类型命令
-
SET
:添加或修改已经存在的一个String的键值对redis:6379> SET mykey "Hello" "OK" redis:6379> GET mykey "Hello"
-
GET
:根据ket获取String类型的valueredis:6379> GET nonexisting (nil) redis:6379> SET mykey "Hello" "OK" redis:6379> GET mykey "Hello"
-
MSET
:批量添加多个String类型的键值对redis:6379> MSET key1 "Hello" key2 "World" "OK" redis:6379> GET key1 "Hello" redis:6379> GET key2 "World"
-
MGET
:根据多个key获取多个String类型的valueredis:6379> SET key1 "Hello" "OK" redis:6379> SET key2 "World" "OK" redis:6379> MGET key1 key2 nonexisting 1) "Hello" 2) "World" 3) (nil)
-
INCR
:让一个整型的key自增1redis:6379> SET mykey "10" "OK" redis:6379> INCR mykey (integer) 11 redis:6379> GET mykey "11"
-
INCRBY
:让一个整型的key以指定步长增长redis:6379> SET mykey "10" "OK" redis:6379> INCRBY mykey 5 (integer) 15
-
INCRBYFLOAT
:让一个浮点类型的数字以指定步长增长redis:6379> SET mykey 10.50 "OK" redis:6379> INCRBYFLOAT mykey 0.1 "10.6" redis:6379> INCRBYFLOAT mykey -5 "5.6" redis:6379> SET mykey 5.0e3 "OK" redis:6379> INCRBYFLOAT mykey 2.0e2 "5200"
-
SETNX
:当key不存在时,添加String类型的键值对,当key存在时,不执行。redis:6379> SETNX mykey "Hello" (integer) 1 redis:6379> SETNX mykey "World" (integer) 0 redis:6379> GET mykey "Hello"
-
SETEX
:添加一个String类型的键值对,并指定有效期redis:6379> SETEX mykey 10 "Hello" "OK" redis:6379> TTL mykey (integer) 10 redis:6379> GET mykey "Hello"
3.Hash类型命令
-
HSET
:添加或者修改hash类型key的field的值redis:6379> HSET myhash field1 "Hello" (integer) 1 redis:6379> HGET myhash field1 "Hello"
-
HGET
:获取一个hash类型key的field的值redis:6379> HSET myhash field1 "foo" (integer) 1 redis:6379> HGET myhash field1 "foo" redis:6379> HGET myhash field2 (nil)
-
HMSET
:批量添加多个hash类型key的field的值(在4.0.0版本后用HSET
也能达到此效果)redis:6379> HMSET myhash field1 "Hello" field2 "World" "OK" redis:6379> HGET myhash field1 "Hello" redis:6379> HGET myhash field2 "World"
-
HMGET
:批量获取多个hash类型key的field的值redis:6379> HSET myhash field1 "Hello" (integer) 1 redis:6379> HSET myhash field2 "World" (integer) 1 redis:6379> HMGET myhash field1 field2 nofield 1) "Hello" 2) "World" 3) (nil)
-
HGETALL
:获取一个hash类型的key中的所有的field和valueredis:6379> HSET myhash field1 "Hello" (integer) 1 redis:6379> HSET myhash field2 "World" (integer) 1 redis:6379> HGETALL myhash 1) "field1" 2) "Hello" 3) "field2" 4) "World"
-
HKEYS
:获取一个hash类型的key中的所有的fieldredis:6379> HSET myhash field1 "Hello" (integer) 1 redis:6379> HSET myhash field2 "World" (integer) 1 redis:6379> HKEYS myhash 1) "field1" 2) "field2"
-
HVALS
:获取一个hash类型的key中的所有的valueredis:6379> HSET myhash field1 "Hello" (integer) 1 redis:6379> HSET myhash field2 "World" (integer) 1 redis:6379> HVALS myhash 1) "Hello" 2) "World"
4.List类型命令
-
LPUSH
:向列表左侧插入一个或多个元素redis:6379> LPUSH mylist "world" (integer) 1 redis:6379> LPUSH mylist "hello" (integer) 2 redis:6379> LRANGE mylist 0 -1 1) "hello" 2) "world"
-
LPOP
:移除并返回列表左侧的第一个元素redis:6379> RPUSH mylist "one" "two" "three" "four" "five" (integer) 5 redis:6379> LPOP mylist "one" redis:6379> LPOP mylist 2 1) "two" 2) "three" redis:6379> LRANGE mylist 0 -1 1) "four" 2) "five"
-
RPUSH
:向列表右侧插入一个或多个元素redis:6379> RPUSH mylist "hello" (integer) 1 redis:6379> RPUSH mylist "world" (integer) 2 redis:6379> LRANGE mylist 0 -1 1) "hello" 2) "world"
-
RPOP
:移除并返回列表右侧的第一个元素redis:6379> RPUSH mylist "one" "two" "three" "four" "five" (integer) 5 redis:6379> RPOP mylist "five" redis:6379> RPOP mylist 2 1) "four" 2) "three" redis:6379> LRANGE mylist 0 -1 1) "one" 2) "two"
-
LRANGE
:返回一段脚标范围内的所有元素redis:6379> RPUSH mylist "one" (integer) 1 redis:6379> RPUSH mylist "two" (integer) 2 redis:6379> RPUSH mylist "three" (integer) 3 redis:6379> LRANGE mylist 0 0 1) "one" redis:6379> LRANGE mylist -3 2 1) "one" 2) "two" 3) "three" redis:6379> LRANGE mylist -100 100 1) "one" 2) "two" 3) "three" redis:6379> LRANGE mylist 5 10 (empty array)
5.Set类型命令
-
SADD
:向set中添加一个或多个元素redis:6379> SADD myset "Hello" (integer) 1 redis:6379> SADD myset "World" (integer) 1 redis:6379> SADD myset "World" (integer) 0 redis:6379> SMEMBERS myset 1) "World" 2) "Hello"
-
SREM
:移除set中的指定元素redis:6379> SADD myset "one" (integer) 1 redis:6379> SADD myset "two" (integer) 1 redis:6379> SADD myset "three" (integer) 1 redis:6379> SREM myset "one" (integer) 1 redis:6379> SREM myset "four" (integer) 0 redis:6379> SMEMBERS myset 1) "two" 2) "three"
-
SCARD
:返回set中元素的个数redis:6379> SADD myset "Hello" (integer) 1 redis:6379> SADD myset "World" (integer) 1 redis:6379> SCARD myset (integer) 2
-
SISMEMBER
:判断一个元素是否存在于set中redis:6379> SADD myset "one" (integer) 1 redis:6379> SISMEMBER myset "one" (integer) 1 redis:6379> SISMEMBER myset "two" (integer) 0
-
SMEMBERS
:获取set中的所有元素redis:6379> SADD myset "Hello" (integer) 1 redis:6379> SADD myset "World" (integer) 1 redis:6379> SMEMBERS myset 1) "World" 2) "Hello"
-
SINTER
:求两个set的交集redis:6379> SADD key1 "a" (integer) 1 redis:6379> SADD key1 "b" (integer) 1 redis:6379> SADD key1 "c" (integer) 1 redis:6379> SADD key2 "c" (integer) 1 redis:6379> SADD key2 "d" (integer) 1 redis:6379> SADD key2 "e" (integer) 1 redis:6379> SINTER key1 key2 1) "c"
-
SDIFF
:求两个set的差集(k1-k2)redis:6379> SADD key1 "a" (integer) 1 redis:6379> SADD key1 "b" (integer) 1 redis:6379> SADD key1 "c" (integer) 1 redis:6379> SADD key2 "c" (integer) 1 redis:6379> SADD key2 "d" (integer) 1 redis:6379> SADD key2 "e" (integer) 1 redis:6379> SDIFF key1 key2 1) "a" 2) "b"
-
SUNION
:求两个set的并集redis:6379> SADD key1 "a" (integer) 1 redis:6379> SADD key1 "b" (integer) 1 redis:6379> SADD key1 "c" (integer) 1 redis:6379> SADD key2 "c" (integer) 1 redis:6379> SADD key2 "d" (integer) 1 redis:6379> SADD key2 "e" (integer) 1 redis:6379> SUNION key1 key2 1) "a" 2) "b" 3) "d" 4) "c" 5) "e"
6.SortedSet类型命令
-
ZADD
:添加一个或多个元素到sorted set,如果已经存在则更新其score值redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 1 "uno" (integer) 1 redis:6379> ZADD myzset 2 "two" 3 "three" (integer) 2 redis:6379> ZRANGE myzset 0 -1 WITHSCORES 1) "one" 2) "1" 3) "uno" 4) "1" 5) "two" 6) "2" 7) "three" 8) "3"
-
ZREM
:删除sorted set中的一个指定元素redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZADD myzset 3 "three" (integer) 1 redis:6379> ZREM myzset "two" (integer) 1 redis:6379> ZRANGE myzset 0 -1 WITHSCORES 1) "one" 2) "1" 3) "three" 4) "3"
-
ZSCORE
:获取sorted set中的指定元素的score值redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZSCORE myzset "one" "1"
-
ZRANK
:获取sorted set中的指定元素的排名redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZADD myzset 3 "three" (integer) 1 redis:6379> ZRANK myzset "three" (integer) 2 redis:6379> ZRANK myzset "four" (nil)
-
ZCARD
:获取sorted set中的元素个数redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZCARD myzset (integer) 2
-
ZCOUNT
:统计score值在给定范围内的所有元素的个数redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZADD myzset 3 "three" (integer) 1 redis:6379> ZCOUNT myzset -inf +inf (integer) 3 redis:6379> ZCOUNT myzset (1 3 (integer) 2
-
ZINCRBY
:让sorted set中的指定元素自增,步长为指定值redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZINCRBY myzset 2 "one" "3" redis:6379> ZRANGE myzset 0 -1 WITHSCORES 1) "two" 2) "2" 3) "one" 4) "3"
-
ZRANGE
:按照score排序后,获取指定排名范围内的元素redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZADD myzset 3 "three" (integer) 1 redis:6379> ZRANGE myzset 0 -1 1) "one" 2) "two" 3) "three" redis:6379> ZRANGE myzset 2 3 1) "three" redis:6379> ZRANGE myzset -2 -1 1) "two" 2) "three"
-
ZRANGEBYSCORE
:按照score排序后,获取指定score范围内的元素redis:6379> ZADD myzset 1 "one" (integer) 1 redis:6379> ZADD myzset 2 "two" (integer) 1 redis:6379> ZADD myzset 3 "three" (integer) 1 redis:6379> ZRANGEBYSCORE myzset -inf +inf 1) "one" 2) "two" 3) "three" redis:6379> ZRANGEBYSCORE myzset 1 2 1) "one" 2) "two" redis:6379> ZRANGEBYSCORE myzset (1 2 1) "two" redis:6379> ZRANGEBYSCORE myzset (1 (2 (empty array)
以上是Redis基本数据类型的一部分简单命令,其中的一些命令会随着Redis版本的迭代而发生变化,更多信息可以查看Redis官网对各种命令的详细介绍。Redis官网-命令介绍