Redis (12) redis key (KEY)

Redis key commands are used to manage redis keys.

This knowledge point has been ignored by me. When I tested the data type of redis a few days ago, I was thinking that I built so many keys to store the corresponding data during the test, although the final data was cleared by me. , But the key should still exist.

How to delete this stuff? If I want to check, I have defined those keys. Does this thing have to have a command?

Time is tight, and I didn't think much about it.

I saw this redis key command today, oh, so you are here.

Roughly look at a few commonly used redis-key commands:

One: linux command

1: del delete key, return 1 on success, return 0 on failure

del hash-key               // 返回(integer) 1

2: exists to check whether a key exists, return 1 if it exists, return 0 if it does not exist

exists hash-key            // (integer) 0,我们上边删除了

3: type returns the key storage value type

Set the key string to store string type data

set string 'camellia'              // 返回OK

View the type of data stored in the key string

type string                          // 返回string

4: rename modify the name of the specified key

Change the name of the key string to str

rename string str                 // 返回OK

Query whether the key string exists

exists string                         // 返回(integer) 0,不存在,因为改名了

Query whether the key str exists

exists str                             // 返回 (integer) 1,存在

5: keys view all the keys in redis or the keys that meet the corresponding conditions

Search key str

keys str

Output:

1)     "str"

Find key st

keys st

Output:

(empty list or set)

Find all keys starting with st

keys st*

Output:

1) "string"
2) "str"

View all keys in redis

keys *

Output:

 1) "time"
 2) "111.206.221.110time"
 3) "111.206.198.77time"
 4) "123.125.71.80time"
 5) "test"

6: expire Set the expiration time of the key, calculated in seconds

Set the expiration time of the key str to 300 seconds

expire str 300        // 返回(integer) 1

7: persist removes the expiration time of the current key setting

persist str             // 返回(integer) 1

Two: PHP commands

1: del, return 1 on success, return 0 on failure

$res = r e d i s − > d e l ( ′ h a s h − k e y ′ ) ; v a r d u m p ( redis->del('hash-key'); var_dump( r e d i s ->d and l (hashkey);v a rdump(res);

2: exists, returns 1 if exists, 0 if failure

$res = r e d i s − > e x i s t s ( ′ t e s t ′ ) ; v a r d u m p ( redis->exists('test'); var_dump( r e d i s ->exists(test);v a rdump(res);

3: type, this command is different from linux, the execution in linux directly returns the type, and the execution of this command in PHP returns a number of 0-5, which respectively represent:

0不存在

1字符串

2集合

3列表

4有序集合

5哈希表
$res = $redis->type('miaoshaceshi');
var_dump($res);       // 返回3

4: rename modifies the name of the key, returns true on success, false on failure

$res = $redis->rename('test','te');
    var_dump($res);

5: keys find all keys, or find the keys that meet the corresponding conditions

$res = $redis->keys('*');
    var_dump($res);

Output:

array(32) {
    
     [0]=> string(13) "54.36.148.248" [1]=> string(4) "time" [2]=> string(19) "111.206.221.110time" [3]=> string(18) "111.206.198.77time" [4]=> string(17) "123.125.71.80time" [5]=> string(18) "216.244.66.238time"}

6: expire Set the expiration time, return true for success, false for failure

$res = $redis->expire('te',300);
var_dump($res);

7: persist removes the expiration time of the corresponding key value, it returns true if it succeeds, and false if it fails

$res = $redis->persist('te');
var_dump($res);

Here are some commonly used redis-key commands. If you are interested, you can try it yourself:

Serial number

Command and description

1

DEL key
  该命令用于在 key 存在时删除 key。

2

DUMP key 
  序列化给定 key ,并返回被序列化的值。

3

EXISTS key 
  检查给定 key 是否存在。

4

EXPIRE key seconds
  为给定 key 设置过期时间,以秒计。

5

EXPIREAT   key timestamp 
  EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)

6

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

7

PEXPIREAT   key milliseconds-timestamp 
  设置 key 过期时间的时间戳(unix timestamp) 以毫秒计

8

KEYS pattern 
  查找所有符合给定模式( pattern)的 key 。

9

MOVE key db 
  将当前数据库的 key 移动到给定的数据库 db 当中。

10

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

11

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

12

TTL key 
  以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)

13

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

14

RENAME key newkey 
  修改 key 的名称

15

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

16

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

Have a good suggestion, please enter your comment below.

Welcome to personal blog
https://guanchao.site

Welcome to the Mini Program:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39708228/article/details/113674757
Recommended