redis cli quick start tutorial

Redis is a fast key value cache and store. It is useful to get familiar with some handy redis command line interface (cli) commands. Here are some quick start commands which can be handy at times:

Connecting to redis

Connecting to redis db running on a specific host and port

//redis-cli -h [hostname] -p [port] -n [db]
$ redis-cli -h localhost -p 6379 -n 0

Executing a set command

Executing a set command on redis running on localhost:

$ redis-cli set key1 value1
OK

Executing a set command on redis running on localhost using pipe:

echo "set key1 value1" | redis-cli
OK

Listing all dbs

List all databases where at least one key is defined with total and expiring number of keys:

$ echo "INFO keyspace" | redis-cli
db0:keys=3,expires=0,avg_ttl=0
db1:keys=2,expires=1,avg_ttl=3573665

Listing all keys

Listing all keys in redis

echo "KEYS *" | redis-cli
1) "mykey"
2) "myley2"
3) "key1"

Listing all keys and types

Listing all keys and types in single command using xargs

$ redis-cli KEYS \* | xargs -n 1 -t redis-cli TYPE

Outcome:

redis-cli TYPE user:1000
hash
redis-cli TYPE mykey
string
redis-cli TYPE myley2
string
redis-cli TYPE key1
string

Listing all keys and values

Listing all keys and values (works if all values are strings) in single command using xargs

$ redis-cli KEYS \* | xargs -n 1 -t redis-cli GET

Outcome:

redis-cli GET mykey
"10"
redis-cli GET myley2
"val2"
redis-cli GET key1
"value1"

redis currently used memory

Currently used memory by redis in human readable format:

$ redis-cli INFO | grep "used_memory_human"
used_memory_human:1003.70K
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments