Permalink
Browse files

Fix Cache

  • Loading branch information...
1 parent f1f0fa1 commit 685e46caf10042afbf5aa1bf25df7c81abf946a9 @cdujeu cdujeu committed Sep 8, 2016
@@ -163,7 +163,7 @@ private function initCacheWithNamespace($namespace){
// Using chained array for nodes
if ($namespace == AJXP_CACHE_SERVICE_NS_NODES) {
- $arrayCache = new ArrayCache();
+ $arrayCache = new Ext\PydioArrayCache();
$cacheDriver = new PydioChainCache([
$arrayCache,
@@ -0,0 +1,152 @@
+<?php
+/*
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Pydio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <http://pyd.io/>.
+ */
+namespace Pydio\Cache\Doctrine\Ext;
+use Doctrine\Common\Cache\Cache;
+use Doctrine\Common\Cache\CacheProvider;
+
+/**
+ * Array cache driver with ability to clear keys
+ */
+class PydioArrayCache extends CacheProvider implements PatternClearableCache
+{
+ /**
+ * @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool
+ */
+ private $data = [];
+
+ /**
+ * @var int
+ */
+ private $hitsCount = 0;
+
+ /**
+ * @var int
+ */
+ private $missesCount = 0;
+
+ /**
+ * @var int
+ */
+ private $upTime;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct()
+ {
+ $this->upTime = time();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doFetch($id)
+ {
+ if (! $this->doContains($id)) {
+ $this->missesCount += 1;
+
+ return false;
+ }
+
+ $this->hitsCount += 1;
+
+ return $this->data[$id][0];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doContains($id)
+ {
+ if (! isset($this->data[$id])) {
+ return false;
+ }
+
+ $expiration = $this->data[$id][1];
+
+ if ($expiration && $expiration < time()) {
+ $this->doDelete($id);
+
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doSave($id, $data, $lifeTime = 0)
+ {
+ $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
+
+ return true;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doDelete($id)
+ {
+ unset($this->data[$id]);
+
+ return true;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doFlush()
+ {
+ $this->data = [];
+
+ return true;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doGetStats()
+ {
+ return [
+ Cache::STATS_HITS => $this->hitsCount,
+ Cache::STATS_MISSES => $this->missesCount,
+ Cache::STATS_UPTIME => $this->upTime,
+ Cache::STATS_MEMORY_USAGE => null,
+ Cache::STATS_MEMORY_AVAILABLE => null,
+ ];
+ }
+
+ /**
+ * @param string $pattern
+ * @return bool
+ */
+ public function deleteKeysStartingWith($pattern) {
+
+ $pat = "[" . $pattern;
+ foreach($this->data as $key => $value){
+ if(substr($key, 0, strlen($pat)) === $pat){
+ unset($this->data[$key]);
+ }
+ }
+
+ }
+}
@@ -1,26 +1,46 @@
<?php
-/**
- * Created by PhpStorm.
- * User: ghecquet
- * Date: 29/06/16
- * Time: 10:07
+/*
+ * Copyright 2007-2015 Abstrium <contact (at) pydio.com>
+ * This file is part of Pydio.
+ *
+ * Pydio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Pydio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The latest code can be found at <http://pyd.io/>.
*/
-
namespace Pydio\Cache\Doctrine\Ext;
+use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\ChainCache;
+/**
+ * Class PydioChainCache
+ * @package Pydio\Cache\Doctrine\Ext
+ */
class PydioChainCache extends ChainCache implements PatternClearableCache {
/**
- * @var Redis
+ * @var CacheProvider[]
*/
protected $cacheProviders;
protected $internalNamespace;
protected $internalNamespaceVersion;
-
+ /**
+ * PydioChainCache constructor.
+ * @param array|\Doctrine\Common\Cache\CacheProvider[] $cacheProviders
+ */
public function __construct($cacheProviders)
{
parent::__construct($cacheProviders);
@@ -33,22 +53,25 @@ public function __construct($cacheProviders)
* @return bool
*/
public function deleteKeysStartingWith($pattern) {
+ $result = false;
foreach ($this->cacheProviders as $cache) {
if(!($cache instanceof PatternClearableCache)) {
- break;
+ continue;
}
/** @var PatternClearableCache $cache */
- $pattern = $cache->deleteKeysStartingWith($pattern);
+ $result &= $cache->deleteKeysStartingWith($pattern);
}
+ return $result;
}
/**
* @param string $namespace
* @return void
*/
public function setNamespace($namespace) {
+ parent::setNamespace($namespace);
foreach ($this->cacheProviders as $cache) {
$cache->setNamespace($namespace);
}
@@ -157,7 +157,6 @@ public function contains($namespace, $id){
}
$result = $cacheDriver->contains($id);
-
return $result;
}
@@ -178,6 +177,7 @@ public function save($namespace, $id, $data, $lifeTime = 0){
return false;
}
+ error_log("Saving ".$id);
$result = $cacheDriver->save($id, $data, $lifeTime);
return $result;
@@ -192,6 +192,7 @@ public function save($namespace, $id, $data, $lifeTime = 0){
*/
public function delete($namespace, $id){
+ error_log("Clearing ".$namespace." - ".$id);
$cacheDriver = $this->getCacheDriver($namespace);
if($this->requiresHttpForwarding($cacheDriver)){
$this->httpDeletion[$namespace.$id.'key'] = ["namespace"=>$namespace, "key" => $id];
@@ -258,6 +259,7 @@ public function deleteKeyStartingWith($namespace, $id){
return false;
}
Logger::debug("CacheDriver::Http", "Clear Pattern ".$id, ["namespace" => $namespace]);
+ error_log("Clear pattern ".$id);
return $cacheDriver->deleteKeysStartingWith($id);
}
@@ -182,7 +182,7 @@ protected function clearCacheForNode($node){
$cacheDriver->delete(AJXP_CACHE_SERVICE_NS_NODES, AbstractCacheDriver::getOptionsForNode($node, "stat")["id"]);
// Clear parent listing
if($node->getParent() !== null){
- $cacheDriver->delete(AJXP_CACHE_SERVICE_NS_NODES, AbstractCacheDriver::getOptionsForNode($node->getParent(), "list"))["id"];
+ $cacheDriver->delete(AJXP_CACHE_SERVICE_NS_NODES, AbstractCacheDriver::getOptionsForNode($node->getParent(), "list")["id"]);
}
}else {
// Delete node data and all its children

0 comments on commit 685e46c

Please sign in to comment.