From 4df4ab500788f0792b75baf1fa98e4647d713ed1 Mon Sep 17 00:00:00 2001 From: corbosman Date: Thu, 19 Feb 2015 14:55:09 +0100 Subject: session refactor and add redis driver --- program/lib/Roundcube/rcube_session_memcache.php | 140 +++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 program/lib/Roundcube/rcube_session_memcache.php (limited to 'program/lib/Roundcube/rcube_session_memcache.php') diff --git a/program/lib/Roundcube/rcube_session_memcache.php b/program/lib/Roundcube/rcube_session_memcache.php new file mode 100644 index 000000000..85a4aa617 --- /dev/null +++ b/program/lib/Roundcube/rcube_session_memcache.php @@ -0,0 +1,140 @@ + | + | Author: Aleksander Machniak | + | Author: Cor Bosman | + +-----------------------------------------------------------------------+ +*/ + +/** + * Class to provide memcache session storage + * + * @package Framework + * @subpackage Core + * @author Thomas Bruederli + * @author Aleksander Machniak + * @author Cor Bosman + */ +class rcube_session_memcache extends rcube_session +{ + private $memcache; + + public function __construct() + { + $this->memcache = rcube::get_instance()->get_memcache(); + + if(! $this->memcache) { + rcube::raise_error(array('code' => 604, 'type' => 'db', + 'line' => __LINE__, 'file' => __FILE__, + 'message' => "Failed to connect to memcached. Please check configuration"), + true, true); + } + + // register sessions handler + $this->register_session_handler(); + + } + + /** + * @param $save_path + * @param $session_name + * @return bool + */ + public function open($save_path, $session_name) + { + return true; + } + + /** + * @return bool + */ + public function close() + { + return true; + } + + /** + * Handler for session_destroy() with memcache backend + * + * @param $key + * @return bool + */ + public function destroy($key) + { + if ($key) { + // #1488592: use 2nd argument + $this->memcache->delete($key, 0); + } + + return true; + } + + + /** + * Read session data from memcache + * + * @param $key + * @return null|string + */ + public function read($key) + { + if ($value = $this->memcache->get($key)) { + $arr = unserialize($value); + $this->changed = $arr['changed']; + $this->ip = $arr['ip']; + $this->vars = $arr['vars']; + $this->key = $key; + + return !empty($this->vars) ? (string) $this->vars : ''; + } + + return null; + } + + /** + * write data to memcache storage + * + * @param $key + * @param $vars + * @return bool + */ + public function write($key, $vars) + { + return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars)), + MEMCACHE_COMPRESSED, $this->lifetime + 60); + } + + /** + * update memcache session data + * + * @param $key + * @param $newvars + * @param $oldvars + * @return bool + */ + public function update($key, $newvars, $oldvars) + { + $ts = microtime(true); + + if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) { + return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)), + MEMCACHE_COMPRESSED, $this->lifetime + 60); + } + + return true; + } + +} \ No newline at end of file -- cgit v1.2.3