redis通用API

DEL 删除key

Redis DEL command is used to delete the existing key in Redis.

Syntax 语法

redis 127.0.0.1:6379> DEL KEY_NAME 

Example 例子

redis 127.0.0.1:6379> SET tutorialspoint redis 
OK
redis 127.0.0.1:6379> DEL tutorialspoint 
(integer) 1

DUMP 序列化key

Redis DUMP command is used to get a serialized version of data stored at specified key in Redis.

Syntax 语法

redis 127.0.0.1:6379> DUMP KEY_NAME

Example 例子

redis 127.0.0.1:6379> SET tutorialspoint redis 
OK
redis 127.0.0.1:6379> DUMP tutorialspoint 
"\x00\x05redis\x06\x00S\xbd\xc1q\x17z\x81\xb2"

EXISTS 是否存在key

Redis EXISTS command is used to check whether the key exists in Redis or not.

Syntax 语法

redis 127.0.0.1:6379> EXISTS KEY_NAME

Example 例子

redis 127.0.0.1:6379> EXISTS tutorialspoint-new-key 
(integer) 0

Now, create a key with the name tutorialspoint-new-key and check for its existence.

redis 127.0.0.1:6379> EXISTS tutorialspoint-new-key 
(integer) 1

Expire 设置过期时间

Redis Expire command is used to set the expiry of a key. After the expiry time, the key will not be available in Redis.

Syntax 语法

Following is the basic syntax of Redis Expire command.

redis 127.0.0.1:6379> Expire KEY_NAME TIME_IN_SECONDS

Example 例子

First, create a key in Redis and set some value in it.

redis 127.0.0.1:6379> SET tutorialspoint redis 
OK 

Now , set timeout of the previously created key

redis 127.0.0.1:6379> EXPIRE tutorialspoint 60 
(integer) 1

KEYS 获取所有的key或指定前缀的key

Redis KEYS command is used to search keys with a matching pattern.

Syntax 语法

redis 127.0.0.1:6379> KEYS PATTERN

Example 例子

First, create a key in Redis and set some value in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 
redis 127.0.0.1:6379> SET tutorial2 mysql 
OK 
redis 127.0.0.1:6379> SET tutorial3 mongodb 
OK 

Now, search Redis with keys starting from the keyword tutorial.

redis 127.0.0.1:6379> KEYS tutorial* 
1) "tutorial3" 
2) "tutorial1" 
3) "tutorial2" 

To get a list of all the keys available in Redis, use only

redis 127.0.0.1:6379> KEYS * 
1) "tutorial3" 
2) "tutorial1" 
3) "tutorial2" 

MOVE

Redis MOVE command is used to move a key from the currently selected database to the specified destination database.
将key从一个数据库移动到另一个数据库

Synatax 语法

redis 127.0.0.1:6379> MOVE KEY_NAME DESTINATION_DATABASE 

Example 例子

First, create a key in Redis and set some value in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 

In Redis, by default 0th database is selected, so now we are moving the generated key in the second database.

redis 127.0.0.1:6379> MOVE tutorial1 1 
1) (integer) 1 

PERSIST

Redis PERSIST command is used to remove the expiration from the key.
移除过期时间

Synatax 语法

redis 127.0.0.1:6379> PERSIST KEY_NAME

Example 例子

First, create a key in Redis and set some value in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 

Now, set the expiry of the key, and later remove the expiry.

redis 127.0.0.1:6379> EXPIRE tutorial1 60 
1) (integer) 1 
redis 127.0.0.1:6379> TTL tutorial1 
1) (integer) 60 
redis 127.0.0.1:6379> PERSIST tutorial1 
1) (integer) 1 
redis 127.0.0.1:6379> TTL tutorial1 
1) (integer) -1

TTL

Redis TTL command is used to get the remaining time of the key expiry in seconds.

Synatax 语法

redis 127.0.0.1:6379> TTL KEY_NAME 

Example 例子

First, create a key in Redis and set some value in it.

redis 127.0.0.1:6379> SET tutorialname redis 
OK 

Now, set the expiry of the key, and later check the remaining expiry time.

redis 127.0.0.1:6379> EXPIRE tutorialname 60 
1) (integer) 1 
redis 127.0.0.1:6379> TTL tutorialname 
1) (integer) 59

RANDOMKEY

Redis RANDOMKEY command is used to get a random key from Redis database.
随机访问

Synatax 语法

redis 127.0.0.1:6379> RANDOMKEY 

Example 例子

First, create some keys in Redis and set some values in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 
redis 127.0.0.1:6379> SET tutorial2 mysql 
OK 
redis 127.0.0.1:6379> SET tutorial3 mongodb 
OK

Now, get a random key from Redis.

redis 127.0.0.1:6379> RANDOMKEY 
1) tutorial3 

RENAME

Redis RENAME command is used to change the name of a key.

Synatax 语法

redis 127.0.0.1:6379> RENAME OLD_KEY_NAME NEW_KEY_NAME 

Example 例子

First, create some keys in Redis and set some values in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 

Now, rename the key ‘tutorial1’ to ‘new-tutorial’.

redis 127.0.0.1:6379> RENAME tutorial1 new-tutorial 
OK 

RENAMENX

Redis RENAMENX command is used to change the name of a key, if the new key does not exist.

Synatax 语法

redis 127.0.0.1:6379> RENAMENX OLD_KEY_NAME NEW_KEY_NAME

Example 例子

First, create some keys in Redis and set some values in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 
redis 127.0.0.1:6379> SET tutorial2 mongodb 
OK

Now, rename the key ‘tutorial1’ to ‘new-tutorial’.

redis 127.0.0.1:6379> RENAMENX tutorial1 new-tutorial 
(integer) 1 
redis 127.0.0.1:6379> RENAMENX tutorial2 new-tutorial 
(integer) 0

TYPE

Redis TYPE command is used to get the data type of the value stored in the key.
获取key对应的value数据类型

Synatax 语法

redis 127.0.0.1:6379> TYPE KEY_NAME 

Example 例子

First, create some keys in Redis and set some values in it.

redis 127.0.0.1:6379> SET tutorial1 redis 
OK 

Now, check the type of the key.

redis 127.0.0.1:6379> TYPE tutorial1 
string 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值