Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Expand Doctrine caches implementation to add "deleteKeyStartingBy" wh…
Browse files Browse the repository at this point in the history
…en possible (currently only APC and Redis). If this feature is supported, activate the nodes cache layer (otherwise we cannot prune a full branch of the filesystem tree).
  • Loading branch information
cdujeu committed Apr 26, 2016
1 parent ca2212b commit 4c6869a
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 36 deletions.
9 changes: 6 additions & 3 deletions core/src/plugins/cache.doctrine/class.DoctrineCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
define('XCACHE_EXTENSION_LOADED', extension_loaded('xcache'));

use \Doctrine\Common\Cache;
use \Pydio\Plugins\Cache\Doctrine\Ext;

/**
* Standard Memcache driver
Expand All @@ -38,7 +39,7 @@
*/
class doctrineCacheDriver extends AbstractCacheDriver
{

/**
* @param string $namespace
* @return Cache\CacheProvider
Expand Down Expand Up @@ -148,7 +149,8 @@ private function initCacheWithNamespace($namespace){

public function _apc_init($options) {
if (extension_loaded('apcu')) {
$cacheDriver = new Cache\ApcuCache();
require_once ("ext/PydioApcuCache.php");
$cacheDriver = new Ext\PydioApcuCache();
} else {
$cacheDriver = new Cache\ApcCache();
}
Expand Down Expand Up @@ -183,7 +185,8 @@ public function _redis_init($options) {

if (! $running) return null;

$cacheDriver = new \Doctrine\Common\Cache\RedisCache();
require_once "ext/PydioRedisCache.php";
$cacheDriver = new Ext\PydioRedisCache();
$cacheDriver->setRedis($redis);
return $cacheDriver;
}
Expand Down
39 changes: 39 additions & 0 deletions core/src/plugins/cache.doctrine/ext/PatternClearableCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Plugins\Cache\Doctrine\Ext;

defined('AJXP_EXEC') or die('Access not allowed');

interface PatternClearableCache{

/**
* @param string $pattern
* @return bool
*/
public function deleteKeysStartingWith($pattern);

/**
* @param string $namespace
* @return void
*/
public function setNamespace($namespace);

}
66 changes: 66 additions & 0 deletions core/src/plugins/cache.doctrine/ext/PydioApcuCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Plugins\Cache\Doctrine\Ext;

defined('AJXP_EXEC') or die('Access not allowed');
require_once "PatternClearableCache.php";

class PydioApcuCache extends \Doctrine\Common\Cache\ApcuCache implements PatternClearableCache
{
protected $internalNamespace;
protected $internalNamespaceVersion;

/**
* Prefixes the passed id with the configured namespace value.
*
* @param string $id The id to namespace.
*
* @return string The namespaced id.
*/
private function getNamespacedId($id)
{
return sprintf('%s\['.preg_quote($id, "/"), $this->internalNamespace);
}

/**
* @param string $pattern
* @return bool
*/
public function deleteKeysStartingWith($pattern)
{
$pattern = '/^'.$this->getNamespacedId($pattern).'/';
//SAMPLE /^pydio-unique-id_nodes_\[list\:\/\/1/
$iterator = new APCIterator('user', $pattern);
foreach ($iterator as $data) {
$res = $this->doDelete($data['key']);
}
}

/**
* @param string $namespace
* @return void
*/
public function setNamespace($namespace)
{
parent::setNamespace($namespace);
$this->internalNamespace = $namespace;
}
}
83 changes: 83 additions & 0 deletions core/src/plugins/cache.doctrine/ext/PydioRedisCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Plugins\Cache\Doctrine\Ext;
defined('AJXP_EXEC') or die('Access not allowed');

require_once "PatternClearableCache.php";
use Redis;


