redis命令操作

  • 安装window版redis
  • 下载地址
  • 解压缩后配置环境变量(不配置也可使用)
  • redis-cli连接远程服务 `redis-cli.exe redis-cli -h host -p port -a password`
  • redis 键key
  • redis 字符串string
  • redis 哈希hash
  • redis 列表list
  • redis 列表set
  • redis 有序集合 sorted set


安装window版redis

下载地址

https://github.com/microsoftarchive/redis/releases https://github.com/microsoftarchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.zip

解压缩后配置环境变量(不配置也可使用)

redis返回数据类型 redis返回0_centos

redis-cli连接远程服务 redis-cli.exe redis-cli -h host -p port -a password

C:\Users\ly-zhengzc>redis-cli.exe -h 172.26.123.154 -p 6379 -a 123456
172.26.123.154:6379> set a 123
OK
172.26.123.154:6379> get a
"123"
172.26.123.154:6379>

redis 键key

exists: 检查给定 key 是否存在, 若 key 存在返回 1 ,否则返回 0 。
del :被删除 key 的数量
expired :为 key 设置过期时间,单位 second
keys : 符合给定模式的 key 列表
pttl :当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以毫秒为单位,返回 key 的剩余生存时间。
ttl :当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key 的剩余生存时间。
type :返回 key 的数据类型,数据类型有:
none (key不存在)
string (字符串)
list (列表)
set (集合)
zset (有序集)
hash (哈希表)

D:\Program Files (x86)\Redis-x64-3.2.100>redis-cli.exe -h centoshost
centoshost:6379> auth 123456
OK
centoshost:6379> exists humanKey
(integer) 0
centoshost:6379> set humanKey man
OK
centoshost:6379> get humanKey
"man"
centoshost:6379> exists humanKey
(integer) 1
centoshost:6379> del humanKey
(integer) 1
centoshost:6379> get humanKey
(nil)
centoshost:6379> set expireKey elephant
OK
centoshost:6379> get expireKey
"elephant"
centoshost:6379> expire expireKey 5
(integer) 1
centoshost:6379> get expireKey
"elephant"
centoshost:6379> get expireKey
(nil)
centoshost:6379> set girl alice
OK
centoshost:6379> set girl2 Mary
OK
centoshost:6379> keys girl*
1) "girl"
2) "girl2"
centoshost:6379> pttl girl
(integer) -1
centoshost:6379> expire girl 20
(integer) 1
centoshost:6379> pttl girl
(integer) 15640
centoshost:6379> ttl girl
(integer) 9
centoshost:6379> ttl girl
(integer) -2
centoshost:6379> pttl girl
(integer) -2
centoshost:6379> keys girl*
1) "girl2"
centoshost:6379> type girl2
string

redis 字符串string

centoshost:6379> set testKey apple
OK
centoshost:6379> get testKey
"apple"
centoshost:6379> set testKey pear
OK
centoshost:6379> get testKey
"pear"

getset: 返回给定 key 的旧值。 当 key 没有旧值时,即 key 不存在时,返回 nil 。
当 key 存在但不是字符串类型时,返回一个错误。

centoshost:6379> getset testKey grape
"pear"
centoshost:6379> getset msg hello,Alice
(nil)
centoshost:6379> get msg
"hello,Alice"
centoshost:6379>

mget: 一个包含所有给定 key 的值的列表。
mset :同时设置一个或多个 key-value 对

centoshost:6379> mget testKey msg otherKey
1) "grape"
2) "hello,Alice"
3) (nil)
centoshost:6379> mset key1 apple key2 pear key3 grape
OK
centoshost:6379> mget key1 key2 key3
1) "apple"
2) "pear"
3) "grape"

setex:指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。
时间单位为:second

centoshost:6379> setex fruit 5 apple
OK
centoshost:6379> get fruit
"apple"
centoshost:6379> get fruit
(nil)

strlen :字符串值的长度。 当 key 不存在时,返回 0。

centoshost:6379> strlen msg
(integer) 11

incr :将 key 中储存的数字值增一
incrby :将 key 所储存的值加上给定的增量值
incrbyfloat :将 key 所储存的值加上给定的浮点增量值

centoshost:6379> incr testCount
(integer) 1
centoshost:6379> incr testCount
(integer) 2
centoshost:6379> incrby testCount 5
(integer) 7
centoshost:6379> incrbyfloat testCount 3.5
"10.5"

decr :将 key 中储存的数字值减一
decrby :key 所储存的值减去给定的减量值

centoshost:6379> decr testCount2
(integer) -1
centoshost:6379> decrby testCount2 3
(integer) -4

