LoginSignup
0
1

More than 5 years have passed since last update.

RedisのCentOS7へのインストールと操作

Posted at

NoSQLの中で人気のあるRedisをこちらを参考に触ってみたのでまとめます。
NoSQL.JPG
引用 : 知らないなんて言えないNoSQLまとめ ― @IT

Redisとは

  • インメモリDB、オプションとして永続性をサポート
  • キー・バリュー型:Key/Valueを単位としてデータを格納
  • 5種類のデータ・タイプ
data type 扱う要素  
文字列 key に valueを登録
ハッシュ key に fieldとvalueをペアで登録
セット key に memberを登録
ソート済みセット key に memberとscoreをペアで登録
リスト key に valueを登録

Redisのインストール、起動

yum -y install epel-release   #エンタープライズ用パッケージが必要
yum -y install redis

redis-server --version
redis-server &

対話式環境の起動

※参考:redis-cliのオプション

redis-cli
127.0.0.1:6379> set -h redis   #hostname設定
OK
127.0.0.1:6379> get -h
"redis"

SET / SETNX / GET

setコマンドで値を登録しgetコマンドで値を取得
setnxコマンド(SET if Not eXists)は既存のキーに値を登録(update)不可

127.0.0.1:6379> set mystring "abc"
OK
127.0.0.1:6379> get mystring
"abc"
127.0.0.1:6379> setnx mystring "cde"
(integer) 0
127.0.0.1:6379> get mystring
"abc"

SETEX / PSETEX

キーに有効期限を設定して値を設定(SETEX:秒、PSETEX:ミリ秒)

127.0.0.1:6379> setex mystring 10 "abc"
OK
127.0.0.1:6379> ttl mystring   #有効期限を確認
(integer) 6
127.0.0.1:6379> ttl mystring   #有効期限切れでキーが存在しない:-2
(integer) -2

GETSET

キーの値の取得と設定を同時に実施

127.0.0.1:6379> getset mystring "abc"
(nil)   #新規に登録する場合はnilが返る

127.0.0.1:6379> getset mystring "def"
"abc"
127.0.0.1:6379> get mystring
"def"

SELECT: DBの切替え

127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> set Key 'Value'
OK
127.0.0.1:6379> get Key
"Value"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get Key
(nil)
127.0.0.1:6379[1]>

KEYS: 登録されているKeyの検索

127.0.0.1:6379> keys *
1) "Key"
2) "-h"
3) "mystring"
4) "hello"

リスト操作(RPUSH / LPUSH / LLEN / LRANGE / LPOP / RPOP)

PUSH/LPUSHは、リストの左右にオブジェクトを追加

127.0.0.1:6379> rpush abc 'cde'
(integer) 1
127.0.0.1:6379> rpush abc 'fgh'
(integer) 2
127.0.0.1:6379> rpush abc 'ijk'
(integer) 3
127.0.0.1:6379> lpush abc '000'
(integer) 4
127.0.0.1:6379> llen abc   #llen: 長さを取得
(integer) 4

LPOP/RPOPは、リストの左右からオブジェクトを削除し、返す

127.0.0.1:6379> lpop abc
"000"

LRANGEは、範囲を指定してリストを取得

127.0.0.1:6379> lrange abc 0 2
1) "cde"
2) "fgh"
3) "ijk"
0
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
0
1