今回はphpでmemcachedに接続する方法を紹介します。
linuxにphp拡張をインストール
https://pecl.php.net/からmemcachedを検索すると、二つ結果が出る。
memcached : linuxのみ、laravelで使っている
memcache : linux,windows両方
memcache : linux,windows両方
yumインストール
yum search memcached | grep ^php
yum install -y php-pecl-memcached.x86_64
yum install -y php-pecl-memcached.x86_64
ソースコードでインストール
①yum install -y memcached.x86_64 memcached-devel.x86_64
②wget https://pecl.php.net/get/memcached-3.1.5.tgz
tar zxf memcached-3.1.5.tgz
cd memcached-3.1.5
③phpize
エラーの場合
yum search dev | grep ^php
yum install -y php-devel.x86_64
④which php-config
./configure –with-php-config=/usr/bin/php-config
make && make install
エラー対応:
・error: memcached support requires libmemcached.
yum -y install libmemcached-devel.x86_64
・error: memcached support requires ZLIB.
yum install zlib-devel -y
・error: no acceptable C compiler found in $PATH
yum install -y gcc
⑤cd /etc/php.d
vi 30-memcached.ini
extension = memcached.so
②wget https://pecl.php.net/get/memcached-3.1.5.tgz
tar zxf memcached-3.1.5.tgz
cd memcached-3.1.5
③phpize
エラーの場合
yum search dev | grep ^php
yum install -y php-devel.x86_64
④which php-config
./configure –with-php-config=/usr/bin/php-config
make && make install
エラー対応:
・error: memcached support requires libmemcached.
yum -y install libmemcached-devel.x86_64
・error: memcached support requires ZLIB.
yum install zlib-devel -y
・error: no acceptable C compiler found in $PATH
yum install -y gcc
⑤cd /etc/php.d
vi 30-memcached.ini
extension = memcached.so
windowsにphp拡張をインストール
①公式サイト(https://pecl.php.net/)からmemcacheのWindows版のdllファイルをダウンロードする。
②ダウンロードしたdllファイルをphpのextフォルダに入れる。
③php.iniファイルに「extension=php-memcache.dll」を追加する
④php-fpmを再起動する。
⑤phpinfo()で確認するmemcached クラスタ構成
php.iniファイルに「memcache.hash_strategy=consistent」を追加する
Session情報管理
session情報はデフォルトでファイルに格納します。
memcachedに格納したい場合、session_set_save_handler関数を利用する
参考内容は<PHPでsessionについて>
以下のコードを参考します。
<?php
class MemcachSession {
private static $handler = null;
private static $lifetime = null;
private static $time = null;
const NS = ‘session_’;
private static function init($handler, $lifetime) {
self::$handler = $handler;
self::$lifetime = $lifetime;
self::$time = time();
}
public static function start(Memcached $memcached) {
self::init($memcached, 1440);
session_set_save_handler(
array(__CLASS__, ‘open’),
array(__CLASS__, ‘close’),
array(__CLASS__, ‘read’),
array(__CLASS__, ‘write’),
array(__CLASS__, ‘destroy’),
array(__CLASS__, ‘gc’)
);
session_start();
}
public static function open($path, $name) {
return true;
}
public static function close() {
return true;
}
public static function read($session_id) {
$out = self::$handler->get(self::session_key($session_id));
if ($out === false || $out == null)
return ”;
return $out;
}
public static function write($session_id, $data) {
$method = $data ? ‘set’:’replace’;
return self::$handler->$method(self::session_key($session_id),$data,self::$lifetime);
}
public static function destroy($session_id) {
return self::$handler->delete(self::sessin_key($session_id));
}
public static function gc($lifetime) {
return true;
}
private static function session_key($session_id) {
$session_key = self::NS. $session_id;
return $session_key;
}
}
class MemcachSession {
private static $handler = null;
private static $lifetime = null;
private static $time = null;
const NS = ‘session_’;
private static function init($handler, $lifetime) {
self::$handler = $handler;
self::$lifetime = $lifetime;
self::$time = time();
}
public static function start(Memcached $memcached) {
self::init($memcached, 1440);
session_set_save_handler(
array(__CLASS__, ‘open’),
array(__CLASS__, ‘close’),
array(__CLASS__, ‘read’),
array(__CLASS__, ‘write’),
array(__CLASS__, ‘destroy’),
array(__CLASS__, ‘gc’)
);
session_start();
}
public static function open($path, $name) {
return true;
}
public static function close() {
return true;
}
public static function read($session_id) {
$out = self::$handler->get(self::session_key($session_id));
if ($out === false || $out == null)
return ”;
return $out;
}
public static function write($session_id, $data) {
$method = $data ? ‘set’:’replace’;
return self::$handler->$method(self::session_key($session_id),$data,self::$lifetime);
}
public static function destroy($session_id) {
return self::$handler->delete(self::sessin_key($session_id));
}
public static function gc($lifetime) {
return true;
}
private static function session_key($session_id) {
$session_key = self::NS. $session_id;
return $session_key;
}
}