contributions/cache.memcache/session.memcache.impl.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Redirect session to write to Memcache 00004 * 00005 * Memcache(d) extension already comes with a session handler build in, so if you want to use 00006 * that, rather than this implementation, set constant APP_MEMCACHE_STORE_SESSIONS to FALSE in 00007 * your config files. 00008 * 00009 * @author Gerd Riesselmann 00010 * @ingroup Memcache 00011 */ 00012 class MemcacheSession implements ISessionHandler { 00013 /** 00014 * Open a session 00015 */ 00016 public function open($save_path, $session_name) { 00017 return true; 00018 } 00019 00020 /** 00021 * Close a session 00022 */ 00023 public function close() { 00024 //Note that for security reasons the Debian and Ubuntu distributions of 00025 //php do not call _gc to remove old sessions, but instead run /etc/cron.d/php*, 00026 //which check the value of session.gc_maxlifetime in php.ini and delete the session 00027 //files in /var/lib/php*. This is all fine, but it means if you write your own 00028 //session handlers you'll need to explicitly call your _gc function yourself. 00029 //A good place to do this is in your _close function 00030 00031 // Since Memcache takes care of life time, no gc is needed 00032 //$this->gc(ini_get('session.gc_maxlifetime')); 00033 return true; 00034 } 00035 00036 /** 00037 * Load session data from xcache 00038 */ 00039 public function read($key) { 00040 // Write and Close handlers are called after destructing objects since PHP 5.0.5 00041 // Thus destructors can use sessions but session handler can't use objects. 00042 // So we are moving session closure before destructing objects. 00043 register_shutdown_function('session_write_close'); 00044 $key = $this->create_key($key); 00045 00046 $ret = GyroMemcache::get($key); 00047 if ($ret === false) { 00048 $ret = ''; 00049 } 00050 return $ret; 00051 } 00052 00053 /** 00054 * Write session data to XCache 00055 */ 00056 public function write($key, $value) { 00057 try { 00058 GyroMemcache::set($this->create_key($key), $value, ini_get('session.gc_maxlifetime')); 00059 return true; 00060 } 00061 catch(Exception $ex) { 00062 return false; 00063 } 00064 } 00065 00066 /** 00067 * Delete a session 00068 */ 00069 public function destroy($key) { 00070 GyroMemcache::delete($this->create_key($key)); 00071 } 00072 00073 /** 00074 * Delete outdated sessions 00075 */ 00076 public function gc($lifetime) { 00077 // Memcache does this for us 00078 return true; 00079 } 00080 00081 protected function create_key($key) { 00082 return 'g$s' . Config::get_url(Config::URL_DOMAIN) . '_' . $key; 00083 } 00084 }