class PydioRedisCache extends \Doctrine\Common\Cache\RedisCache implements PatternClearableCache
{
/**
* @var Redis
*/
protected $internalRedis;
protected $internalNamespace;
protected $internalNamespaceVersion;


public function setRedis($redis){
parent::setRedis($redis);
$this->internalRedis = $redis;
}

/**
* Prefixes the passed id with the configured namespace value.
*
* @param string $id The id to namespace.
*
* @return string The namespaced id.
*/
private function getNamespacedId($id)
{
// Escape redis MATCH special characters
$id = str_replace(array("?", "*", "[", "]", "^", "-"), array("\?", "\*", "\[", "\]", "\^", "\-"), $id);
return sprintf('%s\['.$id.'*', $this->internalNamespace);
}


/**
* @param string $pattern
* @return bool
*/
public function deleteKeysStartingWith($pattern)
{
$pattern = $this->getNamespacedId($pattern);
$it = NULL; /* Initialize our iterator to NULL */
$this->internalRedis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* retry when we get no keys back */
while($arr_keys = $this->internalRedis->scan($it, $pattern)) {
foreach($arr_keys as $str_key) {
$this->doDelete($str_key);
}
}
}

/**
* @param string $namespace
* @return void
*/
public function setNamespace($namespace)
{
parent::setNamespace($namespace);
$this->internalNamespace = $namespace;
}
}
44 changes: 44 additions & 0 deletions core/src/plugins/core.cache/class.AbstractCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,50 @@ public function init($options)
*/
abstract public function getCacheDriver($namespace=AJXP_CACHE_SERVICE_NS_SHARED);

/**
* @param AJXP_Node $node
* @param string $cacheType
* @param string $details
* @return string
*/
public static function computeIdForNode($node, $cacheType, $details = ''){
$repo = $node->getRepository();
if($repo == null) return "failed-id";
$scope = $repo->securityScope();
$additional = "";
if($scope === "USER"){
$additional = AuthService::getLoggedUser()->getId()."@";
}else if($scope == "GROUP"){
$additional = ltrim(str_replace("/", "__", AuthService::getLoggedUser()->getGroupPath()), "__")."@";
}
$scheme = parse_url($node->getUrl(), PHP_URL_SCHEME);
return str_replace($scheme . "://", $cacheType."://".$additional, $node->getUrl()).($details?"##".$details:"");
}

/**
* @param string $namespace
* @return bool
*/
public function supportsPatternDelete($namespace)
{
$cacheDriver = $this->getCacheDriver($namespace);
return $cacheDriver instanceof Pydio\Plugins\Cache\Doctrine\Ext\PatternClearableCache;
//return is_a($cacheDriver, "PatternClearableCache");
}

/**
* @param string $namespace
* @param string $id
* @return bool
*/
public function deleteKeyStartingWith($namespace, $id){
$cacheDriver = $this->getCacheDriver($namespace);
if(!($cacheDriver instanceof Pydio\Plugins\Cache\Doctrine\Ext\PatternClearableCache)){
return false;
}
return $cacheDriver->deleteKeysStartingWith($id);
}

/**
* Fetches an entry from the cache.
*
Expand Down
14 changes: 2 additions & 12 deletions core/src/plugins/core.cache/class.CacheStreamLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,8 @@ public static function clearDirCache($path){

protected function computeCacheId($path, $type){

$node = new AJXP_Node($path);
$repo = $node->getRepository();
if($repo == null) return "failed-id";
$scope = $repo->securityScope();
$additional = "";
if($scope === "USER"){
$additional = AuthService::getLoggedUser()->getId()."@";
}else if($scope == "GROUP"){
$additional = ltrim(str_replace("/", "__", AuthService::getLoggedUser()->getGroupPath()), "__")."@";
}
return str_replace("pydio.cache://", $type."://".$additional, $path);

return AbstractCacheDriver::computeIdForNode(new AJXP_Node($path), $type);

}

// Keep listing in cache
Expand Down
Loading

0 comments on commit 4c6869a

Please sign in to comment.