diff --git a/core/Cache/CachePeer.class.php b/core/Cache/CachePeer.class.php index 35ea662b27..f6c41e1b80 100644 --- a/core/Cache/CachePeer.class.php +++ b/core/Cache/CachePeer.class.php @@ -68,7 +68,7 @@ abstract public function delete($key) abstract public function isAlive() - Memcached <- CachePeer: + SocketMemcached <- CachePeer: public function __construct( $host = Memcached::DEFAULT_PORT, diff --git a/core/Cache/PeclMemcache.class.php b/core/Cache/PeclMemcache.class.php new file mode 100644 index 0000000000..1695e8b93d --- /dev/null +++ b/core/Cache/PeclMemcache.class.php @@ -0,0 +1,171 @@ +instance = new Memcache(); + + try { + try { + $this->instance->pconnect($host, $port); + } catch (BaseException $e) { + $this->instance->connect($host, $port); + } + + $this->alive = true; + } catch (BaseException $e) { + // bad luck. + } + } + + public function __destruct() + { + if ($this->alive) { + try { + $this->instance->close(); + } catch (BaseException $e) { + // shhhh. + } + } + } + + /** + * @return PeclMemcached + **/ + public function clean() + { + try { + $this->instance->flush(); + } catch (BaseException $e) { + $this->alive = false; + } + + return parent::clean(); + } + + public function increment($key, $value) + { + try { + return $this->instance->increment($key, $value); + } catch (BaseException $e) { + return null; + } + } + + public function decrement($key, $value) + { + try { + return $this->instance->decrement($key, $value); + } catch (BaseException $e) { + return null; + } + } + + public function getList($indexes) + { + return + ($return = $this->get($indexes)) + ? $return + : array(); + } + + public function get($index) + { + try { + return $this->instance->get($index); + } catch (BaseException $e) { + if(strpos($e->getMessage(), 'Invalid key') !== false) + return null; + + $this->alive = false; + + return null; + } + + Assert::isUnreachable(); + } + + public function delete($index) + { + try { + // second parameter required, wrt new memcached protocol: + // delete key 0 (see process_delete_command in the memcached.c) + // Warning: it is workaround! + return $this->instance->delete($index, 0); + } catch (BaseException $e) { + return $this->alive = false; + } + + Assert::isUnreachable(); + } + + public function append($key, $data) + { + try { + return $this->instance->append($key, $data); + } catch (BaseException $e) { + return $this->alive = false; + } + + Assert::isUnreachable(); + } + + protected function store( + $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM + ) + { + try { + return + $this->instance->$action( + $key, + $value, + $this->compress + ? MEMCACHE_COMPRESSED + : false, + $expires + ); + } catch (BaseException $e) { + return $this->alive = false; + } + + Assert::isUnreachable(); + } + } +?> \ No newline at end of file diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index e2c41fc41d..098fc1e788 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -34,18 +34,18 @@ class PeclMemcached extends CachePeer * @return PeclMemcached **/ public static function create( - $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT, - $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT + $host = self::DEFAULT_HOST, + $port = self::DEFAULT_PORT, + $connectTimeout = self::DEFAULT_TIMEOUT ) { return new self($host, $port, $connectTimeout); } public function __construct( - $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT, - $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT + $host = self::DEFAULT_HOST, + $port = self::DEFAULT_PORT, + $connectTimeout = self::DEFAULT_TIMEOUT ) { $this->host = $host; diff --git a/core/Cache/Memcached.class.php b/core/Cache/SocketMemcached.class.php similarity index 95% rename from core/Cache/Memcached.class.php rename to core/Cache/SocketMemcached.class.php index f05fe55acb..8a34f7c3f0 100644 --- a/core/Cache/Memcached.class.php +++ b/core/Cache/SocketMemcached.class.php @@ -19,7 +19,7 @@ * * @ingroup Cache **/ - class Memcached extends CachePeer + class SocketMemcached extends CachePeer { const DEFAULT_PORT = 11211; const DEFAULT_HOST = '127.0.0.1'; @@ -29,24 +29,24 @@ class Memcached extends CachePeer private $timeout = null; - private $buffer = Memcached::DEFAULT_BUFFER; + private $buffer = self::DEFAULT_BUFFER; /** - * @return Memcached + * @return SocketMemcached **/ public static function create( - $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT, - $buffer = Memcached::DEFAULT_BUFFER + $host = self::DEFAULT_HOST, + $port = self::DEFAULT_PORT, + $buffer = self::DEFAULT_BUFFER ) { - return new Memcached($host, $port, $buffer); + return new self($host, $port, $buffer); } public function __construct( - $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT, - $buffer = Memcached::DEFAULT_BUFFER + $host = self::DEFAULT_HOST, + $port = self::DEFAULT_PORT, + $buffer = self::DEFAULT_BUFFER ) { $errno = $errstr = null; @@ -70,7 +70,7 @@ public function __destruct() } /** - * @return Memcached + * @return SocketMemcached */ public function setTimeout($microseconds) { @@ -90,7 +90,7 @@ public function setTimeout($microseconds) /** - * @return Memcached + * @return SocketMemcached **/ public function clean() { diff --git a/doc/ChangeLog b/doc/ChangeLog index 31fd06d6a9..26e4436ac4 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,11 @@ +2012-05-28 Georgiy T. Kutsurua + + * core/Cache/PeclMemcache.class.php + core/Cache/PeclMemcached.class.php + core/Cache/{Memcached.class.php → SocketMemcached.class.php} + +Code clean & add SocketMemcached instead of Memcached + 2012-05-23 Georgiy T. Kutsurua * main/Base/LightMetaProperty.class.php: fix after Enum update diff --git a/doc/examples/cacheSettings.php b/doc/examples/cacheSettings.php index dd32d9f44d..e6d766ad65 100644 --- a/doc/examples/cacheSettings.php +++ b/doc/examples/cacheSettings.php @@ -6,7 +6,7 @@ // set up default cache peer Cache::setPeer( - Memcached::create() + SocketMemcached::create() ); // or even several aggregated peers @@ -15,7 +15,7 @@ AggregateCache::create()-> addPeer( 'memcached daemon at localhost', - Memcached::create() + SocketMemcached::create() )-> addPeer( 'local low-priority file system', diff --git a/test/core/AggregateCacheTest.class.php b/test/core/AggregateCacheTest.class.php index 00332b9c63..08afaac5d8 100644 --- a/test/core/AggregateCacheTest.class.php +++ b/test/core/AggregateCacheTest.class.php @@ -8,11 +8,11 @@ public function testAggregateCache() { return $this->doTestMemcached( AggregateCache::create()-> - addPeer('low', Memcached::create(), AggregateCache::LEVEL_LOW)-> - addPeer('normal1', Memcached::create())-> - addPeer('normal2', Memcached::create())-> - addPeer('normal3', Memcached::create())-> - addPeer('high', Memcached::create(), AggregateCache::LEVEL_HIGH)-> + addPeer('low', SocketMemcached::create(), AggregateCache::LEVEL_LOW)-> + addPeer('normal1', SocketMemcached::create())-> + addPeer('normal2', SocketMemcached::create())-> + addPeer('normal3', SocketMemcached::create())-> + addPeer('high', SocketMemcached::create(), AggregateCache::LEVEL_HIGH)-> setClassLevel('one', 0xb000) ); } @@ -33,11 +33,11 @@ public function testSimpleAggregateCache() { return $this->doTestMemcached( SimpleAggregateCache::create()-> - addPeer('low', Memcached::create(), AggregateCache::LEVEL_LOW)-> - addPeer('normal1', Memcached::create())-> - addPeer('normal2', Memcached::create())-> - addPeer('normal3', Memcached::create())-> - addPeer('high', Memcached::create(), AggregateCache::LEVEL_HIGH)-> + addPeer('low', SocketMemcached::create(), AggregateCache::LEVEL_LOW)-> + addPeer('normal1', SocketMemcached::create())-> + addPeer('normal2', SocketMemcached::create())-> + addPeer('normal3', SocketMemcached::create())-> + addPeer('high', SocketMemcached::create(), AggregateCache::LEVEL_HIGH)-> setClassLevel('one', 0xb000) ); } @@ -47,7 +47,7 @@ public function testCyclicAggregateCache() $this->doTestMemcached( CyclicAggregateCache::create()-> setSummaryWeight(42)-> - addPeer('first', Memcached::create(), 25)-> + addPeer('first', SocketMemcached::create(), 25)-> addPeer('second', PeclMemcached::create(), 1)-> addPeer('third', PeclMemcached::create(), 13) ); diff --git a/test/core/BaseCachesTest.class.php b/test/core/BaseCachesTest.class.php index c464522aef..82e7bcc0ff 100644 --- a/test/core/BaseCachesTest.class.php +++ b/test/core/BaseCachesTest.class.php @@ -5,7 +5,7 @@ final class BaseCachesTest extends TestCase public static function cacheProvider() { return array( -// array(Memcached::create()), +// array(SocketMemcached::create()), // array(SharedMemory::create()), // array(PeclMemcached::create()), array(RuntimeMemory::create()), @@ -61,7 +61,7 @@ public function testWrongKeys(CachePeer $cache) public function testWithTimeout() { $cache = - Memcached::create('localhost')-> + SocketMemcached::create('localhost')-> setTimeout(200); $cache->add('a', 'b'); diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index 531ff17cd1..31b975dfcd 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -16,7 +16,7 @@ public function testMultiCacheAliveLast() $alifePeer = new PeclMemcached("127.0.0.1", "11211"); //some existing memcached $alifePeer->set('some_key', 'some_value'); - $deadPeer = new Memcached("165.42.42.42", "11211"); //some not existing memcache + $deadPeer = new SocketMemcached("165.42.42.42", "11211"); //some not existing memcache $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache @@ -34,7 +34,7 @@ public function testMultiCacheAliveLast() public function testMultiCacheAliveFirst() { - $alifePeer = new Memcached("127.0.0.1", "11211"); //some existing memcached + $alifePeer = new SocketMemcached("127.0.0.1", "11211"); //some existing memcached $alifePeer->set('some_key', 'some_value'); $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache @@ -54,7 +54,7 @@ public function testMultiCacheAliveOnly() CyclicAggregateCache::create()-> //some existing memcached setSummaryWeight(42)-> addPeer('first', new PeclMemcached("127.0.0.1", "11211"), 0)-> - addPeer('second', new Memcached("127.0.0.1", "11211"), 21); + addPeer('second', new SocketMemcached("127.0.0.1", "11211"), 21); $alifePeer->set('some_key', 'some_value'); @@ -77,4 +77,4 @@ public function testMultiCacheNoAlive() $result = $cache->get("some_key"); //will throw RuntimeException } - } + } \ No newline at end of file