SoFunction
Updated on 2024-07-16

redis View all key methods

You can use the KEYS command

KEYS pattern

for example

List all keys

redis> keys *

List matching keys

redis>keys apple*
1) apple1
2) apple2

Supplementary: common commands for redis - KEY

Redis is an open source (BSD-licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial index radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of disk persistence, and provides a variety of features through Redis Sentinel and Cluster. Sentinel and Cluster to provide high availability.

However, the most commonly used commands are those related to key and the five major data types, key, strings, hashes, lists, sets, and sorted sets. In this section, let's look at the commands related to KEY.

key:

1、KEYS pattern

Find all keys that match the given pattern, keys * View all cached keys

2、DEL key1 key2

Delete the specified caches (one or more)

3、DUMP key

Derive the value of key, if key does not exist, then return nil; otherwise, return the serialized value.

"\x00\x02v3\b\x00\xf1*K%b\xcd\x8e\xa0"

4、EXISTS key

Queries whether a key exists

5、EXPIRE key 20

Set the number of seconds a key will expire (in seconds)

6、TTL key

Get the valid time of the key (in seconds), if the key does not exist, return -2, it is recommended to test the effect with EXPIRE key.

7、EXPIRE key 20000

Setting the validity time of the key (in milliseconds)

8、PTTL key

Get the number of valid milliseconds for the key (in milliseconds)

9、RENAME key newkey

Rename a key, if the newkey already exists, then use the value of the renamed key to overwrite to the value of the old newkey that already exists, see my test below

10、RENAMENX key newkey

Rename a key, the new key must be a non-existing key

Returns 1 if the modification was successful. Returns 0 if the newkey already exists.

11、RANDOMKEY

Returns a random key

12、TYPE key

Get the storage type of the key

13、MOVE key db

Move the key to another database

What does that mean? We vim , /databases to see that the

From the comments, we know that there are 16 DBs in redis, ranging from 0-15, and the default is D0, which we can specify with the command

SELECT dbid to select a different DB, e.g. select 1; select 15, etc. Look at the following test

14. FLUSHDB and FLUSHALL

We know that there are 16 DBs in redis by default, so FLUSHDB is to clear all the data in the current DB (use with caution) and FLUSHALL is to clear all the data in all the DBs (don't use).

Here's an example to test FLUSHALL.

Little Maple Warm Tips:

1、Each command should be knocked once, not eyeballing it

2, I'm here just commonly used commands, but also need you to go to the official website to expand their own

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.