[Wact-cvs] SF.net SVN: wact:[979] trunk/Wact/Cache/Store
Status: Pre-Alpha
Brought to you by:
jeffmoore
From: <nor...@us...> - 2009-06-15 16:31:41
|
Revision: 979 http://wact.svn.sourceforge.net/wact/?rev=979&view=rev Author: norbertmocsnik Date: 2009-06-15 16:30:39 +0000 (Mon, 15 Jun 2009) Log Message: ----------- Added memcached cache store. Added Paths: ----------- trunk/Wact/Cache/Store/Memcache.inc.php trunk/Wact/Cache/Store/Memcache.test.php Added: trunk/Wact/Cache/Store/Memcache.inc.php =================================================================== --- trunk/Wact/Cache/Store/Memcache.inc.php (rev 0) +++ trunk/Wact/Cache/Store/Memcache.inc.php 2009-06-15 16:30:39 UTC (rev 979) @@ -0,0 +1,168 @@ +<?php +/** +* Web Application Component Toolkit +* +* @link http://www.phpwact.org/ +* +* @author Wact Development Team +* @link http://www.phpwact.org/team +* +* @copyright Copyright 2006, Jeff Moore +* @license http://opensource.org/licenses/mit-license.php MIT +*/ + +/** +*/ +class Wact_Cache_Store_Memcache implements Wact_Cache_Store { + + /** + * @var string memcached host + */ + protected $host; + + /** + * @var int memcached port + */ + protected $port; + + /** + * @var int Connection timeout + */ + protected $timeout; + + /** + * @var Memcache Memcache object + */ + protected $memcache; + + /** + * @var bool Connection state + */ + protected $connected = FALSE; + + /** + * Is the extension that supports this cache store available? + */ + function isAvailable() { + return extension_loaded('memcache'); + } + + /** + * Class constructor. + * + * Stores connection parameters but doesn't connect + * to the memcached system yet. + * + * @param $host string + * @param $port int + * @param $timeout int + */ + public function __construct($host, $port = 11211, $timeout = NULL) { + $this->host = $host; + $this->port = $port; + $this->timeout = $timeout; + } + + /** + * Called internally before the first use of the cache. + * + * Throws Wact_Cache_Exception on connection failure. + */ + protected function connect() { + $this->memcache = new Memcache(); + if (!$this->memcache->connect($this->host, $this->port, $this->timeout)) { + throw new Wact_Cache_Exception('Could not connect to memcached.'); + } + } + + /** + * @see Wact_Cache_Store.store + */ + public function store($key, $data, $lifetime = NULL) { + if (!$this->connected) { + $this->connect(); + } + + settype($key, 'string'); + return $this->memcache->set($key, (string) $data, 0, $lifetime); + } + + /** + * @see Wact_Cache_Store.storeValue + */ + public function storeValue($key, $data, $lifetime = NULL) { + return $this->store($key, serialize($data), $lifetime); + } + + /** + * @see Wact_Cache_Store.fetch + */ + public function fetch($key, $default = NULL) { + if (!$this->connected) { + $this->connect(); + } + + settype($key, 'string'); + $result = $this->memcache->get($key); + if ($result === FALSE) { + return $default; + } else { + return $result; + } + } + + /** + * @see Wact_Cache_Store.fetch + */ + public function fetchValue($key, $default = NULL) { + $result = $this->fetch($key, FALSE); + if ($result === FALSE) { + return $default; + } + return unserialize($result); + } + + /** + * @see Wact_Cache_Store.includeCode + */ + public function includeCode($key) { + $code = $this->fetch($key, FALSE); + if ($code !== FALSE) { + // Strange concatination works better with source code parsing tools + $result = eval(' ?' . '>' . $code . '<' . '?php '); + + if (is_null($result)) { + return TRUE; + } else { + return $result; + } + } + return FALSE; + } + + /** + * @see Wact_Cache_Store.remove + */ + public function remove($key) { + if (!$this->connected) { + $this->connect(); + } + + settype($key, 'string'); + return $this->memcache->delete($key); + } + + /** + * @see Wact_Cache_Store.clear + */ + public function clear() { + if (!$this->connected) { + $this->connect(); + } + + return $this->memcache->flush(); + } + +} + +?> \ No newline at end of file Added: trunk/Wact/Cache/Store/Memcache.test.php =================================================================== --- trunk/Wact/Cache/Store/Memcache.test.php (rev 0) +++ trunk/Wact/Cache/Store/Memcache.test.php 2009-06-15 16:30:39 UTC (rev 979) @@ -0,0 +1,24 @@ +<?php + +class WactMemcacheCacheTestCase extends Wact_Cache_Store_Test { + + /** + * @var Wact_Cache_Store_Memcache + */ + protected $cache; + + function setUp() { + $this->cache = new Wact_Cache_Store_Memcache('localhost'); + } + + function tearDown() { + $this->cache->clear(); + } + + function skip() { + $this->skipUnless(extension_loaded('memcache')); + } + +} + +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |