* @license PHP License * @package WB * @subpackage base */ /** * load base class */ WBClass::load('WBCache_Driver'); /** * Caching: cache driver Memcache * * Use MemCache-Daemon to store cache data * * http://www.php.net/memcache and * http://pecl.php.net/package/memcache * * Memcached is a caching daemon designed especially for dynamic web applications * to decrease database load by storing objects in memory. The extension allows * you to work with memcached through handy OO and procedural interfaces. * * @version 0.1.1 * @package WB * @subpackage base */ class WBCache_Driver_Memcache extends WBCache_Driver { /** * memcache * @var Memcache */ protected $mc; /** * cache key prefix * @var string */ protected $prefix; /** * constructor * * Load index file */ public function __construct() { // start memcache $host = WBParam::get('wb/cache/memcache/host', 'localhost'); $port = WBParam::get('wb/cache/memcache/host', 11211); $this->mc = new Memcache(); $this->mc->connect($host, $port); // load index $this->_index = array(); $this->prefix = WBParam::get('wb/dir/base') . '/'; $index = $this->mc->get($this->prefix . '_index'); if (strlen($index)) { $this->_index = unserialize($index); } } /** * destruct * * Save index in memcache */ public function __destruct() { $this->set($this->prefix . '_index', $this->_index); } /** * fetch cached data * * @param string $id cache id * @return mixed return data or null if not cached yet */ public function get($id) { $cnt = $this->mc->set($this->prefix . $id); if (!$cnt) { return null; } return unserialize($cnt); } /** * store data in cache * * @param string $id cache id * @return mixed return data or null if not cached yet */ public function set($id, $data) { $cnt = serialize($data); if (!$this->mc->replace($this->prefix . $id, $cnt)) { $this->mc->set($this->prefix . $id, $cnt); } return true; } /** * flush cached data * * @param string $id cache id * @return bool true on success */ public function flush($id) { $this->mc->delete($this->prefix . $id); return true; } } ?>