Return to Tech/php

Redis


参照元: https://github.com/phpredis/phpredis


phpからRedisを利用する方法

Class Redis
Description: Redisクライアントを作成する

$redis = new Redis();


Connection
接続方法は2種類存在
Connect to a server
$redis->connect('192.168.??.??', 6379);


Connect to a server(persistent)
$redis->pconnect('192.168.??.??', 6379);


Ping the server
echo $redis->ping();



Set and get
$redis->set("test", "testes");

echo $redis->get("test");



Session savepath
セッションの保存先をRedisへ

/etc/php.iniのsession.save_pathを以下のように調整することで実現可能
session.save_path = "tcp://ip-addr1:6379?weight=1"
session.save_path = "tcp://ip-addr1:6379?weight=1, tcp://ip-addr2:6379?weight=2"

当環境ではwebサーバとapp(redis)サーバは個別に稼働し
web -> appの構成でapp側のredis-serverにセッションが保持されます



Return to Tech/php