Skip to content

Commit

Permalink
Adding memcacheD cache driver
Browse files Browse the repository at this point in the history
git-svn-id: http://manialib.googlecode.com/svn/trunk@807 e79368ca-600c-11de-ad4a-3d330faba797
  • Loading branch information
magnetik committed Oct 22, 2013
1 parent dece766 commit 76f47de
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libraries/ManiaLib/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const APC = 'apc';
const MEMCACHE = 'memcache';
const MEMCACHED = 'memcached';
const MYSQL = 'mysql';
const NONE = 'nocache';

Expand All @@ -37,6 +38,7 @@ static function factory($driver = null)
switch($driver)
{
case APC: return static::getDriver('APC');
case MEMCACHED: return static::getDriver('Memcached');
case MEMCACHE: return static::getDriver('Memcache');
case MYSQL: return static::getDriver('MySQL');
default: throw new Exception();
Expand Down
98 changes: 98 additions & 0 deletions libraries/ManiaLib/Cache/Drivers/Memcached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* ManiaLib - Lightweight PHP framework for Manialinks
*
* @see http://code.google.com/p/manialib/
* @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
* @version $Revision$:
* @author $Author$:
* @date $Date$:
*/

namespace ManiaLib\Cache\Drivers;

/**
* Memcache driver based on the PECL\Memcache extension
* @see http://www.php.net/manual/en/book.memcached.php
*/
class Memcached extends \ManiaLib\Utils\Singleton implements \ManiaLib\Cache\CacheInterface
{

/**
* @var \Memcached
*/
protected $memcached;

protected function __construct()
{
if(!class_exists('Memcached'))
{
throw new Exception('PECL\Memcached extension not found');
}
$this->memcached = new \Memcached();

$config = MemcacheConfig::getInstance();
foreach($config->hosts as $host)
{
$this->memcached->addServer($host);
}
}

/**
* @deprecated
*/
function exists($key)
{
return!($this->fetch($key) === false);
}

function fetch($key)
{
$key = str_replace('\\', '/', $key);
return $this->memcached->get($key);
}

function add($key, $value, $ttl=0)
{
$key = str_replace('\\', '/', $key);
if(!$this->memcached->add($key, $value, false, $ttl))
{
$message = sprintf('Memcache::set() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}

function replace($key, $value, $ttl=0)
{
$key = str_replace('\\', '/', $key);
if(!$this->memcached->replace($key, $value, false, $ttl))
{
$message = sprintf('Memcache::replace() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}

function delete($key)
{
$key = str_replace('\\', '/', $key);
if(!$this->memcached->delete($key))
{
$message = sprintf('Memcache::delete() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}

function inc($key)
{
$key = str_replace('\\', '/', $key);
if(!$this->memcached->increment($key))
{
$message = sprintf('Memcache::increment() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}

}

?>

0 comments on commit 76f47de

Please sign in to comment.