Skip to content

Commit

Permalink
Sync memcahe changes to memcached
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Narloch committed Aug 18, 2016
1 parent 127885f commit 82ac607
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 117 deletions.
9 changes: 5 additions & 4 deletions libraries/joomla/cache/storage/memcache.php
Expand Up @@ -259,11 +259,11 @@ public function clean($group, $mode = null)

if (is_array($index))
{
$secret = $this->_hash;
$prefix = $this->_hash . '-cache-' . $group . '-';

foreach ($index as $key => $value)
{
if (strpos($value->name, $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
if (strpos($value->name, $prefix) === 0 xor $mode != 'group')
{
static::$_db->delete($value->name);
unset($index[$key]);
Expand Down Expand Up @@ -320,7 +320,7 @@ public static function isSupported()
*/
public function lock($id, $group, $locktime)
{
$returning = new stdClass;
$returning = new stdClass;
$returning->locklooped = false;

$looptime = $locktime * 10;
Expand All @@ -333,7 +333,8 @@ public function lock($id, $group, $locktime)
{
$lock_counter = 0;

// Loop until you find that the lock has been released. That implies that data get from other thread has finished
// Loop until you find that the lock has been released.
// That implies that data get from other thread has finished.
while ($data_lock === false)
{
if ($lock_counter > $looptime)
Expand Down
178 changes: 65 additions & 113 deletions libraries/joomla/cache/storage/memcached.php
Expand Up @@ -25,14 +25,6 @@ class JCacheStorageMemcached extends JCacheStorage
*/
protected static $_db = null;

/**
* Persistent session flag
*
* @var boolean
* @since 12.1
*/
protected $_persistent = false;

/**
* Payload compression level
*
Expand All @@ -52,7 +44,9 @@ public function __construct($options = array())
{
parent::__construct($options);

if (static::isSupported() && static::$_db === null)
$this->_compress = JFactory::getConfig()->get('memcached_compress', false) ? Memcached::OPT_COMPRESSION : 0;

if (static::$_db === null)
{
$this->getConnection();
}
Expand All @@ -68,28 +62,54 @@ public function __construct($options = array())
*/
protected function getConnection()
{
$config = JFactory::getConfig();
$this->_persistent = $config->get('memcached_persist', true);
$this->_compress = $config->get('memcached_compress', false) == false ? 0 : Memcached::OPT_COMPRESSION;
if (!static::isSupported())
{
throw new RuntimeException('Memcache Extension is not available');
}

$config = JFactory::getConfig();

$host = $config->get('memcache_server_host', 'localhost');
$port = $config->get('memcache_server_port', 11211);


// Create the memcache connection
if ($this->_persistent)
if ($config->get('memcached_persist', true))
{
static::$_db = new Memcached(JFactory::getSession()->getId());
static::$_db = new Memcached($this->_hash);
$servers = static::$_db->getServerList();

if ($servers)
{
if ($servers[0]['host'] != $host || $servers[0]['host'] != $port)
{
static::$_db->resetServerList();
$servers = array();
}
}

if (!$servers)
{
static::$_db->addServer($host, $port);
}
}
else
{
static::$_db = new Memcached;
static::$_db->addServer($host, $port);
}

$memcachedtest = static::$_db->addServer($config->get('memcached_server_host', 'localhost'), $config->get('memcached_server_port', 11211));
static::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);

$stats = static::$_db->getStats();
$result = !empty($stats["$host:$port"]) && $stats["$host:$port"]['pid'] > 0;

if ($memcachedtest == false)
if (!$result)
{
throw new RuntimeException('Could not connect to memcached server', 404);
}
static::$_db = null;

static::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);
throw new RuntimeException('Could not connect to memcached server');
}
}

/**
Expand All @@ -105,9 +125,7 @@ protected function getConnection()
*/
public function get($id, $group, $checkTime = true)
{
$cache_id = $this->_getCacheId($id, $group);

return static::$_db->get($cache_id);
return static::$_db->get($this->_getCacheId($id, $group));
}

/**
Expand All @@ -124,7 +142,7 @@ public function getAll()

$data = array();

if (!empty($keys) && is_array($keys))
if (is_array($keys))
{
foreach ($keys as $key)
{
Expand Down Expand Up @@ -180,7 +198,7 @@ public function store($id, $group, $data)

$index = static::$_db->get($this->_hash . '-index');

if ($index === false)
if (!is_array($index))
{
$index = array();
}
Expand All @@ -193,11 +211,7 @@ public function store($id, $group, $data)
static::$_db->set($this->_hash . '-index', $index, 0);
$this->unlockindex();

// Prevent double writes, write only if it doesn't exist else replace
if (!static::$_db->replace($cache_id, $data, $this->_lifetime))
{
static::$_db->set($cache_id, $data, $this->_lifetime);
}
static::$_db->set($cache_id, $data, $this->_lifetime);

return true;
}
Expand All @@ -223,22 +237,19 @@ public function remove($id, $group)

$index = static::$_db->get($this->_hash . '-index');

if ($index === false)
{
$index = array();
}

foreach ($index as $key => $value)
if (is_array($index))
{
if ($value->name == $cache_id)
foreach ($index as $key => $value)
{
unset($index[$key]);
if ($value->name == $cache_id)
{
unset($index[$key]);
static::$_db->set($this->_hash . '-index', $index, 0);
break;
}
}

break;
}

static::$_db->set($this->_hash . '-index', $index, 0);
$this->unlockindex();

return static::$_db->delete($cache_id);
Expand Down Expand Up @@ -266,23 +277,22 @@ public function clean($group, $mode = null)

$index = static::$_db->get($this->_hash . '-index');

if ($index === false)
if (is_array($index))
{
$index = array();
}

$secret = $this->_hash;
$prefix = $this->_hash . '-cache-' . $group . '-';

foreach ($index as $key => $value)
{
if (strpos($value->name, $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
foreach ($index as $key => $value)
{
static::$_db->delete($value->name, 0);
unset($index[$key]);
if (strpos($value->name, $prefix) === 0 xor $mode != 'group')
{
static::$_db->delete($value->name);
unset($index[$key]);
}
}

static::$_db->set($this->_hash . '-index', $index, 0);
}

static::$_db->set($this->_hash . '-index', $index, 0);
$this->unlockindex();

return true;
Expand Down Expand Up @@ -318,17 +328,7 @@ public static function isSupported()
* GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
* If the class is there, we can assume support.
*/
if (!class_exists('Memcached'))
{
return false;
}

// Now check if we can connect to the specified Memcached server
$config = JFactory::getConfig();

$memcached = new Memcached;

return @$memcached->addServer($config->get('memcached_server_host', 'localhost'), $config->get('memcached_server_port', 11211));
return class_exists('Memcached');
}

/**
Expand All @@ -351,48 +351,27 @@ public function lock($id, $group, $locktime)

$cache_id = $this->_getCacheId($id, $group);

if (!$this->lockindex())
{
return false;
}

$index = static::$_db->get($this->_hash . '-index');

if ($index === false)
{
$index = array();
}

$tmparr = new stdClass;
$tmparr->name = $cache_id;
$tmparr->size = 1;

$index[] = $tmparr;
static::$_db->set($this->_hash . '-index', $index, 0);

$this->unlockindex();

$data_lock = static::$_db->add($cache_id . '_lock', 1, $locktime);

if ($data_lock === false)
{
$lock_counter = 0;

// Loop until you find that the lock has been released.
// That implies that data get from other thread has finished
// That implies that data get from other thread has finished.
while ($data_lock === false)
{
if ($lock_counter > $looptime)
{
$returning->locked = false;
$returning->locklooped = true;
break;
}

usleep(100);
$data_lock = static::$_db->add($cache_id . '_lock', 1, $locktime);
$lock_counter++;
}

$returning->locklooped = true;
}

$returning->locked = $data_lock;
Expand All @@ -413,32 +392,6 @@ public function lock($id, $group, $locktime)
public function unlock($id, $group = null)
{
$cache_id = $this->_getCacheId($id, $group) . '_lock';

if (!$this->lockindex())
{
return false;
}

$index = static::$_db->get($this->_hash . '-index');

if ($index === false)
{
$index = array();
}

foreach ($index as $key => $value)
{
if ($value->name == $cache_id)
{
unset($index[$key]);
}

break;
}

static::$_db->set($this->_hash . '-index', $index, 0);
$this->unlockindex();

return static::$_db->delete($cache_id);
}

Expand All @@ -464,7 +417,6 @@ protected function lockindex()
if ($lock_counter > $looptime)
{
return false;
break;
}

usleep(100);
Expand Down

0 comments on commit 82ac607

Please sign in to comment.