The content this article brings to you is about the common operations (code) of keys in Redis. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Enter the directory: cd /usr/local/redis/
Start the service: ./bin/redis-server ./redis.conf
Enter: ./bin/ redis-cli
(1) View keys: keys *
View all keys: 127.0.0.1:6379> keys *
127.0.0.1:6379> keys * 1) "myb1" 2) "mya1" 3) "mya2" 4) "my3" 5) "myhash" 6) "mylist2" 7) "num2" 8) "my1" 9) "num" 10) "mylist3" 11) "mya3" 12) "name" 13) "myb3" 14) "mylist" 15) "my2" 16) "num3" 17) "imooc" 18) "num5" 19) "mylist4" 20) "myb2" 21) "myset" 22) "mysort"
(2) View keys starting with any character: keys string?
Query keys starting with my: keys my?
127.0.0.1:6379> keys my? 1) "my3" 2) "my1" 3) "my2"
(3) Delete key: del
Delete key: del my1 my2 my3
127.0.0.1:6379> del my1 my2 my3 (integer) 3
(4) Check whether the key exists exists
Check whether it exists, 1 represents existence, 0 represents non-existence: exists my1
127.0.0.1:6379> exists my1 (integer) 0 127.0.0.1:6379> exists num3 (integer) 1
(5) Rename the key: rename
Rename the key operation: rename original key name new Key name
127.0.0.1:6379> get num "36" 127.0.0.1:6379> rename num newnum OK 127.0.0.1:6379> get newnum "36"
(6) Set expiration time: expire
Set expiration time: expire key name seconds
127.0.0.1:6379> expire newnum 1000 (integer) 1
( 7) Check the remaining time of the key: ttl
Check the remaining time of the key: ttl key name. If it is not set, a negative value will be returned.
127.0.0.1:6379> expire newnum 1000 (integer) 1 127.0.0.1:6379> ttl newnum (integer) 885
(8) Determine the type of key: type
Determine the type of key: type name of key.
127.0.0.1:6379> type newnum string 127.0.0.1:6379> type my1 none 127.0.0.1:6379> type myhash hash 127.0.0.1:6379> type mylist4 list 127.0.0.1:6379> type mysort zset
Related recommendations:
Use redis queue operation example code in php
Nosql's Redis: key (key) operation command
The above is the detailed content of Common operations of keys in Redis (code). For more information, please follow other related articles on the PHP Chinese website!