LoginSignup
23
25

More than 5 years have passed since last update.

phpredisの使い方まとめ

Last updated at Posted at 2016-05-02

phpreidsの使い方の日本語の情報が少なかったのでまとめます。

前準備

  • Redisのインストール
  • phpredisのインストール

環境によってインストールの方法が異なってくるのでここでは省きます。

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

echo $redis->ping();

+PONG と出力されればOK。

Redisで扱えるデータ型

  • String
  • List
  • Set
  • Sorted Set
  • Hash

String

文字列や数値など、Keyに対して1つに定まる値。

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// hogeというkeyにhugaという値をセット
$redis->set('hoge', 'huga');

// 値を取得する
$value = $redis->get('hoge');

// 表示
echo $value; // huga

List

順番を持った値の集合

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// lPushは先頭、rPushは末尾に値をpush
$redis->rPush('hoge', 'a');
$redis->rPush('hoge', 'b');
$redis->lPush('hoge', 'c');
$redis->lPush('hoge', 'd');

// 値をすべて取得する -1はすべて
$value = $redis->lRange('hoge', 0, -1);

// 表示
var_dump($value);

array(4) {
  [0] =>
  string(1) "d"
  [1] =>
  string(1) "c"
  [2] =>
  string(1) "a"
  [3] =>
  string(1) "b"
}

Set

順不同の値の集合。重複を許さない

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// 値を追加
$redis->sAdd('hoge1' , '1');
$redis->sAdd('hoge1' , '2');
$redis->sAdd('hoge2' , '2');
$redis->sAdd('hoge2' , '3');
$redis->sAdd('hoge2' , '4');

// 和、積、差を取得する
$union = $redis->sUnion('hoge1', 'hoge2');
$inter = $redis->sInter('hoge1', 'hoge2');
$diff  = $redis->sDiff('hoge1', 'hoge2');

// 表示
var_dump($union);
var_dump($inter);
var_dump($diff);

/*
array(4) {
  [0] =>
  string(1) "1"
  [1] =>
  string(1) "2"
  [2] =>
  string(1) "3"
  [3] =>
  string(1) "4"
}
array(1) {
  [0] =>
  string(1) "2"
}
array(1) {
  [0] =>
  string(1) "1"
}
*/

Sorted Set

セット型と同様の特徴に加え、個々の値がスコアを持つ。

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// スコアと値を追加
$redis->zAdd('hoge' , 10, 'a');
$redis->zAdd('hoge' , 20, 'b');
$redis->zAdd('hoge' , 30, 'c');
$redis->zAdd('hoge' , 40, 'd');
$redis->zAdd('hoge' , 50, 'e');
$redis->zAdd('hoge' , 60, 'f');

// ソート済のスコアを取得
$value = $redis->zRangeByScore('hoge', 0, 60);

// 表示
var_dump($value);

/*
array(6) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "b"
  [2] =>
  string(1) "c"
  [3] =>
  string(1) "d"
  [4] =>
  string(1) "e"
  [5] =>
  string(1) "f"
}
*/

Hash

Fieldとvalueを1セットとする値の集合

test.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// 値をセット
$redis->hSet('hoge', 'hoge1', 1);
$redis->hSet('hoge', 'hoge2', 2);
$redis->hSet('hoge', 'hoge3', 3);

// 値を取得
$value1 = $redis->hGet('hoge', 'hoge1');
$value2 = $redis->hGet('hoge', 'hoge2');
$all = $redis->hGetAll('hoge');

// 表示
var_dump($value1);
var_dump($value2);
var_dump($all);

/*
string(1) "1"
string(1) "2"
array(3) {
  'hoge1' =>
  string(1) "1"
  'hoge2' =>
  string(1) "2"
  'hoge3' =>
  string(1) "3"
}
*/
23
25
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
23
25