Summary of Linux Redis Daily Work Orders
– Locate the Redis installation directory using the 'whereis' command.
whereis redis
– Find the Redis client installation path with:
whereis redis-cli
– Determine the Redis server installation location by running:
whereis redis-server
– Start the Redis server and client from the Redis installation directory, such as /usr/local/redis/bin
.
To start the Redis server:
./redis-server
To launch the Redis client:
./redis-cli
KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX Commands:
– Launch the Redis CLI from the shell terminal.
redis-cli
– Clear the current database to make it easier to follow the examples.
redis 127.0.0.1:6379> flushdb
OK
– Add some sample data in String type.
redis 127.0.0.1:6379> set mykey 2
OK
redis 127.0.0.1:6379> set mykey2 "hello"
OK
– Insert sample data for Set type.
redis 127.0.0.1:6379> sadd mysetkey 1 2 3
(integer) 3
– Add hash-type simulation data.
redis 127.0.0.1:6379> hset mmtest username "stephen"
(integer) 1
– Use the KEYS command to find all keys matching a pattern. Note that this command does not differentiate between key types.
redis 127.0.0.1:6379> keys my*
1) "mysetkey"
2) "mykey"
3) "mykey2"
– Delete two keys for testing.
redis 127.0.0.1:6379> del mykey mykey2
(integer) 2
– Check if one of the deleted keys still exists. The result shows that mykey has been removed.
redis 127.0.0.1:6379> exists mykey
(integer) 0
– Verify the remaining key to compare results.
redis 127.0.0.1:6379> exists mysetkey
(integer) 1
– Move the key 'mysetkey' to database ID 1 and confirm the move was successful.
redis 127.0.0.1:6379> move mysetkey 1
(integer) 1
– Switch to database 1 to check the moved key.
redis 127.0.0.1:6379> select 1
OK
redis 127.0.0.1:6379[1]> exists mysetkey
(integer) 1
– Return to the default database (ID 0) and verify the key is no longer present.
redis 127.0.0.1:6379[1]> select 0
OK
redis 127.0.0.1:6379> exists mysetkey
(integer) 0
– Prepare new test data.
redis 127.0.0.1:6379> set mykey "hello"
OK
– Rename 'mykey' to 'mykey1'.
redis 127.0.0.1:6379> rename mykey mykey1
OK
– After renaming, querying 'mykey' returns nil.
redis 127.0.0.1:6379> get mykey
(nil)
– Retrieve the value using the new key name.
redis 127.0.0.1:6379> get mykey1
"hello"
– Attempting to rename an already renamed key will return an error.
redis 127.0.0.1:6379> rename mykey mykey1
(error) ERR no such key
– Prepare test keys for RENAMENX.
redis 127.0.0.1:6379> set oldkey "hello"
OK
redis 127.0.0.1:6379> set newkey "world"
OK
– Try to rename 'oldkey' to 'newkey', which fails because 'newkey' already exists.
redis 127.0.0.1:6379> renamenx oldkey newkey
(integer) 0
– Check the value of 'newkey' to confirm it was not overwritten.
redis 127.0.0.1:6379> get newkey
"world"
Class 2 Power Adapter,Class 2 Power Adaptor 24V,1.5Amp Class 2 Adaptor,1.5A Switching Adaptor
ShenZhen Yinghuiyuan Electronics Co.,Ltd , https://www.yhypoweradapter.com