Redis

9 Notes
+ Listing Connections (Oct. 26, 2019, 10:43 a.m.)

CLIENT LIST -------------------------------------------------------- You can terminate connections with: CLIENT KILL <IP>:<port> --------------------------------------------------------

+ Dump Database Backup (Oct. 26, 2019, 10:43 a.m.)

As Redis allows RDB database dumps in background, you can issue a dump at any time. Just run: BGSAVE -------------------------------------------------------- When running this command Redis will fork and the new process will dump into the "dbfilename" configured in the Redis configuration without the original process being blocked. Of course the fork itself might cause an interruption. Use "LASTSAVE" to check when the dump file was last updated. For a simple backup solution just backup the dump file. If you need a synchronous save run "SAVE" instead of "BGSAVE". --------------------------------------------------------

+ Databases (Oct. 26, 2019, 10:42 a.m.)

Redis has a concept of separated namespaces called "databases". You can select the database number you want to use with "SELECT". By default the database with index 0 is used. So issuing SELECT 1 switches to the second database. Note how the prompt changed and now has a "[1]" to indicate the database selection. To find out how many databases there are you might want to run redis-cli from the shell: redis-cli INFO | grep ^db ------------------------------------------------------------- Dropping Databases:Dropping Databases To drop the currently selected database run: FLUSHDB To drop all databases at once run: FLUSHALL --------------------------------------------------------

+ Server Statistics (Oct. 26, 2019, 10:42 a.m.)

$ redis-cli INFO

+ CLI Scripting (Oct. 26, 2019, 10:42 a.m.)

For scripting just pass commands to "redis-cli". For example: redis-cli INFO | grep connected

+ Queries (Oct. 26, 2019, 10:41 a.m.)

monitor -------------------------------------------------------- Slow Queries: # print top 25 slow queries slowlog get 25 slowlog len slowlog reset -------------------------------------------------------- Search Keys: # Find key matching exactly keys pattern # Find keys matching in back keys *pattern # Find keys matching somewhere keys *pattern* # Find keys matching in front keys pattern* -------------------------------------------------------- Generic: del <key> dump <key> # Serialize key exists <key> expire <key> <seconds> -------------------------------------------------------- Scalars: get <key> set <key> <value> # Set key value only if key does not exist setnx <key> <value> Batch commands: mget <key> <key> ... mset <key> <value> <key> <value> ... Counter commands: incr <key> decr <key> -------------------------------------------------------- Lists: lrange <key> <start> <stop> # Get all of a list lrange mylist 0 -1 # Get by index lindex mylist 5 # Get length llen mylist lpush mylist "value" lpush mylist 5 rpush mylist "value" # Only push in mylist exists lpushx mylist 6 rpushx mylist 0 lpop mylist rpop mylist # Remove 'value' count times lrem mylist 1 "value" # mylist[2] = 6 lset mylist 2 6 ltrim <key> <start> <stop> -------------------------------------------------------- Hashes: # Check if hash key exists hexists myhash field1 hget myhash field1 hdel myhash field2 hset myhash field1 "value" hsetnx myhash field1 "value" hgetall myhash hkeys myhash hlen myhash Batch commands: hmget <key> <key> ... hmset <key> <value> <key> <value> ... Counter commands: hincrby myhash field1 1 hincrby myhash field1 5 hincrby myhash field1 -1 hincrbrfloat myhash field2 1.123445 --------------------------------------------------------

+ Dictionary (Oct. 26, 2019, 10:41 a.m.)

redis_db = redis.StrictRedis(host="localhost", port=6379, db=3, decode_responses=True) redis_db.hmset('mohsen', {'name': 'Mohsen', 'age': 33, 'friend': 'Hadi'}) redis_db.hgetall('mohsen') redis_db.hget('mohsen', 'age')

+ Usage (Oct. 26, 2019, 10:40 a.m.)

import redis redis_db = redis.StrictRedis(host="localhost") or redis_db = redis.StrictRedis(host="localhost", port=6379, db=0) or redis_db = redis.StrictRedis(host="localhost", port=6379, db=0, decode_responses=True) redis_db.keys() redis_db.delete('twilio')

+ Installation (Oct. 26, 2019, 10:39 a.m.)

Redis: Open source in-memory data structure store -------------------------------------------------------- apt install redis-server To access Redis command prompt: redis-cli -------------------------------------------------------- pip install redis --------------------------------------------------------