LoginSignup
1
1

More than 3 years have passed since last update.

Redis-server/Redis-cliをインストールして、Redisを操作する

Posted at

:construction: Redisを使うためにしたこと

用意するもの

Windows (Windows subsystem for linux)

$ cat /etc/os-release

NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

Redis-server を検索してみる

$ sudo apt show redis-server

Package: redis-server
Version: 2:3.0.6-1
Priority: optional
Section: universe/misc
Source: redis
Origin: Ubuntu
・・・

Redis-server インストール

$ sudo apt install redis-server

バージョンを確認してみる

$ redis-cli --version

Redis-server 起動

$ redis-server

Redis-server 停止

Crtl + C

Redis-cli 起動

$ redis-cli

Redis-cli cliから操作したサーバのデータを保存してRedis-server を終了

$ shutdown

Redis-cli 停止

$ exit


keys 一覧取得

$ keys *

対象のkey 取得

$ keys xxxxxxxxxxxxxxxx

型によって確認方法が異なるので型の確認を行う`

$ type xxxxxxxxxxxxxxxx

下記いずれかが返ってくる

string
 => `$ get xxxxxxxxxxxxxxxx

list
 => `$ lrange xxxxxxxxxxxxxxxx`0 -1 (先頭 から 最後から1番目までの値を返す)

set` <= topic-publisherが入れるときはこの型
 => `$ smembers xxxxxxxxxxxxxxxx

zset`<= topic-accumulatorが入れるときはこの型
 => `$ zrange xxxxxxxxxxxxxxxx`0 -1 (先頭 から 最後から1番目までの値を返す)
 => `$ zrange xxxxxxxxxxxxxxxx`0 2 (先頭 から 2番目までの値を返す)

hash
 => `$ hkeys xxxxxxxxxxxxxxxx
 => `$ hvals xxxxxxxxxxxxxxxx

対象のkey のみ削除

keys 一覧取得

$ keys *

対象のkey ごと削除

$ del xxxxxxxxxxxxxxxx

対象のkey を残しvalueのみ削除

$ srem xxxxxxxxxxxxxxxx

flushdb(選択しているDB indexのkeysをすべて削除する場合)

DB index 「0」 を選択、抽出

$ select 0
$ dbsize
(integer) 1

DB index 「1」 を選択、抽出

$ select 1
$ dbsize
(integer) 1

DB index 「0」 flushdb(選択しているDB indexのkeysを全て削除)

$ select 0
$ flushdb

DB index「0」のデータは削除され、件数が0になる

$ dbsize
(integer) 0

DB index「1」は1件のまま残る

$ select 1
$ dbsize
(integer) 1

感想


特になし

1
1
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
1
1