0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめてのredis

Last updated at Posted at 2020-05-01

redisのjob queueを使いたかったんだけど、何もわからず立ち尽くしてしまったのでGW的に復習しようかと。

ゆくゆくはここを読む予定。
https://redislabs.com/ebook/part-2-core-concepts/chapter-6-application-components-in-redis/6-4-task-queues/6-4-1-first-in-first-out-queues/

install redis on mac

いったんlocalでやってみよ

brew install redis

redis-server /usr/local/etc/redis.conf redis-server /usr/local/etc/redis.conf

2行で起動できる便利さ。待ち時間ゼロ。

Use local redis

違うterminalで実行

redis-cli

これだけ。redis-cliは brew install redis で一緒に入ります。

データ種類

あんま真面目に使ったことなかったのでどこまでdict型が入るのか確認ん。この記事によると5種類。
https://qiita.com/keinko/items/60c844bcf329bd3f4af8

  1. String型: key-value | "name" = "john"
  2. List型 : [1,1,2,2,3]
  3. Set型 : Sets | [1,2,3]
  4. Sorted set型 : いったんスルー
  5. Hash型 : Dictionary | dog = {"name": "Hachi", "age": 10}

STRING

127.0.0.1:6379>  set a 1
OK
127.0.0.1:6379> get a
"1"

LIST

重複OK。POPで取り出せる。queueはこれでもいいっちゃいい。
RPUSHできっと右側に追加する。

127.0.0.1:6379> LPUSH mylist a
(integer) 1
127.0.0.1:6379> LPUSH mylist a    <------重複
(integer) 2
127.0.0.1:6379> LPUSH mylist b
(integer) 3
127.0.0.1:6379> LPUSH mylist c
(integer) 4
127.0.0.1:6379> LPOP mylist
"c"
127.0.0.1:6379> LPOP mylist
"b"
127.0.0.1:6379> LPOP mylist
"a"
127.0.0.1:6379> LPOP mylist        <------重複
"a"
127.0.0.1:6379> LPOP mylist
(nil)

SET

SETsというからには、重複が入らない

127.0.0.1:6379> SADD myset "a"
(integer) 1
127.0.0.1:6379> SADD myset "b"
(integer) 1
127.0.0.1:6379> SADD myset "c"
(integer) 1
127.0.0.1:6379> SADD myset "c"     <------重複
(integer) 0
127.0.0.1:6379> SPOP myset
"a"
127.0.0.1:6379> SPOP myset
"c"
127.0.0.1:6379> SPOP myset
"b"
127.0.0.1:6379> SPOP myset     <------重複なし
(nil)
127.0.0.1:6379>

HASH = HMSET

やっぱmongoか?感が少し漂う。

$ redis-cli
127.0.0.1:6379> HMSET john age 25 gender "male"
OK
127.0.0.1:6379> HMSET Kate age 33 gender "female"
OK

127.0.0.1:6379> HMGET john age
1) "25"
127.0.0.1:6379> HMGET john gender
1) "male"
127.0.0.1:6379> HMGET kate age         <----- Case sensitive
1) (nil)
127.0.0.1:6379> HMGET Kate age
1) "33"
127.0.0.1:6379> HMGET Kate gender
1) "female"

HSETもあるらしい
https://redis.io/commands/hset

んーどうなんだろか。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?