<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_Memcache { use vB_Trait_NoSerialize; protected static $instance; protected $config = null; protected $memcached = null; protected $memcached_connected = false; protected function __construct() { $this->memcached = new Memcache(); } public static function instance() { if (!isset(self::$instance)) { if (class_exists('Memcached', FALSE)) { $class = 'vB_Memcached'; } else if (class_exists('Memcache', FALSE)) { $class = __CLASS__; } else { throw new Exception('Memcached is not installed'); } self::$instance = new $class(); self::$instance->config = vB::getConfig(); } return self::$instance; } public function setConfig(&$config) { $this->config = & $config; } protected function addServers() { if (is_array($this->config['Misc']['memcacheserver'])) { if (method_exists($this->memcached, 'addServer')) { $connected = false; foreach (array_keys($this->config['Misc']['memcacheserver']) AS $key) { $res = vB_Utility_Functions::callSupressError( E_DEPRECATED, [$this->memcached, 'addServer'], $this->config['Misc']['memcacheserver'][$key], $this->config['Misc']['memcacheport'][$key], $this->config['Misc']['memcachepersistent'][$key], $this->config['Misc']['memcacheweight'][$key], $this->config['Misc']['memcachetimeout'][$key], $this->config['Misc']['memcacheretry_interval'][$key] ); if ($res === true) { $connected = true; } } if (!$connected) { return 3; } } else if (!$this->memcached->connect($this->config['Misc']['memcacheserver'][1], $this->config['Misc']['memcacheport'][1], $this->config['Misc']['memcachetimeout'][1])) { return 3; } } else if (!$this->memcached->connect($this->config['Misc']['memcacheserver'], $this->config['Misc']['memcacheport'])) { return 3; } return 1; } public function connect() { if (!$this->memcached_connected) { if (($res = $this->addServers()) !== 3) { $this->memcached_connected = true; } return $res; } return 2; } public function add($key, $value, $expiration = 0) { if (!$this->memcached_connected) { return false; } return $this->memcached->add($key, $value, MEMCACHE_COMPRESSED, $expiration); } public function set($key, $value, $expiration = 0) { if (!$this->memcached_connected) { return false; } return $this->memcached->set($key, $value, MEMCACHE_COMPRESSED, $expiration); } public function get($keys) { if (!$this->memcached_connected) { return false; } return $this->memcached->get($keys); } public function delete($key) { if (!$this->memcached_connected) { return false; } if (empty($key)) { return true; } return $this->memcached->delete($key,0); } public function flush() { if (!$this->memcached_connected) { return false; } return $this->memcached->flush(); } public function close() { if (!$this->memcached_connected) { return false; } $this->memcached_connected = false; return $this->memcached->close(); } } 