Location: PHPKode > projects > OpenNitro > trunk/Nitro/SessionHandler/memcache.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Session Handler :: memcache                                      |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2005 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: memcache.inc.php 229 2008-04-17 09:20:31Z oli $
//
// This file contains the memcache Nitro Session Handler
//

/**
 * This file contains the memcache Nitro Session Handler
 *
 * To use the memcache handler in stead of the default PHP file based
 * handler you must define NITRO_SESSION_HANDLER as memcache in your index
 * file or your NitroBaseConfig file.
 *
 * <CODE>
 * define('NITRO_SESSION_HANDLER', 'memcache');
 * </CODE>
 *
 * @package	Nitro
 * @subpackage	Session
 * @author 		Siggi Oskarsson
 * @version 	$Revision: 1.39 $
 * @copyright	2005 June Systems BV
 */

if (extension_loaded('memcache')) {
	// This class requires the memcache extension to be loaded!
	if (!defined('NITRO_SESSION_HANDLER_MEMCACHE_HOST')) define('NITRO_SESSION_HANDLER_MEMCACHE_HOST', 'localhost');
	if (!defined('NITRO_SESSION_HANDLER_MEMCACHE_PORT')) define('NITRO_SESSION_HANDLER_MEMCACHE_PORT', 11211);

	function NitroSessionHandler_memcache_open($save_path = FALSE, $session_name = FALSE) 
	{
	  global $_nitro_memcache;
		DebugGroup('Session', __FUNCTION__, "Opening session: ".$session_name, __FILE__, __LINE__, DEBUG_SESS_OK);
	  // nothing needs to be done

		$_nitro_memcache = memcache_connect(NITRO_SESSION_HANDLER_MEMCACHE_HOST, NITRO_SESSION_HANDLER_MEMCACHE_PORT);
		if (!$_nitro_memcache) Debug('Session', __FUNCTION__, "Failed opening connection to host: ".NITRO_SESSION_HANDLER_MEMCACHE_HOST, __FILE__, __LINE__, DEBUG_SESS_ERR);

		DebugCloseGroup(DEBUG_SESS_OK);
	  return TRUE;
	}
	
	function NitroSessionHandler_memcache_close() 
	{
	  global $_nitro_memcache;
		DebugGroup('Session', __FUNCTION__, "Closing session", __FILE__, __LINE__, DEBUG_SESS_OK);

		if (!$_nitro_memcache) $_nitro_memcache->close();

		DebugCloseGroup(DEBUG_SESS_OK);
	  return TRUE;
	}
	
	function NitroSessionHandler_memcache_read($id) 
	{
	  global $_nitro_memcache;
		DebugGroup('Session', __FUNCTION__, "Read session: $id", __FILE__, __LINE__, DEBUG_SESS_OK);

		if (!$_nitro_memcache) NitroSessionHandler_memcache_open();

		$RV = $_nitro_memcache->get('PHPSESS_'.$id);
		if (!$RV) Debug('Session', __FUNCTION__, "Failed reading session", __FILE__, __LINE__, DEBUG_SESS_ERR);
		if (!$RV) $RV = "";

		DebugCloseGroup(DEBUG_SESS_OK);
		return $RV;
	}
	
	function NitroSessionHandler_memcache_write($id, $sess_data) 
	{
	  global $_nitro_memcache, $__Nitro_ReadOnly_Session;
		
		if ($__Nitro_ReadOnly_Session) {
			DebugGroup('Session', __FUNCTION__, "Read Only session, not saving!", __FILE__, __LINE__, DEBUG_SESS_OK);
		} else {
			DebugGroup('Session', __FUNCTION__, "Write session: $id (".strlen($sess_data)." bytes)", __FILE__, __LINE__, DEBUG_SESS_OK);
	
			if (!$_nitro_memcache) NitroSessionHandler_memcache_open();
			
			$expire = 8*60*60; // expiration time in seconds, for sessions 8 hours
			$rv = $_nitro_memcache->replace('PHPSESS_'.$id, $sess_data, MEMCACHE_COMPRESSED, $expire);
			if (!$rv) {
				Debug('Session', __FUNCTION__, "Failed replacing data in session. New session? Trying to set data.", __FILE__, __LINE__, DEBUG_SESS_ERR);
				$rv = $_nitro_memcache->set('PHPSESS_'.$id, $sess_data, MEMCACHE_COMPRESSED, $expire);
				if (!$rv) Debug('Session', __FUNCTION__, "Failed setting data for session", __FILE__, __LINE__, DEBUG_SESS_ERR);
			}
		}

		DebugCloseGroup(DEBUG_SESS_OK);
		return $rv;
	}
	
	function NitroSessionHandler_memcache_destroy($id) 
	{
	  global $_nitro_memcache;
		DebugGroup('Session', __FUNCTION__, "Destroy session: $id", __FILE__, __LINE__, DEBUG_SESS_OK);

		if (!$_nitro_memcache) NitroSessionHandler_memcache_open();

		$rv = $_nitro_memcache->delete('PHPSESS_'.$id);
		if (!$rv) Debug('Session', __FUNCTION__, "Failed destroying session", __FILE__, __LINE__, DEBUG_SESS_ERR);

		DebugCloseGroup(DEBUG_SESS_OK);
		return $rv;
	}
	
	function NitroSessionHandler_memcache_gc($maxlifetime)
	{
		DebugGroup('Session', __FUNCTION__, "Garbage collection called", __FILE__, __LINE__, DEBUG_SESS_OK);
		DebugCloseGroup(DEBUG_SESS_OK);
	  return true;
	}
	
	function NitroSession_DeleteSessionData($id)
	{
		DebugGroup('Session', __FUNCTION__, "Delete session data for: $id", __FILE__, __LINE__, DEBUG_SESS_OK);

		NitroSessionHandler_memcache_destroy($id);

		DebugCloseGroup(DEBUG_SESS_OK);
	}

	function NitroSession_SaveToDisk()
	{
		DebugGroup('Session', __FUNCTION__, "Save and close session", __FILE__, __LINE__, DEBUG_SESS_OK);
		/*
		//make a backup copy of the current changed session
		//$tmp = $_SESSION;
		//re-start session, original session settings get loaded
		// we must set the save handlers again here! PHP bug!
		session_set_save_handler(
			"NitroSessionHandler_memcache_open",
			"NitroSessionHandler_memcache_close",
			"NitroSessionHandler_memcache_read",
			"NitroSessionHandler_memcache_write",
			"NitroSessionHandler_memcache_destroy",
			"NitroSessionHandler_memcache_gc"
		);
		//session_start();
		//overwrite reloaded session settings with backed up setting of current script
		//$_SESSION = $tmp;
		//close the session again so it gets written to disk for use with next page/script
		*/
		session_write_close();
		DebugCloseGroup(DEBUG_SESS_OK);
	}
	
	session_set_save_handler(
		"NitroSessionHandler_memcache_open",
		"NitroSessionHandler_memcache_close",
		"NitroSessionHandler_memcache_read",
		"NitroSessionHandler_memcache_write",
		"NitroSessionHandler_memcache_destroy",
		"NitroSessionHandler_memcache_gc"
	);
}
?>
Return current item: OpenNitro