📜 ⬆️ ⬇️

Redis Cheat Sheet

About Redis (the official site , materials on Habré ) has been written a lot, but to this day I didn’t have enough material to serve as a cheat sheet for its practical use, as well as a reference book on basic theoretical points. I will try to fill this gap in the rich knowledge base of Habr.

I set a goal to show the capabilities of Redis using code examples. After the publication I will accept any suggestions for improving the material.

It uses communication with the server through the console client, but based on the examples provided, you can easily find the implementation of these examples in the client libraries in your favorite language.
')

Keys


Redis is a key-value data store. Key Facts:


Redis data types



There is a separate good article about Redis data types: “Data Structures Used in Redis” .


Basic operations


The console uses the redis-cli client program to communicate with Redis. Consider the basic operations.

Task 1, creation, selection, modification, deletion and basic information about objects.
Set and read values ​​with keys of the form test:1:* . Find out the type of value, check the existence of the element, extract all fields of the test:1 entry, delete the field of the test:1 entry.

 redis 127.0.0.1:6379> set test:1:string "my binary safe string" OK redis 127.0.0.1:6379> get test:1:string "my binary safe string" redis 127.0.0.1:6379> getset test:1:string "other value" "my binary safe string" redis 127.0.0.1:6379> type test:1:string string redis 127.0.0.1:6379> set test:1:vlaue "487" OK redis 127.0.0.1:6379> rename test:1:vlaue test:1:value OK redis 127.0.0.1:6379> exists test:1:vlaue (integer) 0 redis 127.0.0.1:6379> exists test:1:value (integer) 1 redis 127.0.0.1:6379> keys test:1:* 1) "test:1:string" 2) "test:1:value" redis 127.0.0.1:6379> del test:1:value (integer) 1 redis 127.0.0.1:6379> keys test:1:* 1) "test:1:string" 

Details on the commands related to this task can be found in the documentation in the Keys and Strings sections.

Task 2, the lifetime of the object.

 redis 127.0.0.1:6379> ttl test:1:string (integer) -1 redis 127.0.0.1:6379> expire test:1:string 6000 (integer) 1 redis 127.0.0.1:6379> ttl test:1:string (integer) 5997 

By default, the object is not limited to the lifetime, so TTL returns -1, after setting the value with the EXPIRE command, the TTL command already returns the remaining number of seconds.

Details on the commands related to this task can be found in the documentation in the Keys and Strings sections.

Task 3, pipelining, executing several commands in one request.

 PS D:\Programs\Redis\64bit> "set test:1:pvalue 'test'`r`nget test:1:pvalue" | .\redis-cli OK "test" 

Pipelining is useful for optimizing bulk inserts.

Task 4, transaction.
The following basic commands are used to implement transactions in Redis:

 redis 127.0.0.1:6379> multi OK redis 127.0.0.1:6379> set test:1:trValue "1" QUEUED redis 127.0.0.1:6379> incr test:1:trValue QUEUED redis 127.0.0.1:6379> decr test:1:trValue QUEUED redis 127.0.0.1:6379> exec 1) OK 2) (integer) 2 3) (integer) 1 

Details on the commands related to this task can be found in the documentation in the Transactions section.

Lines / numbers


Task 5, demonstrate basic string operations.

 redis 127.0.0.1:6379> set test:1:string "hello" OK redis 127.0.0.1:6379> append test:1:string " world!" (integer) 12 redis 127.0.0.1:6379> get test:1:string "hello world!" redis 127.0.0.1:6379> strlen test:1:string (integer) 12 redis 127.0.0.1:6379> getrange test:1:string 6 10 "world" redis 127.0.0.1:6379> setrange test:1:string 6 "habrahabr!" (integer) 16 redis 127.0.0.1:6379> get test:1:string "hello habrahabr!" 


Task 6, demonstrate operations on numbers.

 redis 127.0.0.1:6379> set test:1:int 1 OK redis 127.0.0.1:6379> incr test:1:int (integer) 2 redis 127.0.0.1:6379> decr test:1:int (integer) 1 redis 127.0.0.1:6379> incrby test:1:int 20 (integer) 21 redis 127.0.0.1:6379> decrby test:1:int 15 (integer) 6 

Details on the commands related to these tasks can be found in the documentation in the Strings section.

Lists


Task 7, create a list, demonstrate basic list operations.

 redis 127.0.0.1:6379> rpush test:1:messages "Hello, world!" (integer) 1 redis 127.0.0.1:6379> rpush test:1:messages "Hello, user!" (integer) 2 redis 127.0.0.1:6379> rpush test:1:messages "Wow!" (integer) 3 redis 127.0.0.1:6379> lrange test:1:messages 0 2 1) "Hello, world!" 2) "Hello, user!" 3) "Wow!" redis 127.0.0.1:6379> llen test:1:messages (integer) 3 redis 127.0.0.1:6379> lpop test:1:messages "Hello, world!" redis 127.0.0.1:6379> lpop test:1:messages "Hello, user!" redis 127.0.0.1:6379> lpop test:1:messages "Wow!" redis 127.0.0.1:6379> llen test:1:messages (integer) 0 

Details on the commands related to this task can be found in the documentation in the Lists section.

Sets, ordered sets


