Professional writings

Redis: Hello World!

Redis: Hello World!

This writing is actually a draft note made for myself, so that I myself don’t forget what I had learned. But I think this note will be helpful for those who want to learn Redis. If you don’t know what Redis is, or haven’t installed Redis in your system yet, check it out here.

1. Hello World in Redis

1.1. PING

Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk. This command is often used to test if a connection is still alive, or to measure latency.

1.2. ECHO

Returns message.

1
2
3
4
5
6
7
[arafat@server ~]$ redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> PING "hello world"
"hello world"
127.0.0.1:6379> ECHO "Hello World!"
"Hello World!"

1.3. SET

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type.

1.4. GET

Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> SET foo 100
OK
127.0.0.1:6379> GET foo
"100"
127.0.0.1:6379> SET bar "Hello World!"
OK
127.0.0.1:6379> GET bar
"Hello World!"
127.0.0.1:6379> GET nonexisting
(nil)

1.5. INCR

Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

1.6. DECR

Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

1
2
3
4
5
6
7
8
127.0.0.1:6379> INCR foo
(integer) 101
127.0.0.1:6379> GET foo
"101"
127.0.0.1:6379> DECR foo
(integer) 100
127.0.0.1:6379> GET foo
"100"

1.7. EXISTS

Returns if key exists: - 1 if the key exists. - 0 if the key does not exist.

1.8. DEL

Removes the specified keys. A key is ignored if it does not exist.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
127.0.0.1:6379> EXISTS foo
(integer) 1
127.0.0.1:6379> EXISTS nosuchkey
(integer) 0
127.0.0.1:6379> EXISTS foo bar nosuchkey
(integer) 2
127.0.0.1:6379> DEL bar
(integer) 1
127.0.0.1:6379> EXISTS bar
(integer) 0
127.0.0.1:6379> GET bar
(nil)

1.9. FLUSHALL

Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.

1
2
3
4
5
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> GET foo
(nil)
127.0.0.1:6379>

1.10. EXPIRE

Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.

1.11. TTL

Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
127.0.0.1:6379> SET greeting "Hello World!"
OK
127.0.0.1:6379> EXPIRE greeting 50
(integer) 1
127.0.0.1:6379> TTL greeting
(integer) 47
127.0.0.1:6379> TTL greeting
(integer) 43
127.0.0.1:6379> TTL greeting
(integer) 37
127.0.0.1:6379> TTL greeting
(integer) 30
127.0.0.1:6379> TTL greeting
(integer) 30
127.0.0.1:6379> TTL greeting
(integer) 26
127.0.0.1:6379> TTL greeting
(integer) 19
127.0.0.1:6379> TTL greeting
(integer) 3
127.0.0.1:6379> TTL greeting
(integer) -2
127.0.0.1:6379> TTL greeting
(integer) -2

1.12. SETEX

Set key to hold the string value and set key to timeout after a given number of seconds. This command is equivalent to executing the following commands:

1
2
SET mykey value
EXPIRE mykey seconds

1.13. PERSIST

Remove the existing timeout on key, turning the key from volatile (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
127.0.0.1:6379> SETEX greeting 30 "Hello World!"
OK
127.0.0.1:6379> TTL greeting
(integer) 26
127.0.0.1:6379> TTL greeting
(integer) 21
127.0.0.1:6379> SETEX greeting 130 "Hello World!"
OK
127.0.0.1:6379> TTL greeting
(integer) 125
127.0.0.1:6379> PERSIST greeting
(integer) 1
127.0.0.1:6379> TTL greeting
(integer) -1
127.0.0.1:6379> GET greeting
"Hello World!"

1.14. MSET

Sets the given keys to their respective values. MSET replaces existing values with new values, just as regular SET. See MSETNX if you don’t want to overwrite existing values.

1.15. APPEND

If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.

1.16. RENAME

Renames key to newkey. It returns an error when key does not exist. If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
127.0.0.1:6379> MSET key1 "Hello" key2 "world"
OK
127.0.0.1:6379> GET key1
"Hello"
127.0.0.1:6379> GET key2
"world"
127.0.0.1:6379> APPEND key1 " world!"
(integer) 12
127.0.0.1:6379> GET key1
"Hello world!"
127.0.0.1:6379> RENAME key1 greeting
OK
127.0.0.1:6379> GET key1
(nil)
127.0.0.1:6379> GET greeting
"Hello world!"

2. Redis Datatypes

Official Documentation: Redis Datatypes

  • Strings

  • Lists

  • Sets

  • Sorted sets

  • Hashes

  • Bitmaps and HyperLogLogs

2.1. Lists

2.1.1. LPUSH

Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.

2.1.2. LRANGE

Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.

2.1.3. RPUSH

Insert all the specified values at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
127.0.0.1:6379> LPUSH people "arafat"
(integer) 1
127.0.0.1:6379> LPUSH people "Jen"
(integer) 2
127.0.0.1:6379> LPUSH people "Tom"
(integer) 3
127.0.0.1:6379> LRANGE people 0 -1
1) "Tom"
2) "Jen"
3) "arafat"
127.0.0.1:6379> LRANGE people 1 2
1) "Jen"
2) "arafat"
127.0.0.1:6379> RPUSH people "Harry"
(integer) 4
127.0.0.1:6379> LRANGE people 0 -1
1) "Tom"
2) "Jen"
3) "arafat"
4) "Harry"

2.1.4. LPOP

Removes and returns the first element of the list stored at key.

2.1.5. RPOP

Removes and returns the last element of the list stored at key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
127.0.0.1:6379> LRANGE people 0 -1
1) "Tom"
2) "Jen"
3) "arafat"
127.0.0.1:6379> LPOP people
"Tom"
127.0.0.1:6379> LRANGE people 0 -1
1) "Jen"
2) "arafat"
3) "Harry"
127.0.0.1:6379> RPOP people
"Harry"
127.0.0.1:6379> LRANGE people 0 -1
1) "Jen"
2) "arafat"

