You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In these general Redis tutorials, you've been introduced to Redis string, list, set, sorted set, and hash data structure types.
1
+
In these Redis basic commands tutorials, you've been introduced to the Redis string, list, set, sorted set, and hash data structure types.
2
2
There are many more Redis data structure types that you can use for a variety of use cases. See [the Redis documentation](https://redis.io/docs/data-types) for the entire list of supported data types.
Lists, sets, and sorted sets are great for many use cases, but the [hash](https://redis.io/docs/data-types/hashes) data type is on a higher level. Redis hashes are maps (sequences of key-value pairs) and are used to represent objects in Redis. Consider the following example that shows how to create a hash key using the [HSET](https://redis.io/commands/hset) command.
1
+
Lists, sets, and sorted sets are great for many use cases, but the hash data type is on a higher level. Redis hashes are maps (sequences of field-value pairs) and are used to represent objects in Redis. Consider the following example that shows how to create a hash key using the `HSET` command.
2
2
3
3
```redis Create a hash
4
4
HSET bike:1 model Deimos brand Ergonom type "Enduro bikes" price 4972
5
5
```
6
6
7
7
`HSET` returns the number of added key/value pairs.
8
8
9
-
To retrieve the stored data, use the [HGETALL](https://redis.io/commands/hgetall) command.
9
+
To retrieve the stored data, use the `HGETALL` command.
10
10
11
11
```redis HGETALL usage
12
-
HGETALL bike:1
12
+
HGETALL bike:1 // returns all the field-value pairs associated with the key
13
13
```
14
14
15
-
If you only want the values of a subset of the fields, use the [HGET](https://redis.io/commands/hget) command.
15
+
If you only want the values of a subset of the fields, use the `HGET` command.
16
16
17
17
```redis HGET usage
18
18
HGET bike:1 price
19
19
```
20
20
21
-
Numerical values in hash keys can be incremented in the same way as simple string keys using the [HINCRBY](https://redis.io/commands/hincrby) command.
21
+
You can update fields in a hash using `HSET` by specifying a subset of its field-value pairs.
22
+
23
+
```redis Update an existing hash
24
+
HSET bike:1 model "Kraken" price 3000
25
+
```
26
+
27
+
Use the `HDEL` command to delete one or more fields from a hash. Once all fields are removed, the hash key itself will be deleted.
28
+
29
+
```redis Delete hash fields and keys
30
+
HDEL bike:1 model
31
+
HGETALL bike:1
32
+
HDEL bike:1 brand type price
33
+
HGETALL bike:1
34
+
EXISTS bike:1
35
+
```
36
+
37
+
Numerical values in hash keys can be incremented in the same way as simple string keys using the `HINCRBY` command.
22
38
23
39
```redis Hash INCRBY usage
24
40
HINCRBY bike:1 price 100
25
41
HINCRBY bike:1 price -100
26
42
```
27
43
28
-
See [here](https://redis.io/commands/?group=hash) for the entire set of Redis hash-related commands.
44
+
See [here](https://redis.io/docs/data-types/hashes) for the hash type reference page, and [here](https://redis.io/commands/?group=hash) for the entire set of Redis hash commands.
29
45
30
46
**Note**:
31
47
> If you're more familiar with JSON and want to use it with Redis, there is a [JSON data type](https://redis.io/docs/data-types/json) that is provided as part of Redis Stack. There's also a tutorial titled "Working with JSON" that is bundled with RedisInsight.
Copy file name to clipboardExpand all lines: src/redis/working_with_lists.md
+14-8Lines changed: 14 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,17 @@
1
-
A Redis [list](https://redis.io/docs/data-types/lists) is a data type that contains a series of ordered values. You interact with lists using commands such as:
1
+
A Redis list is a data type that contains a series of ordered string values. Use cases include
2
2
3
-
-[RPUSH](https://redis.io/commands/rpush) - adds items to the end of a list.
4
-
-[LPUSH](https://redis.io/commands/lpush) - adds items to the beginning of a list.
5
-
-[LLEN](https://redis.io/commands/llen) - retrieves the length of a list.
6
-
-[LRANGE](https://redis.io/commands/lrange) - retrieves list items from a specified range.
7
-
-[LPOP](https://redis.io/commands/lpop) - removes and returns an item from the beginning of a list.
8
-
-[RPOP](https://redis.io/commands/rpop) - removes and returns an item from the end of a list.
3
+
- implementing stacks (LIFO data structures)
4
+
- implementing queues (FIFO data structures)
5
+
- implementing producer-consumer patterns
6
+
7
+
You interact with lists using commands such as:
8
+
9
+
- RPUSH - adds items to the end of a list.
10
+
- LPUSH - adds items to the beginning of a list.
11
+
- LLEN - retrieves the length of a list.
12
+
- LRANGE - retrieves list items from a specified range.
13
+
- LPOP - removes and returns an item from the beginning of a list.
14
+
- RPOP - removes and returns an item from the end of a list.
9
15
10
16
You can begin working with a list without first creating its key, simply by adding values to the key. This works as long as the key doesn't already exist as a different type.
11
17
@@ -72,4 +78,4 @@ KEYS bike:colors
72
78
**Note**:
73
79
> `LRANGE` will return an empty (null) list if a key no longer exists.
74
80
75
-
See [here](https://redis.io/commands/?group=list) for the entire set of Redis list-related commands.
81
+
See [here](https://redis.io/docs/data-types/list) for the list type reference page, and [here](https://redis.io/commands/?group=list) for the entire set of Redis list commands.
Copy file name to clipboardExpand all lines: src/redis/working_with_sets.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
-
Redis [sets](https://redis.io/docs/data-types/sets/) are similar to lists, except they are unordered, and each element must be unique. You can use sets to:
1
+
Redis sets are similar to lists, except they are unordered, and each element must be unique. You can use sets to:
2
2
3
3
- Track unique items (e.g., track all unique IP addresses accessing a given blog post).
4
4
- Represent relations (e.g., the set of all users with a specified role).
5
5
- Perform common set operations such as intersection, union, and difference.
6
6
7
7
Here are some important set commands:
8
8
9
-
-[SADD](https://redis.io/commands/sadd)
10
-
-[SREM](https://redis.io/commands/srem)
11
-
-[SISMEMBER](https://redis.io/commands/sismember)
12
-
-[SMEMBERS](https://redis.io/commands/smembers)
13
-
-[SUNION](https://redis.io/commands/sunion)
9
+
-`SADD`
10
+
-`SREM`
11
+
-`SISMEMBER`
12
+
-`SMEMBERS`
13
+
-`SUNION`
14
14
15
15
Use `SADD` to create and update a set. Each `SADD` command will return the number of added members. If you try to add a member that is already in the set, `0` is returned.
16
16
@@ -22,7 +22,7 @@ SADD bike:1:addons "bell"
22
22
23
23
Notice that the `SADD` command is variadic.
24
24
25
-
`SREM` is used to remove members of a list. It returns `1` if the member is in the set, or `0` if it is not.
25
+
`SREM` is used to remove members of a set. It returns `1` if the member is in the set, or `0` if it is not.
Each command takes a key and, optionally, a count as arguments. `SPOP` removes and returns a random member, whereas `SRANDMEMBER` just returns the randomly selected members without removing them.
58
58
@@ -63,4 +63,4 @@ SPOP bike:1:addons
63
63
SMEMBERS bike:1:addons
64
64
```
65
65
66
-
See [here](https://redis.io/commands/?group=set) for the entire list of Redis set-related commands.
66
+
See [here](https://redis.io/docs/data-types/sets) for the set type reference page, and [here](https://redis.io/commands/?group=set) for the entire list of Redis set commands.
Copy file name to clipboardExpand all lines: src/redis/working_with_sorted_sets.md
+27-4Lines changed: 27 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,7 @@
1
-
Redis sets are unsorted, which limits their usefulness. [Sorted sets](https://redis.io/docs/data-types/sorted-sets) are similar to sets, except each unique member has an associated score, which provides the mechanism for sorting.
1
+
Redis sets are unsorted, which limits their usefulness. Sorted sets are similar to sets, except each unique member has an associated score, which provides the mechanism for sorting. Use cases include:
2
+
3
+
- rate limiters
4
+
- game leaderboards
2
5
3
6
The following example creates a set of bike brands named for famous computer programmers, using their birth year as the score element to sort the set.
Now you can use the [ZRANGE](https://redis.io/commands/zrange) command to retrieve members in the order of birth year.
19
+
Now you can use the `ZRANGE` command to retrieve members in the order of birth year using member ranks as the arguments. As with lists, you can use `-1`, `-2`, etc., to represent the last, second to last, etc., members.
17
20
18
21
```redis ZRANGE usage
19
22
ZRANGE bike:brands 2 4
20
23
```
21
24
22
-
A popular use case for sorted sets is leaderboards for games.
25
+
To delete members from a sorted set, use the `ZREM` command.
26
+
27
+
```redis Remove the Alan Turing model and score
28
+
ZREM bike:brands "Alan Turing"
29
+
```
30
+
31
+
Once all the members of a sorted set have been removed, the set's key will also be removed.
32
+
33
+
To list all the members in score order, use the `ZRANGEBYSCORE`. If you pass the `WITHSCORES` argument, each member's score will also be listed.
34
+
35
+
```redis List members ordered by score
36
+
ZRANGEBYSCORE bike:brands -inf +inf WITHSCORES
37
+
```
38
+
39
+
Note the use of `-inf` and `+inf` in the previous example. This is useful when you don't readily know the score range or particular values. See the manual page for `ZRANGEBYSCORE` for more options.
40
+
41
+
You can get the rank of any sorted set member using the `ZRANK` command. Note: the member with the lowest score has rank `0`.
42
+
43
+
```redis Get a bike brand's rank
44
+
ZRANK bike:brands "Claude Shannon" WITHSCORE
45
+
```
23
46
24
-
See [here](https://redis.io/commands/?group=sorted-set) for the entire list of Redis sorted set commands.
47
+
See [here](https://redis.io/docs/data-types/sorted-sets) for the sorted set type reference page, and [here](https://redis.io/commands/?group=sorted-set) for the entire list of Redis sorted set commands.
Redis [strings](https://redis.io/docs/data-types/strings) are used to store strings, numbers, and other kinds of information. They are the simplest of the Redis data types.
1
+
Redis strings are used to store strings, numbers, images, serialized objects, and other kinds of information. They are the simplest of the Redis data types.
2
2
3
-
As a first example, use the [SET](https://redis.io/commands/set/) command to store the value "Deimos", a type of bike, at key "bike:1".
3
+
As a first example, use the `SET` command to store the value "Deimos", a type of bike, at key "bike:1".
4
4
5
5
```redis Create a new key
6
6
SET bike:1 "Deimos"
7
7
```
8
8
9
-
Redis will store the `bike:1` data permanently. You can retrieve the data using the [GET](https://redis.io/commands/get/) command.
9
+
You can retrieve the data using the `GET` command.
10
10
11
11
```redis Retrieve the data
12
12
GET bike:1
13
13
```
14
14
15
-
To see if a key exists, use the [EXISTS](https://redis.io/commands/exists) command.
15
+
To see if a key exists, use the `EXISTS` command.
16
16
17
17
```redis Do these keys exist?
18
18
EXISTS bike:1
@@ -21,29 +21,40 @@ EXISTS bike:2
21
21
22
22
The `EXISTS` command returns `1` when a key exists and `0` when it does not exist. The `bike:1` key exists, so Redis returns a value of `1`. The `bike:2` key does not exist, so Redis returns `0`.
23
23
24
-
Use the [DEL](https://redis.io/commands/del) to delete a key and its data.
24
+
Use the `DEL` to delete a key and its data.
25
+
26
+
```redis Delete bike:1
27
+
DEL bike:1
28
+
EXISTS bike:1
29
+
```
25
30
26
31
## Strings as counters
27
32
28
33
If you store integer data in a string key, you can use these commands to increment and decrement its value:
29
34
30
-
-[INCR](https://redis.io/commands/incr) - increment a number stored in a key by `1`.
31
-
-[INCRBY](https://redis.io/commands/incrby) - increment a number stored in a key by a specific positive or negative amount.
32
-
-[DECR](https://redis.io/commands/decr) - decrement a number stored in a key by `2`.
33
-
-[DECRBY](https://redis.io/commands/decrby) - decrement a number stored in a key by a specific positive or negative amount.
35
+
-`INCR` - increment a number stored in a key by `1`.
36
+
-`INCRBY` - increment a number stored in a key by a specific positive or negative amount.
37
+
-`DECR` - decrement a number stored in a key by `2`.
38
+
-`DECRBY` - decrement a number stored in a key by a specific positive or negative amount.
34
39
35
-
```redis INCR and DEL usage
40
+
```redis INCR usage
36
41
SET bikes:total_number 10
37
42
INCR bikes:total_number
38
43
INCR bikes:total_number
44
+
```
45
+
46
+
```redis Use INCR to set a key to 1
39
47
DEL bikes:total_number
40
48
INCR bikes:total_number
41
49
```
42
50
43
51
Note how the `INCR bikes:total_number` command that follows the `DEL` command re-creates the `bikes:total_number` key and sets its value to `1`.
44
52
45
-
```redis INCRBY, DECR, and DECRBY usage
53
+
```redis INCRBY usage
46
54
INCRBY bikes:total_number 100
55
+
```
56
+
57
+
```redis DECR and DECRBY usage
47
58
DECR bikes:total_number
48
59
DECRBY bikes:total_number 10
49
60
```
@@ -54,11 +65,11 @@ All the Redis operations implemented by single commands are atomic, including th
54
65
55
66
## Key expiration
56
67
57
-
When keys are created without constraints, they live for the full lifespan of the Redis server to which they are attached. However, if you want a key to live for only a specific amount of time, you can use the [EXPIRE](https://redis.io/commands/expire) command to alter its lifespan. To find out how much time is left before a key expires, use the [TTL](https://redis.io/commands/ttl) command.
68
+
When keys are created without constraints, they live for the full lifespan of the Redis server to which they are attached. However, if you want a key to live for only a specific amount of time, you can use the `EXPIRE` command to alter its lifespan. To find out how much time is left before a key expires, use the `TTL` command.
58
69
59
70
```redis EXPIRE usage
60
71
SET bike:1:lock_status "LOCKED"
61
-
EXPIRE bike:1:lock_status 120
72
+
EXPIRE bike:1:lock_status 20
62
73
```
63
74
64
75
After a few seconds, check the time-to-live status by running the `TTL` command.
@@ -79,22 +90,20 @@ SET bike:2:lock_status "LOCKED"
79
90
TTL bike:2:lock_status
80
91
```
81
92
82
-
`EXPIRE` and `TTL` operate in seconds, but there are analogous commands that operate in milliseconds. They are [PEXPIRE](https://redis.io/commands/pexpire) and [PTTL](https://redis.io/commands/pttl).
93
+
`EXPIRE` and `TTL` operate in seconds, but there are analogous commands that operate in milliseconds. They are `PEXPIRE` and `PTTL`.
83
94
84
95
The `SET` command can take additional arguments, one of which allows you to set the value of a key and its time to live value directly in a single, atomic operation. The `EX` and `PX` arguments allow you to specify the TTL value as either seconds or milliseconds, respectively.
85
96
86
97
```redis SET with time to live
87
98
SET bike:1:lock_status "LOCKED" PX 1
88
99
```
89
100
90
-
It's possible to cancel a key's timeout using the [PERSIST](https://redis.io/commands/persist) command.
101
+
It's possible to cancel a key's timeout using the `PERSIST` command.
91
102
92
103
```redis PERSIST usage
93
104
SET bike:1:lock_status "LOCKED" EX 120
94
105
PERSIST bike:1:lock_status
95
106
TTL bike:1:lock_status
96
107
```
97
108
98
-
See the [SET](https://redis.io/commands/set) command manual page for more time-related and other arguments.
99
-
100
-
See [here](https://redis.io/commands/?group=string) for the entire set of Redis string-related commands.
109
+
See [here](https://redis.io/docs/data-types/strings) for the string type reference page, and [here](https://redis.io/commands/?group=string) for the entire set of Redis string commands.
0 commit comments