[horde] new SessionHandler utilizes memcached

Rong-En Fan rafan at csie.org
Thu Mar 10 02:14:27 PST 2005


--uAKRQypu60I7Lcqm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,

The attached is a new SessionHandler: memcached which
uses memcached written by Brad Fitzpatrick and PECL's
memcache API.

http://www.danga.com/memcached/
http://pecl.php.net/package/memcache/

This is deployed on our webmail system for few days.
Looks good. My php skills may not that good. So,
any comments are welcome and maybe this can be
added into Horde framework.

Regards,
Rong-En Fan

--uAKRQypu60I7Lcqm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="memcached.php"

<?php
/**
 * SessionHandler:: implementation for memchaced.
 *
 * Required values for $params:<pre>
 *     'hostspec'  --  The hostname of the memcached.
 *     'port'      --  The port on which to connect to the memcached.
 *
 * $Horde$
 *
 * Copyright 2005-2005 Rong-En Fan <rafan at infor.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Rong-En Fan <rafan at infor.org>
 * @since   Horde 3.0
 * @package Horde_SessionHandler
 */
class SessionHandler_memcached extends SessionHandler {

    /**
     * Hash containing connection parameters.
     *
     * @var array $_params
     */
    var $_params = array();

    /**
     * Current memcached connection.
     *
     * @var resource $_db
     */
    var $_db;

    /**
     * Constructs a new memcached SessionHandler object.
     *
     * @access public
     *
     * @param optional array $params  A hash containing connection parameters.
     *                                See details above.
     */
    function SessionHandler_memcached($params = array())
    {
        $this->_params = $params;
    }

    /**
     * TODO
     */
    function open($save_path, $session_name)
    {
    }

    /**
     * TODO
     */
    function close()
    {
    }

    /**
     * TODO
     */
    function read($id)
    {
        $this->_connect();

        $result = @memcache_get($this->_db, $id);

        $this->_disconnect();
        if (!$result){
            Horde::logMessage('Error retrieving session data (id = ' . $id . ')',
                              __FILE__, __LINE__, PEAR_LOG_ERR);
            return '';
        }

        return $result;
    }

    /**
     * TODO
     */
    function write($id, $session_data)
    {
        $this->_connect();

        $lifetime = ini_get('session.gc_maxlifetime');
        $result = @memcache_set($this->_db, $id, $session_data, $lifetime);

        $this->_disconnect();
        if (!$result){
            Horde::logMessage('Error writing session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        } 

        return true;
    }

    /**
     * TODO
     */
    function destroy($id)
    {
        $this->_connect();

        $result = @memcache_delete($this->_db, $id);

        $this->_disconnect();
        if (!$result) {
            Horde::logMessage('Failed to delete session (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        }

        return true;
    }

    /**
     * TODO
     */
    function gc($maxlifetime = 300)
    {
        return true;
    }

    /**
     * Attempts to open a connection to the memcached.
     *
     * @access private
     */
    function _connect()
    {
        Horde::assertDriverConfig($this->_params, 'sessionhandler',
            array('hostspec', 'port'),
            'session handler memcached');

        if (!$this->_db = @memcache_connect($this->_params['hostspec'],
                                            $this->_params['port'])) {
            Horde::fatal(PEAR::raiseError('Could not connect to memcached for memcached SessionHandler.'), __FILE__, __LINE__);
        }
    }

    /**
     * Disconnect from memcached.
     *
     * @access private
     */
    function _disconnect()
    {
        return @memcache_close($this->_db);
    }

}

--uAKRQypu60I7Lcqm--


More information about the horde mailing list