Redis CLI

Redis is an open source in-memory key value data store. Can be used as database and cache. It has clients for big variety of languages. Here is the list of snippets for most common Redis operations using CLI.

For full list of commands refer to https://redis.io/commands

Supported data types

  • Cluster
  • Connection
  • Geo
  • Hashes
  • HyperLogLog
  • Keys
  • Lists
  • Pub/Sub
  • Scripting
  • Server
  • Sets
  • Sorted Sets
  • Streams
  • Strings
  • Transactions

Installation & running CLI

I’m using Ubuntu, if you use other operation system you can refer to https://redis.io/ on how to install it.

$ sudo apt install redis-server
# configuration location /etc/redis/redis.conf
# running cli
$ redis-cli
127.0.0.1:6379>

HELP

Display all functions on a specific type

> HELP @string

Basic commands

# check if connection is alive
> PING
PONG

# the commands are case insensitive, echo and ECHO both ok, but the convention is capital letters.
> ECHO 'hello world'
"hello world"

# close the connection Ctrl + D or type QUIT
> QUIT

SET and GET

> SET foo 100
OK
> GET foo
"100"
> SET user:name "John"
OK
> GET user:name
"John"

INC and DECR

# INCR and DECR
> INCR foo
(integer) 101
> GET foo
"101"
> DECR foo
(integer) 100
"100"

EXISTS & DEL

> SET foo 50
> EXISTS foo
(integer) 1
> EXISTS asd
(integer) 0
> DEL foo
> EXISTS foo
(integer) 0
> GET foo
(nil)

FLUSHALL

Deletes all entirely

> FLUSHALL
OK

EXPIRE, TTL, SETEX AND PERSIST

You can set expiration in second with EXPIRE and get the current TTL (time to live)

> SET foo "Hello"
OK
> EXPIRE foo 50
(integer) 1
> TTL foo
(integer) 42
> EXISTS foo
(integer) 1
> TTL foo
(integer) 16
> EXISTS foo
(integer) 0
> TTL foo
(integer) -2 # -2 means expired

To set expiration with the SET statement use SETEX

> SETEX foo 50 "Hello"

To remove expiration use PERSIST

> SETEX foo 50 "Hello"
OK
> TTL foo
(integer) 41
> PERSIST foo
(integer) 1
> TTL foo
(integer) -1 # -1 means never expire
> GET foo
"Hello"

MSET

Set more then one key pair

> MSET foo "1" bar "abc"

APPEND

> SET foo "Hello"
OK
> APPEND foo " world"
(integer) 11
> GET foo
"Hello world"

RENAME

Rename a variable

> RENAME foo bar
OK

List

List common commands. For full reference go to https://redis.io/commands#list

# LPUSH add element from start of list
> LPUSH users 'John'
(integer) 1
> LPUSH users 'Dana'
(integer) 2
> LPUSH users 'Jeck'
(integer) 3

# LRANGE - show elements from to position
> LRANGE users 0 -1
1) "Jeck"
2) "Dana"
3) "John"

# RPUSH - add element to end of list
> RPUSH users 'Nick'
(integer) 4

# LLEN - list length
> LLEN users
(integer) 4

# LPOP and RPOP - pop from left / right
> LPOP users
"Jeck"
> RPOP users
"Nick"

# LINSERT - insert in a specific position
> LINSERT users BEFORE 'John' 'Moshe'
(integer) 3

Sets

# SAD - add to set
> SADD brands adidas
(integer) 1
> SADD brands nike
(integer) 1

# SISMEMBER - check if member of a list
> SISMEMBER brands nike
(integer) 1

# SMEMBERS - display all members of a set
> SMEMBERS brands
1) "adidas"
2) "nike"

# SCARD - show number of elements
> SCARD brands
(integer) 2

# SMOVE - move from one list to another
> SMOVE brands mybrands nike
(integer) 1

# SREM - remove element from a set
> SREM mybrands nike
(integer) 1

# SDIFF - show diff between 2 sets
> SDIFF brands mybrands
1) "adidas"

Sorted Sets

The sort is done by score that you give to each member

# ZADD - add a member to sorted list and assign it a score
> ZADD os 10 'win'
(integer) 1
> ZADD os 20 'macos'
(integer) 1
> ZADD os 30 'linux'
(integer) 1


# ZRANK - get the order position of member in set
> ZRANK os macos
(integer) 1
> ZRANK os win
(integer) 0

# ZRANGE - print members from to position
> ZRANGE os 0 -1
1) "win"
2) "macos"
3) "linux"

# ZINCRBY - increment score to move an member in set
> ZINCRBY os 15 win
"25"
> ZRANGE os 0 -1
1) "macos"
2) "win"
3) "linux"

Hashes

Hashes is an object type. Like object it can have a attributes.

# HSET add attribute to existing or new hash object
127.0.0.1:6379> HSET user:joe name "Joe Doe"
(integer) 1
127.0.0.1:6379> HSET user:joe email "joe@gmail.com"
(integer) 1

# HGET - get an attribute value
127.0.0.1:6379> HGET user:joe name
"Joe Doe"

# HGETALL - get all attributes of an object
127.0.0.1:6379> HGETALL user:joe
1) "name"
2) "Joe Doe"
3) "email"
4) "joe@gmail.com"

# HMSET - set multiple attributes to an object
127.0.0.1:6379> HMSET user:dan name "Dan Woo" email "dan@gmail.com"
OK

# HKEYS - get all keys of an object
127.0.0.1:6379> HKEYS user:dan
1) "name"
2) "email"

# HVALS - get all values of an object
127.0.0.1:6379> HVALS user:dan
1) "Dan Woo"
2) "dan@gmail.com"

# HDEL - delete an attribute from an object
127.0.0.1:6379> HDEL user:dan email
(integer) 1

# HLEN - get number of attributes
127.0.0.1:6379> HLEN user:dan
(integer) 1

Persistence

There are number of options. Please refer to https://redis.io/topics/persistence. Set the persistence option in /etc/redis/redis.conf

One thought on “Redis CLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s