goodbyegangsterのブログ

備忘録的な

Redisのデータ構造

Redisで用意されているデータ構造を確認します。参考サイトたち。

環境

  • Redis server v=5.0.7

Strings

The Redis String type is the simplest type of value you can associate with a Redis key.

Redis keys are strings, when we use the string type as a value too, we are mapping a string to another string.

つまり mapping a string to another string ということで、僕らがKey-Value StoreのNoSQLと聞いて連想するそれですね。

127.0.0.1:6379> set key1 value ex 10
OK
127.0.0.1:6379> get key1
"value"
127.0.0.1:6379> ttl key1
(integer) 5
127.0.0.1:6379> get key1
(nil)

Lists

Redis Lists are simply lists of strings, sorted by insertion order.

Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time. The speed of adding a new element with the LPUSH command to the head of a list with ten elements is the same as adding an element to the head of list with 10 million elements.

いわゆる連結リスト。ユースケースとしては、

Remember the latest updates posted by users into a social network.

Communication between processes, using a consumer-producer pattern where the producer pushes items into a list, and a consumer (usually a worker) consumes those items and executed actions.

下記の説明が分かりやすかったです。

The popular Twitter social network takes the latest tweets posted by users into Redis lists.

ユーザー毎にKeyを用意して、最新アクションをListsに格納するイメージでしょうか。

127.0.0.1:6379> rpush list-key value1       # Push onto end
(integer) 1
127.0.0.1:6379> rpush list-key value2       # Push onto end
(integer) 2
127.0.0.1:6379> lrange list-key 0 -1       # Access range
1) "value1"
2) "value2"
127.0.0.1:6379> lindex list-key 0       # Access by index
"value1"
127.0.0.1:6379> lpush list-key value0       # Push onto start
(integer) 3
127.0.0.1:6379> lrange list-key 0 -1
1) "value0"
2) "value1"
3) "value2"
127.0.0.1:6379> rpop list-key       # Pop from end
"value2"
127.0.0.1:6379> lrange list-key 0 -1
1) "value0"
2) "value1"

Sets

Redis Sets are an unordered collection of Strings.

Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element.

いわゆる集合。keyに対して、一意なvalueの集まりを管理してくれます。ユースケースとしては、下記が紹介されていました。

Want to know all the unique IP addresses visiting a given blog post? Simply use SADD every time you process a page view. You are sure repeated IPs will not be inserted.

127.0.0.1:6379> sadd set-key AAA BBB CCC       # Add item
(integer) 3
127.0.0.1:6379> smembers set-key       # Get all
1) "BBB"
2) "CCC"
3) "AAA"
127.0.0.1:6379> sadd set-key CCC DDD       # Add item
(integer) 1
127.0.0.1:6379> smembers set-key       # Get all
1) "BBB"
2) "CCC"
3) "AAA"
4) "DDD"

Hash

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects

Key-ValueValueに、連想配列を格納できるもの。一組のKey-ValueValueの中に、さらに複数組のKey-Value(sub-key, sub-valueとも言える)をできる、というイメージでしょうか。

127.0.0.1:6379> hset hash-key sub-key1 sub-value1       # Set item
(integer) 1
127.0.0.1:6379> hget hash-key sub-key1       # Get item
"sub-value1"
127.0.0.1:6379> hset hash-key sub-key2 sub-value2
(integer) 1
127.0.0.1:6379> hkeys hash-key       # Return all keys
1) "sub-key1"
2) "sub-key2"

Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

Hashでは、登録できる連想配列Valueに自由な値を指定できましたが、Sorted Setsの場合、数値を登録する事となります。登録された数値により、サブKeyはソートされて管理されます。

ユースケースとして、ゲーム内のユーザーランキング情報などでしょうか。

Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK.

127.0.0.1:6379> zadd zset-key 100 yukari
(integer) 1
127.0.0.1:6379> zadd zset-key 50 maki
(integer) 1
127.0.0.1:6379> zrange zset-key 0 100
1) "maki"
2) "yukari"
127.0.0.1:6379> zrank zset-key yukari
(integer) 1