Home >Backend Development >PHP Tutorial >Comprehensive list of commonly used Redis commands
Redis is a commonly used memory-based Key-Value database. It is more advanced than Memcache and supports multiple data structures. It is efficient and fast. Redis can easily solve high-concurrency data access problems; it is also very good for real-time monitoring and signal processing.
Note: In the following commands, the SHELL command is after the $ sign, and the Redis command is after the > sign.
Enter redis-cli on the command line to start the Redis client.
1. Access and view
$ redis-cli redis 127.0.0.1:6379> > help # 命令行的帮助 > keys * # 查看所有的key列表 > info # 查看服务器信息。如占用系统内存,看其中的used_memory_human值 > select 2 # 切换到数据库2
2. String record command
Add string and numeric records
> set key1 "hello" # 增加一条键值为key1,值为"hello"的字符串记录 > get key1 # 获取记录值 > set key2 1 # 增加一条数字记录key2 > INCR key2 # 让数字自增
3. List record command
> LPUSH key3 a # 增加一个列表记录key3 > LPUSH key3 b # 从左边插入列表 > RPUSH key3 c # 从右边插入列表 > LRANGE key3 0 3 # 输出列表记录,按从左到右的顺序
4. Hash table record command
> HSET key4 name "John Smith" # 增加一个哈希记表录key4 > HSET key4 email "abc@gmail.com" # 在哈希表中插入,email的Key和Value的值 > HGET key4 name # 输出哈希表中键为name的值 > HMSET key5 username antirez password P1pp0 age 3 # 增加一条哈希表记录key5,一次插入多个Key和value的值 > HMGET key5 username age # 打印哈希表中,键为username和age的值 > HGETALL key5 # 打印完整的哈希表记录
5. Delete record
> del key1 # 删除key1 > flushdb # 删除当前库的所有数据 > flushall # 删除所有数据库下的所有数据
6. Batch deletion
The del command of Redis does not support wildcards. Batch deletion can be achieved by combining Linux pipes and xargs commands:
$ redis-cli del `redis-cli keys "user:*"` # 删除以键名为user:开头的所有记录 $ redis-cli keys "user:*" | xargs redis-cli del # 同上 $ redis-cli -a password keys "user:*" | xargs redis-cli -a password del # 同上,有密码 $ redis-cli -n 0 keys "user:*" | xargs redis-cli -n 0 del # 删除数据库序号为0的库里面的指定记录
The above is the detailed content of Comprehensive list of commonly used Redis commands. For more information, please follow other related articles on the PHP Chinese website!