Task 8, create a set, demonstrate the basic operations on sets.

 redis 127.0.0.1:6379> sadd test:1:fruits "banana" (integer) 1 redis 127.0.0.1:6379> sadd test:1:fruits "apple" (integer) 1 redis 127.0.0.1:6379> sadd test:1:fruits "strawberry" (integer) 1 redis 127.0.0.1:6379> sadd test:1:yellowThings "banana" (integer) 1 redis 127.0.0.1:6379> sadd test:1:yellowThings "apple" (integer) 1 redis 127.0.0.1:6379> sadd test:1:redThings "strawberry" (integer) 1 redis 127.0.0.1:6379> scard test:1:fruits (integer) 3 redis 127.0.0.1:6379> sdiff test:1:fruits test:1:yellowThings 1) "strawberry" redis 127.0.0.1:6379> sdiffstore test:1:noYellowFruits test:1:fruits test:1:yellowThings (integer) 1 redis 127.0.0.1:6379> sinter test:1:yellowThings test:1:fruits 1) "banana" 2) "apple" redis 127.0.0.1:6379> sismember test:1:fruits "banana" (integer) 1 redis 127.0.0.1:6379> sismember test:1:fruits "tomato" (integer) 0 redis 127.0.0.1:6379> smembers test:1:noYellowFruits 1) "strawberry" redis 127.0.0.1:6379> smove test:1:yellowThings test:1:redThings "apple" (integer) 1 redis 127.0.0.1:6379> smembers test:1:redThings 1) "strawberry" 2) "apple" redis 127.0.0.1:6379> spop test:1:redThings "apple" redis 127.0.0.1:6379> srandmember test:1:fruits "apple" redis 127.0.0.1:6379> srem test:1:fruits "banana" (integer) 1 redis 127.0.0.1:6379> sunion test:1:yellowThings test:1:redThings 1) "strawberry" 2) "banana" redis 127.0.0.1:6379> sunionstore test:1:allThings test:1:yellowThings test:1:redThings (integer) 2 redis 127.0.0.1:6379> smembers test:1:allThings 1) "strawberry" 2) "banana" 

Details on the commands related to this task can be found in the documentation in the Sets section.

Task 9, create an ordered set and demonstrate the basic operations on it.
In an ordered set, the elements are compared by the additional parameter “score”.

 redis 127.0.0.1:6379> zadd hackers 1953 "Richard Stallman" (integer) 1 redis 127.0.0.1:6379> zadd hackers 1940 "Alan Kay" (integer) 1 redis 127.0.0.1:6379> zadd hackers 1965 "Yukihiro Matsumoto" (integer) 1 redis 127.0.0.1:6379> zadd hackers 1916 "Claude Shannon" (integer) 1 redis 127.0.0.1:6379> zadd hackers 1969 "Linus Torvalds" (integer) 1 redis 127.0.0.1:6379> zadd hackers 1912 "Alan Turing" (integer) 1 redis 127.0.0.1:6379> zrange hackers 0 -1 1) "Alan Turing" 2) "Claude Shannon" 3) "Alan Kay" 4) "Richard Stallman" 5) "Yukihiro Matsumoto" 6) "Linus Torvalds" redis 127.0.0.1:6379> zrevrange hackers 0 -1 1) "Linus Torvalds" 2) "Yukihiro Matsumoto" 3) "Richard Stallman" 4) "Alan Kay" 5) "Claude Shannon" 6) "Alan Turing" redis 127.0.0.1:6379> zrangebyscore hackers -inf 1950 1) "Alan Turing" 2) "Claude Shannon" 3) "Alan Kay" redis 127.0.0.1:6379> zremrangebyscore hackers 1940 1960 (integer) 2 redis 127.0.0.1:6379> zrange hackers 0 -1 1) "Alan Turing" 2) "Claude Shannon" 3) "Yukihiro Matsumoto" 4) "Linus Torvalds" 

Details on the commands related to this task can be found in the documentation in the Sorted sets section.

Hash tables


Task 10, Create a hash table and demonstrate basic hash operations.

 redis 127.0.0.1:6379> hset users:1 name "Andrew" (integer) 1 redis 127.0.0.1:6379> hset users:1 email "andrew@example.com" (integer) 1 redis 127.0.0.1:6379> hkeys users:1 1) "name" 2) "email" redis 127.0.0.1:6379> hvals users:1 1) "Andrew" 2) "andrew@example.com" redis 127.0.0.1:6379> hgetall users:1 1) "name" 2) "Andrew" 3) "email" 4) "andrew@example.com" redis 127.0.0.1:6379> hset users:1 test 1 (integer) 1 redis 127.0.0.1:6379> hincrby users:1 test 123 (integer) 124 redis 127.0.0.1:6379> hvals users:1 1) "Andrew" 2) "andrew@example.com" 3) "124" redis 127.0.0.1:6379> hdel users:1 test (integer) 1 redis 127.0.0.1:6379> hvals users:1 1) "Andrew" 2) "andrew@example.com" 

Details on the commands related to this task can be found in the documentation in the Hashes section.

Pub / Sub, Redis posts


Task 11, subscribe to messages on one client and send a message from another.
We present the windows of two clients; in the first window, a subscription to messages is made and the message sent from the second window is visible.

 redis 127.0.0.1:6379> SUBSCRIBE messages Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "messages" 3) (integer) 1 1) "message" 2) "messages" 3) "Hello world!" 

 redis 127.0.0.1:6379> PUBLISH messages "Hello world!" (integer) 1 


Task 12, subscribe to messages on one client and send a message from another. Subscription exercise using templates.
We present the windows of two clients; in the first window, a subscription to messages is made and the message sent from the second window is visible.

 redis 127.0.0.1:6379> PSUBSCRIBE news.* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "news.*" 3) (integer) 1 1) "pmessage" 2) "news.*" 3) "news.art" 4) "New picture!" 1) "pmessage" 2) "news.*" 3) "news.cinema" 4) "New movie!" 

 redis 127.0.0.1:6379> PUBLISH news.art "New picture!" (integer) 1 redis 127.0.0.1:6379> PUBLISH news.cinema "New movie!" (integer) 1 

Details on the commands related to these tasks can be found in the documentation in the Pub / Sub section.

Source: https://habr.com/ru/post/204354/


All Articles