2.1.6. LINSERT

Inserts element in the list stored at key either before or after the reference value pivot.

When key does not exist, it is considered an empty list and no operation is performed.

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> LRANGE people 0 -1
1) "Jen"
2) "arafat"
127.0.0.1:6379> LINSERT people BEFORE "arafat" "Tom"
(integer) 3
127.0.0.1:6379> LRANGE people 0 -1
1) "Jen"
2) "Tom"
3) "arafat"

2.2. Sets

2.2.1. SADD

Add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.

2.2.2. SMEMBERS

Returns all the members of the set value stored at key.

2.2.3. SISMEMBER

Returns if member is a member of the set stored at key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
127.0.0.1:6379> SADD cars "Ford"
(integer) 1
127.0.0.1:6379> SADD cars "Honda"
(integer) 1
127.0.0.1:6379> SADD cars "BMW"
(integer) 1
127.0.0.1:6379> SMEMBERS cars
1) "Ford"
2) "BMW"
3) "Honda"
127.0.0.1:6379> SISMEMBER cars "Ford"
(integer) 1
127.0.0.1:6379> SISMEMBER cars "Chevy"
(integer) 0

2.2.4. SCARD

Returns the set cardinality (number of elements) of the set stored at key.

2.2.5. SMOVE

Move member from the set at source to the set at destination. This operation is atomic. In every given moment the element will appear to be a member of source or destination for other clients.

2.2.6. SREM

Remove the specified members from the set stored at key. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
127.0.0.1:6379> SMEMBERS cars
1) "Ford"
2) "BMW"
3) "Honda"
127.0.0.1:6379> SCARD cars
(integer) 3
127.0.0.1:6379> SMOVE cars mycars "Ford"
(integer) 1
127.0.0.1:6379> SMEMBERS cars
1) "BMW"
2) "Honda"
127.0.0.1:6379> SMEMBERS mycars
1) "Ford"
127.0.0.1:6379> SREM cars "BMW"
(integer) 1
127.0.0.1:6379> SMEMBERS cars
1) "Honda"
127.0.0.1:6379> FLUSHALL
OK

2.3. Sorted Sets

2.3.1. ZADD

Adds all the specified members with the specified scores to the sorted set stored at key. It is possible to specify multiple score / member pairs. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

2.3.2. ZRANK

Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.

2.3.3. ZRANGE

Returns the specified range of elements in the sorted set stored at key.

2.3.4. ZINCRBY

Increments the score of member in the sorted set stored at key by increment. If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0). If key does not exist, a new sorted set with the specified member as its sole member is created.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
127.0.0.1:6379> ZADD users 1981 "Arafat Hasan"
(integer) 1
127.0.0.1:6379> ZADD users 1975 "John Doe"
(integer) 1
127.0.0.1:6379> ZADD users 1990 "Mike Smith"
(integer) 1
127.0.0.1:6379> ZADD users 1990 "Kate Rogers"
(integer) 1
127.0.0.1:6379> ZRANK users "Mike Smith"
(integer) 3
127.0.0.1:6379> ZRANK users "John Doe"
(integer) 0
127.0.0.1:6379> ZRANK users "John Do"
(nil)
127.0.0.1:6379> ZRANK users "Arafat Hasan"
(integer) 1
127.0.0.1:6379> ZRANGE users 0 -1
1) "John Doe"
2) "Arafat Hasan"
3) "Kate Rogers"
4) "Mike Smith"
127.0.0.1:6379> ZINCRBY users 1 "John Doe"
"1976"
127.0.0.1:6379> ZINCRBY users 10 "John Doe"
"1986"
127.0.0.1:6379> FLUSHALL
OK

2.4. Hash

2.4.1. HSET

Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.

2.4.2. HGET

Returns the value associated with field in the hash stored at key.

2.4.3. HGETALL

Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
127.0.0.1:6379> HSET user:arafat name "Arafat Hasan"
(integer) 1
127.0.0.1:6379> HSET user:arafat email "arafat@example.com"
(integer) 1
127.0.0.1:6379> HGET user:arafat email
"arafat@example.com"
127.0.0.1:6379> HGETALL user:arafat
1) "name"
2) "Arafat Hasan"
3) "email"
4) "arafat@example.com"

2.4.4. HMSET

Sets the specified fields to their respective values in the hash stored at key.

2.4.5. HKEYS

Returns all field names in the hash stored at key.

2.4.6. HVALS

Returns all values in the hash stored at key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
127.0.0.1:6379> HMSET user:john name "John Doe" email "doe@example.com" age "25"
OK
127.0.0.1:6379> HGETALL user:john
1) "name"
2) "John Doe"
3) "email"
4) "doe@example.com"
5) "age"
6) "25"
127.0.0.1:6379> HKEYS user:john
1) "name"
2) "email"
3) "age"
127.0.0.1:6379> HVALS user:john
1) "John Doe"
2) "doe@example.com"
3) "25"

2.4.7. HINCRBY

Increments the number stored at field in the hash stored at key by increment.

2.4.8. HDEL

Removes the specified fields from the hash stored at key.

2.4.9. HLEN

Returns the number of fields contained in the hash stored at key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
127.0.0.1:6379> HINCRBY user:john age 1
(integer) 26
127.0.0.1:6379> HDEL user:john age
(integer) 1
127.0.0.1:6379> HGETALL user:john
1) "name"
2) "John Doe"
3) "email"
4) "doe@example.com"
127.0.0.1:6379> HLEN user:john
(integer) 2

3. Redis Persistence

Official Documentation: Redis Persistence

comments powered by Disqus