Php/zend framework/zend session/memcached
提供: 初心者エンジニアの簡易メモ
bootstrap.php
protected function _initSession() { // applicationコンフィグロード $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV); // memcached接続設定 $frontendOptions = array( 'caching' => true, 'lifetime' => 864000, 'automatic_serialization' => true, ); // memcachedサーバ設定 $backendOptions = array( 'servers' =>array( array( 'host' => $config->memcache->host, 'port' => $config->memcache->port, ) ), ); Zend_Loader::loadClass('Custom_Session_SaveHandler_Memcached'); $cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions, $backendOptions); Zend_Session::setSaveHandler(new Custom_Session_SaveHandler_Memcached($cache)); // sessionコンフィグロード $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/session.ini', APPLICATION_ENV); // sessionオプション設定 Zend_Session::setOptions($config->toArray()); Zend_Session::start(); }
Custom/Session/SaveHandler/Memcached.php
<?php require_once 'Zend/Session/SaveHandler/Interface.php'; class Custom_Session_SaveHandler_Memcached implements Zend_Session_SaveHandler_Interface { private $_cache = ; public function __construct($cacheHandler) { $this->_cache = $cacheHandler; } public function open($save_path, $name) { return true; } public function close() { return true; } public function read($id) { if (!($data = $this->_cache->load($id))) { return ; } else { return $data; } } public function write($id, $data) { if (!preg_match("/^[a-zA-Z0-9_]+$/", $id)) return false; $this->_cache->save($data, $id, array(), $this->_cache->getOption('lifetime')); return true; } public function destroy($id) { $this->_cache->remove($id); return true; } public function gc($notusedformemcache) { return true; } }
configs/application.ini
[production] memcache.host = "localhost" memcache.port = "11211"
configs/session.ini
[production] ; セッション再設定から期限までの秒 cookie_lifetime = 8640000 ; セッション初回設定から期限までの秒 gc_maxlifetime = 86400000
参考URL
http://zend-framework-community.634137.n4.nabble.com/Conflict-between-Zend-Session-setSaveHandler-memcache-and-session-save-handler-memcache-td655782.html http://www.vexedmonkey.com/2008/08/01/zend-framework-sessions-in-memcache/
memcache単体の設定方法
php/memcachedメモ [ショートカット]