namespace Friendica\Core\Cache;
-use Friendica\BaseObject;
use Friendica\Core\Cache;
+use Exception;
+use Memcache;
+
/**
* Memcache Cache Driver
*
- * @author Hypolite Petovan <mrpetovan@gmail.com>
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
-class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver
+class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
{
use TraitCompareSet;
use TraitCompareDelete;
/**
- * @var \Memcache
+ * @var Memcache
*/
private $memcache;
+ /**
+ * @param string $memcache_host
+ * @param int $memcache_port
+ * @throws Exception
+ */
public function __construct($memcache_host, $memcache_port)
{
if (!class_exists('Memcache', false)) {
- throw new \Exception('Memcache class isn\'t available');
+ throw new Exception('Memcache class isn\'t available');
}
- $this->memcache = new \Memcache();
+ $this->memcache = new Memcache();
if (!$this->memcache->connect($memcache_host, $memcache_port)) {
- throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
+ throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
+ }
+ }
+
+ /**
+ * (@inheritdoc)
+ */
+ public function getAllKeys($prefix = null)
+ {
+ $keys = [];
+ $allSlabs = $this->memcache->getExtendedStats('slabs');
+ foreach ($allSlabs as $slabs) {
+ foreach (array_keys($slabs) as $slabId) {
+ $cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId);
+ foreach ($cachedump as $key => $arrVal) {
+ if (!is_array($arrVal)) {
+ continue;
+ }
+ $keys = array_merge($keys, array_keys($arrVal));
+ }
+ }
}
+
+ $keys = $this->getOriginalKeys($keys);
+
+ return $this->filterArrayKeysByPrefix($keys, $prefix);
}
/**
public function get($key)
{
$return = null;
+ $cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications
- $cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
+ $cached = $this->memcache->get($cachekey);
// @see http://php.net/manual/en/memcache.get.php#84275
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
+ $cachekey = $this->getCacheKey($key);
+
// We store with the hostname as key to avoid problems with other applications
if ($ttl > 0) {
return $this->memcache->set(
- self::getApp()->get_hostname() . ":" . $key,
+ $cachekey,
serialize($value),
MEMCACHE_COMPRESSED,
time() + $ttl
);
} else {
return $this->memcache->set(
- self::getApp()->get_hostname() . ":" . $key,
+ $cachekey,
serialize($value),
MEMCACHE_COMPRESSED
);
*/
public function delete($key)
{
- return $this->memcache->delete($key);
+ $cachekey = $this->getCacheKey($key);
+ return $this->memcache->delete($cachekey);
}
- public function clear()
+ /**
+ * (@inheritdoc)
+ */
+ public function clear($outdated = true)
{
- return $this->memcache->flush();
+ if ($outdated) {
+ return true;
+ } else {
+ return $this->memcache->flush();
+ }
}
/**
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
- return $this->memcache->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);
+ $cachekey = $this->getCacheKey($key);
+ return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
}
}