Redis(2.8.3) 命令学习 - Keys

Delete a key

127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> DEL foo hello
(integer) 1
127.0.0.1:6379> EXISTS foo
(integer) 0

 

DUMP key

Return a serialized version of the value stored at the specified key.

127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> DUMP foo
"\x00\x05hello\x06\x00\xf5\x9f\xb7\xf6\x90a\x1c\x99"

 

EXISTS key

Determine if a key exists

127.0.0.1:6379> EXISTS foo
(integer) 0
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> EXISTS foo
(integer) 1

 

EXPIRE key seconds

Set a key's time to live in seconds

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> EXPIRE foo 10
(integer) 1
127.0.0.1:6379> TTL foo
(integer) 5
127.0.0.1:6379> GET foo
"hello"
127.0.0.1:6379> TTL foo
(integer) -2
127.0.0.1:6379> GET foo
(nil)
复制代码

 

EXPIREAT key timestamp

Set the expiration for a key as a UNIX timestamp

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> TIME
1) "1427471335"
2) "585724"
127.0.0.1:6379> EXPIREAT foo 1427471400
(integer) 1
127.0.0.1:6379> TIME
1) "1427471420"
2) "109363"
127.0.0.1:6379> GET foo
(nil)
复制代码

 

KEYS pattern

Find all keys matching the given pattern

复制代码
127.0.0.1:6379http://www.bj999.cn/yinchan/> SET foo 1
OK
127.0.0.1:6379> SET bar 2
OK
127.0.0.1:6379> SET far 3
OK
127.0.0.1:6379> SET bat 4
OK
127.0.0.1:6379> KEYS ba?
1) "bat"
2) "bar"
127.0.0.1:6379> KEYS f*
1) "far"
2) "foo"
复制代码

 

MIGRATE host port key destination-db timeout [COPY] [REPLACE]

Atomically transfer a key from a Redis instance to another one.

More: http://redis.io/commands/migrate, http://www.bj999.cn/yinchan/11284.shtml,http://www.redis.cn/commands/migrate.html


MOVE key db

Move a key to another database

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> MOVE foo 1
(integer) 1
127.0.0.1:6379> GET foo
(nil)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> GET foo
"hello"
复制代码

 

OBJECT subcommand [arguments [arguments ...]]

Inspect the internals of Redis objects

More: http://redis.io/commands/object


PERSIST key

Remove the expiration from a key

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> EXPIRE foo 10
(integer) 1
127.0.0.1:6379> TTL foo
(integer) 7
127.0.0.1:6379> PERSIST foo
(integer) 1
127.0.0.1:6379> TTL foo
(integer) -1
复制代码

 

PEXPIRE key milliseconds

Set a key's time to live in milliseconds

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> PEXPIRE foo 10000
(integer) 1
127.0.0.1:6379> PTTL foo
(integer) 5937
127.0.0.1:6379> GET foo
"hello"
127.0.0.1:6379> PTTL foo
(integer) -2
127.0.0.1:6379> GET foo
(nil)
复制代码

 

PEXPIREAT key milliseconds-timestamp

Set the expiration for a key as a UNIX timestamp specified in milliseconds

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> TIME
1) "1427475623"
2) "105070"
127.0.0.1:6379> PEXPIREAT foo 1427475723105
(integer) 1
127.0.0.1:6379> GET foo
"hello"
127.0.0.1:6379> TIME
1) "1427475724"
2) "402347"
127.0.0.1:6379> GET foo
(nil)
复制代码


PTTL key

Get the time to live for a key in milliseconds

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> PTTL foo
(integer) -1
127.0.0.1:6379> PEXPIRE foo 10000
(integer) 1
127.0.0.1:6379> PTTL foo
(integer) 8658
127.0.0.1:6379> GET foo
"hello"
127.0.0.1:6379> PTTL foo
(integer) -2
127.0.0.1:6379> GET foo
(nil)
复制代码

 

RANDOMKEY

Return a random key from the keyspace

复制代码
127.0.0.1:6379> MSET foo1 1 foo2 2 foo3 3 foo4 4 foo5 5
OK
127.0.0.1:6379> KEYS *
1) "foo1"
2) "foo4"
3) "foo2"
4) "foo5"
5) "foo3"
127.0.0.1:6379> RANDOMKEY
"foo2"
127.0.0.1:6379> RANDOMKEY
"foo1"
127.0.0.1:6379> RANDOMKEY
"foo5"
复制代码

 

RENAME key newkey

Rename a key

127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> RENAME foo bar
OK
127.0.0.1:6379> GET bar
"hello"

 

RENAMENX key newkey

Rename a key, only if the new key does not exist

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> SET bar 0
OK
127.0.0.1:6379> RENAMENX foo bar
(integer) 0
127.0.0.1:6379> GET bar
"0"
127.0.0.1:6379> RENAME foo boo
OK
127.0.0.1:6379> GET boo
"hello"
复制代码

 

RESTORE key ttl serialized-value [REPLACE]

Create a key using the provided serialized value, previously obtained using DUMP.

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> DUMP foo
"\x00\x05hello\x06\x00\xf5\x9f\xb7\xf6\x90a\x1c\x99"
127.0.0.1:6379> DEL foo
(integer) 1
127.0.0.1:6379> RESTORE foo 0 "\x00\x05hello\x06\x00\xf5\x9f\xb7\xf6\x90a\x1c\x99"
OK
127.0.0.1:6379> GET foo
"hello"
复制代码

More: http://redis.io/commands/restore


SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]

Sort the elements in a list, set or sorted set

More: http://redis.io/commands/sort


TTL key

Get the time to live for a key

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> TTL foo
(integer) -1
127.0.0.1:6379> EXPIRE foo 10
(integer) 1
127.0.0.1:6379> TTL foo
(integer) 7
127.0.0.1:6379> GET foo
"hello"
127.0.0.1:6379> TTL foo
(integer) -2
127.0.0.1:6379> GET foo
(nil)
复制代码

 

TYPE key

Determine the type stored at key

复制代码
127.0.0.1:6379> SET foo hello
OK
127.0.0.1:6379> TYPE foo
string
127.0.0.1:6379> LPUSH bar 1
(integer) 1
127.0.0.1:6379> TYPE bar
list
复制代码

 

SCAN cursor [MATCH pattern] [COUNT count]

Incrementally iterate the keys space

More: http://redis.io/commands/scanhttp://www.redis.cn/commands/scan.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值