kohana make session use memcache

Kohana中已经实现Session驱动cookie、native、database,我们再实现基于memcached的驱动:
配置文件session.php:

    <?php defined('SYSPATH') or die('No direct script access.');

    return array(
        'cache' => array(
            'name' => 'cookie_name',
            'lifetime' => 43200,
            'group' => 'memcache',
        ),
        'native' => array(
            'name' => 'session_name',
            'lifetime' => 43200,
        ),
        'cookie' => array(
            'name' => 'cookie_name',
            'encrypted' => TRUE,
            'lifetime' => 43200,
        ),
        'database' => array(
            'name' => 'cookie_name',
            'encrypted' => TRUE,
            'lifetime' => 43200,
            'group' => 'default',
            'table' => 'sessions',
            'columns' => array(
                'session_id'  => 'session_id',
                'last_active' => 'last_active',
                'contents'    => 'contents'
            ),
            'gc' => 500,
        ),
    );

新增文件
\classes\session\cache.php:

    <?php defined('SYSPATH') or die('No direct script access.');

    class Session_Cache extends Kohana_Session_Cache {}

    新增文件
    \classes\kohana\session\cache.php:
    <?php defined('SYSPATH') or die('No direct script access.');

    class Kohana_Session_Cache extends Session {

        // _memcached instance
        protected $_memcached;
        // The current session id
        protected $_session_id;
        // The old session id
        protected $_update_id;

        public function __construct(array $config = null, $id = NULL) {
            if (!isset($config['group'])) {
                $config['group'] = 'memcache';
            }

            $this->_db = Cache::instance($config['group']);

            parent::__construct($config, $id);
        }

        public function id() {
            return $this->_session_id;
        }

        protected function _read($id = NULL) {
            if (!is_null($id) OR $id = Cookie::get($this->_name)) {
                $ret = $this->_db->get($id);
                if (false !== $ret) {
                    $this->_session_id = $this->_update_id = $id;
                    return $ret;
                }
            }

            $this->_regenerate();

            return NULL;
        }

        protected function _regenerate() {
            do {
                $id = str_replace('.', '', uniqid(NULL, TRUE));

                $ret = $this->_db->get($id);
            } while (!is_null($ret));

            return $this->_session_id = $id;
        }

        protected function _write() {

            $this->_db->set($this->_session_id, $this->__toString(), $this->_lifetime);

            $this->_update_id = $this->_session_id;

            Cookie::set($this->_name, $this->_session_id, $this->_lifetime);

            return TRUE;
        }

        /**
         * @return  bool
         */
        protected function _restart() {
            $this->_regenerate();

            return TRUE;
        }

        protected function _destroy() {
            $ret = $this->_db->delete($this->_update_id);

            return $ret;
        }

    }

    // End Session_Cache

 

你可能感兴趣的:(memcache)