append :追加指定值之后,返回 key 中字符串的长度

centoshost:6379> exists animal
(integer) 0
centoshost:6379> append animal monkey
(integer) 6
centoshost:6379> append animal -007
(integer) 10
centoshost:6379> get animal
"monkey-007"

redis 哈希hash

hexists: 如果哈希表含有给定字段,返回 1 。 如果哈希表不含有给定字段,或 key 不存在,返回 0 。
hset
hget
hdel :被成功删除字段的数量,不包括被忽略的字段。
hgetall :以列表形式返回哈希表的字段及字段值。 若 key 不存在,返回空列表。
hkeys :获取所有哈希表中的字段
hincrby :为哈希表 key 中的指定字段的整数值加上增量 increment 。
hincrbyfloat
hlen :获取哈希表中字段的数量
hmget :一个包含多个给定字段关联值的表,表值的排列顺序和指定字段的请求顺序一样。
hmset :
hvals :一个包含哈希表中所有域(field)值的列表。 当 key 不存在时,返回一个空表。
hscan :

centoshost:6379> hexists myhash fruit
(integer) 0
centoshost:6379> hset myhash fruit pear
(integer) 0
centoshost:6379> hexists myhash fruit
(integer) 1
centoshost:6379> hget myhash fruit
"pear"
centoshost:6379> hdel myhash fruit
(integer) 1
centoshost:6379> hexists myhash fruit
(integer) 0

centoshost:6379> hset myhash field1 1
(integer) 1
centoshost:6379> hset myhash field2 2
(integer) 1
centoshost:6379> hgetall myhash
1) "field1"
2) "1"
3) "field2"
4) "2"
centoshost:6379> hkeys myhash
1) "field1"
2) "field2"
centoshost:6379> hlen myhash
(integer) 2
centoshost:6379> hmget myhash field1 field2 field3
1) "1"
2) "2"
3) (nil)
centoshost:6379> hvals myhash
1) "1"
2) "2"

centoshost:6379> hscan myhash 0 match field*
1) "0"
2) 1) "field1"
   2) "1"
   3) "field2"
   4) "2"

redis 列表list

rpush :将一个或多个值插入到列表的尾部(最右边)。
lpush :将一个或多个值插入到列表的头部(最左边)。
lrange :返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
llen :用于返回列表的长度,如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。
lpop :用于移除并返回列表的第一个元素
rpop :用于移除并返回列表的最后一个元素

centoshost:6379> rpush list1 hello
(integer) 1
centoshost:6379> rpush list1 wonderful
(integer) 2
centoshost:6379> lrange list1 0 -1
1) "hello"
2) "wonderful"
centoshost:6379> lpush list1 cat
(integer) 3
centoshost:6379> lrange list1 0 -1
1) "cat"
2) "hello"
3) "wonderful"
centoshost:6379> llen list1
(integer) 3
centoshost:6379> lpop list1
"cat"
centoshost:6379> rpop list1
"wonderful"

redis 列表set

sadd :命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。
smembers :返回集合中的所有成员
sismember :判断成员元素是否是集合的成员。
scard :集合的数量。 当集合 key 不存在时,返回 0 。
spop :移除并返回集合中的一个随机元素
sscan :用于迭代集合中键的元素

sdiff :返回给定集合之间的差集。不存在的集合 key 将视为空集
sdiffstore :将给定集合之间的差集存储在指定的集合中。如果指定的集合 key 已存在,则会被覆盖。

sinter :返回给定所有给定集合的交集。
sinterstore :将给定集合之间的交集存储在指定的集合中。

centoshost:6379> sadd set1 hello
(integer) 1
centoshost:6379> sadd set1 foo
(integer) 1
centoshost:6379> smembers set1
1) "foo"
2) "hello"
centoshost:6379> scard set1
(integer) 2
centoshost:6379> sismember set1 hello
(integer) 1

redis 有序集合 sorted set

zadd:向有序集合添加一个或多个成员,或者更新已存在成员的分数
zrange
zcard:获取有序集合的成员数
zincrby:有序集合中对指定成员的分数加上增量 increment
zscore:返回有序集中,成员的分数值。

centoshost:6379> zadd z1 1 one
(integer) 1
centoshost:6379> zadd z1 1 two
(integer) 1
centoshost:6379> zadd z1 2 three 3 four
(integer) 2
centoshost:6379> zrange z1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
centoshost:6379> zcard z1
(integer) 4
centoshost:6379> zscore z1 three
"2"
centoshost:6379> zscore z1 two
"1"