Permalink
Browse files

Adding chain cache for nodes data

  • Loading branch information...
1 parent 950368a commit cb2166d2372e82d01787296426a7a62ddebd9c53 @ghecquet ghecquet committed Jun 29, 2016
@@ -31,7 +31,11 @@
define('XCACHE_EXTENSION_LOADED', extension_loaded('xcache'));
use \Doctrine\Common\Cache;
+use Doctrine\Common\Cache\ArrayCache;
+use Doctrine\Common\Cache\ChainCache;
use Pydio\Cache\Core\AbstractCacheDriver;
+use Pydio\Cache\Doctrine\Ext\PydioChainCache;
+use Pydio\Core\Model\Context;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Utils\ApplicationState;
use Pydio\Core\Utils\Vars\StringHelper;
@@ -115,7 +119,7 @@ public function init(ContextInterface $ctx, $options = [])
*/
private function initCacheWithNamespace($namespace){
$cacheDriver = null;
- $emptyContext = \Pydio\Core\Model\Context::emptyContext();
+ $emptyContext = Context::emptyContext();
$driverOptions = $this->getContextualOption($emptyContext, "DRIVER");
$cachePrefix = $this->getContextualOption($emptyContext, "CACHE_PREFIX");
@@ -150,7 +154,19 @@ private function initCacheWithNamespace($namespace){
if(empty($cachePrefix)){
$cachePrefix = StringHelper::slugify(ApplicationState::detectServerURL(true));
}
+
$cachePrefix .= "_".$namespace."_";
+
+ // Using chained array for nodes
+ if ($namespace == AJXP_CACHE_SERVICE_NS_NODES) {
+ $arrayCache = new ArrayCache();
+
+ $cacheDriver = new PydioChainCache([
+ $arrayCache,
+ $cacheDriver
+ ]);
+ }
+
$cacheDriver->setNamespace($cachePrefix);
return $cacheDriver;
@@ -20,13 +20,15 @@
*/
namespace Pydio\Cache\Doctrine\Ext;
+use Doctrine\Common\Cache\ApcuCache;
+
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Class PydioApcuCache
* @package Pydio\Cache\Doctrine\Ext
*/
-class PydioApcuCache extends \Doctrine\Common\Cache\ApcuCache implements PatternClearableCache
+class PydioApcuCache extends ApcuCache implements PatternClearableCache
{
protected $internalNamespace;
protected $internalNamespaceVersion;
@@ -38,17 +40,16 @@ class PydioApcuCache extends \Doctrine\Common\Cache\ApcuCache implements Pattern
*
* @return string The namespaced id.
*/
- private function namespacedIdAsPattern($id)
- {
+ private function namespacedIdAsPattern($id) {
+
return sprintf('%s\['.preg_quote($id, "/"), $this->internalNamespace);
}
/**
* @param string $pattern
* @return bool
*/
- public function deleteKeysStartingWith($pattern)
- {
+ public function deleteKeysStartingWith($pattern) {
$pattern = '/^'.$this->namespacedIdAsPattern($pattern).'/';
//SAMPLE /^pydio-unique-id_nodes_\[list\:\/\/1/
$iterator = new \APCIterator('user', $pattern);
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: ghecquet
+ * Date: 29/06/16
+ * Time: 10:07
+ */
+
+namespace Pydio\Cache\Doctrine\Ext;
+
+
+use Doctrine\Common\Cache\ChainCache;
+
+class PydioChainCache extends ChainCache implements PatternClearableCache {
+ /**
+ * @var Redis
+ */
+ protected $cacheProviders;
+
+ protected $internalNamespace;
+ protected $internalNamespaceVersion;
+
+
+ public function __construct($cacheProviders)
+ {
+ parent::__construct($cacheProviders);
+
+ $this->cacheProviders = $cacheProviders;
+ }
+
+ /**
+ * @param string $pattern
+ * @return bool
+ */
+ public function deleteKeysStartingWith($pattern) {
+ foreach ($this->cacheProviders as $cache) {
+
+ if(!($cache instanceof PatternClearableCache)) {
+ break;
+ }
+
+ /** @var PatternClearableCache $cache */
+ $pattern = $cache->deleteKeysStartingWith($pattern);
+ }
+ }
+
+ /**
+ * @param string $namespace
+ * @return void
+ */
+ public function setNamespace($namespace) {
+ foreach ($this->cacheProviders as $cache) {
+ $cache->setNamespace($namespace);
+ }
+ }
+}
@@ -21,14 +21,15 @@
namespace Pydio\Cache\Doctrine\Ext;
defined('AJXP_EXEC') or die('Access not allowed');
+use Doctrine\Common\Cache\RedisCache;
use Redis;
/**
* Class PydioRedisCache
* @package Pydio\Cache\Doctrine\Ext
*/
-class PydioRedisCache extends \Doctrine\Common\Cache\RedisCache implements PatternClearableCache
+class PydioRedisCache extends RedisCache implements PatternClearableCache
{
/**
* @var Redis
@@ -10,6 +10,9 @@
use GuzzleHttp\Stream\StreamDecoratorTrait;
use GuzzleHttp\Stream\StreamInterface;
use Pydio\Access\Core\Model\AJXP_Node;
+use Pydio\Cache\Core\AbstractCacheDriver;
+use Pydio\Cache\Core\CacheStreamLayer;
+use Pydio\Core\Services\CacheService;
/**
* Stream decorator that can cache previously read bytes from a sequentially
@@ -22,6 +25,9 @@ class MetadataCachingStream implements StreamInterface
/** @var array stat */
private static $stat;
+ /** @var AJXP_Node node */
+ private $node;
+
/** @var string uri */
private $uri;
@@ -31,6 +37,9 @@ class MetadataCachingStream implements StreamInterface
/** @var string path */
private $path;
+ /** @var string statCacheId */
+ private $cacheId;
+
/**
* We will treat the buffer object as the body of the stream
*
@@ -43,7 +52,9 @@ public function __construct(
AJXP_Node $node,
StreamInterface $target = null
) {
+ $this->node = $node;
$this->uri = $node->getUrl();
+ $this->cacheId = AbstractCacheDriver::computeIdForNode($node, "meta");
$this->contentFilters = $node->getRepository()->getContentFilter()->filters;
$this->path = parse_url($this->uri, PHP_URL_PATH);
@@ -90,9 +101,10 @@ public function getContents() {
}
public function stat() {
- if (isset(self::$stat[$this->uri])) {
- return self::$stat[$this->uri];
- }
+
+ $stat = CacheService::fetch(AJXP_CACHE_SERVICE_NS_NODES, $this->cacheId);
+
+ if(is_array($stat)) return $stat;
$stats = $this->stream->stat();
@@ -106,13 +118,16 @@ public function stat() {
$path = $this->contentFilters[$path];
}
- $key = rtrim($this->uri . $path, "/");
- self::$stat[$key] = $stat;
+ $node = new AJXP_Node($this->uri . "/" . $path);
+
+ $id = AbstractCacheDriver::computeIdForNode($node, "meta");
+
+ CacheService::save(AJXP_CACHE_SERVICE_NS_NODES, $id, $stat);
}
} else {
- self::$stat[$this->uri] = $stats;
+ CacheService::save(AJXP_CACHE_SERVICE_NS_NODES, $this->cacheId, $stats);
}
- return self::$stat[$this->uri];
+ return CacheService::fetch(AJXP_CACHE_SERVICE_NS_NODES, $this->cacheId);
}
}
@@ -69,7 +69,7 @@ public function getImplementation($pluginsService = null)
}
self::$cacheInstance = $pluginInstance;
if($pluginInstance !== null && $pluginInstance instanceof AbstractCacheDriver && $pluginInstance->supportsPatternDelete(AJXP_CACHE_SERVICE_NS_NODES)){
- //MetaStreamWrapper::appendMetaWrapper("pydio.cache", "\\Pydio\\Cache\\Core\\CacheStreamLayer");
+ MetaStreamWrapper::appendMetaWrapper("pydio.cache", "\\Pydio\\Cache\\Core\\CacheStreamLayer");
}
}

0 comments on commit cb2166d

Please sign in to comment.