Redis is important component when we want to design scalable system. It is used as cache for our system. So, in this article, we will learn how to use some commands in Redis.
Let’s get started.
Table of contents
- How to install redis-cli
- Connect with local/remote redis server
- Get/set commands
- Check commands
- Query commands
- Modification commands
- Wrapping up
How to install redis-cli
-
Install redis-cli on Windows
To install it on Windows, we can visit some webpages to download a redis executable file.
-
Install redis-cli on Ubuntu
Belows are some steps to install redis-cli in Ubuntu.
-
Update the apt package
sudo apt update -
Install redis-tools
sudo apt install -y redis-tools
-
Connect with local/remote redis server
By default, Redis runs without username and password, the default port is: 6379.
# Go to the folder of Redis
# 1st - Run Redis client in local host
redis-cli
# 2nd - Connect to the remote server
redis-cli -h <ip_addr> -p <port_number> -a <password>
Get/set commands
-
Create / Update data
set key value # set if not exist value in key setnx key valueFor example:
set phone_type Iphone -
Get data
get keyFor example:
get phone_type -
Modify key’s name
renamenx <key_name> <new_key_name>For example:
renamenx phone_type phonetype
Check commands
-
Check whether key is existed or not
exists keyFor example:
exists phone_type
Query commands
-
Find all keys with patterns
keys patternFor example:
keys *typeEspecially, if we want to list all keys in Redis.
keys *Note:
- Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
-
All redis commands are single thread and will block the server. The only difference is that
keyshas the potential of blocking server for longer when querying a large data set.So, with version 2.8 or later, use
scanis a superior alternative tokeysbecause it does not block the server nor does it consume significant resources.redis-cli --scan --pattern '*'
-
Get random key from Redis
randomkey -
Get the data type of the value stored in the key
type keyFor example:
type phone_type -
Get the value of a key depends on its type
# string data type get <key> # hash data type hgetall <key> # list data type lrange <key> 0 -1 # set data type smembers <key> # zset data type zrange <key> 0 -1 withscores
Modification commands
-
Increment value in key
incr keyFor example:
set num 2 incr num -
Increment the integer value of a key by the given amount
incrby key incrementFor example:
incrby num 5 -
Increment the float value of a key by the given amount
INCRBYFLOAT key increment -
Decrement the integer value of key by one
decr keyFor example:
decr num -
Decrement the integer value of a key by the given number
decrby key decrementFor example:
decrby num 4 -
Delete key
DEL key -
Set expiration time for key
expire key <expiration_time_ms> -
Returns the number of seconds until a key is deleted
ttl key -
Clear expired time of key
persist key
Wrapping up
- Understanding about the commands of Redis.
Refer:
https://gist.github.com/LeCoupa/1596b8f359ad8812c7271b5322c30946