Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions lib/private/Files/Cache/Wrapper/CacheIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Cache\Wrapper;
use OC\Cache\CappedMemoryCache;

/**
* Jail to a subdirectory of the wrapped cache
*/
class CacheIndex extends CacheWrapper {

/**
* @var CappedMemoryCache
*/
private static $metaDataCache = null;

/**
* @param \OCP\Files\Cache\ICache $cache
* @param string $root
*/
public function __construct($cache) {
parent::__construct($cache);
if (self::$metaDataCache === null) {
self::$metaDataCache = new CappedMemoryCache();
}
}

/**
* get the stored metadata of a file or folder
*
* @param string /int $file
* @return array|false
*/
public function get($file) {
$key = $this->getNumericStorageId().'-get-'.$file;
if (!self::$metaDataCache->hasKey($key)) {
$data = parent::get($file);
self::$metaDataCache->set($key, $data);
}

return self::$metaDataCache->get($key);
}

public function getPathById($id) {
$key = $this->getNumericStorageId().'-getPathById-'.$id;
if (!self::$metaDataCache->hasKey($key)) {
$data = parent::getPathById($id);
self::$metaDataCache->set($key, $data);
// TODO to cache reverse direction normalize path
//$getPathByIdkey = $this->getNumericStorageId().'-getPathById-'.$id;
//self::$metaDataCache->set($getPathByIdkey, $file);
}

return self::$metaDataCache->get($key);
}
public function getId($file) {
$key = $this->getNumericStorageId().'-getId-'.$file;
if (!self::$metaDataCache->hasKey($key)) {
$id = parent::getId($file);
self::$metaDataCache->set($key, $id);
// TODO to cache reverse direction normalize path
//$getPathByIdkey = $this->getNumericStorageId().'-getPathById-'.$id;
//self::$metaDataCache->set($getPathByIdkey, $file);
}

return self::$metaDataCache->get($key);
}

/**
* insert meta data for a new file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
* @throws \RuntimeException
*/
public function insert($file, array $data) {
self::$metaDataCache->clear();
return parent::insert($file, $data);
}

/**
* update the metadata in the cache
*
* @param int $id
* @param array $data
*/
public function update($id, array $data) {
self::$metaDataCache->clear();
parent::update($id, $data);
}

}
46 changes: 46 additions & 0 deletions lib/private/Files/Storage/Wrapper/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\Storage\Wrapper;

use OC\Files\Cache\Wrapper\CacheIndex;

/**
* Availability checker for storages
*
* Throws a StorageNotAvailableException for storages with known failures
*/
class Cache extends Wrapper {

/**
* get a cache instance for the storage
*
* @param string $path
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
* @return \OC\Files\Cache\Cache
*/
public function getCache($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
$sourceCache = $this->storage->getCache($path, $storage);
return new CacheIndex($sourceCache);
}
}
8 changes: 8 additions & 0 deletions lib/private/legacy/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ public static function setupFS($user = '') {
return $storage;
});

// install storage cache wrapper
\OC\Files\Filesystem::addStorageWrapper('oc_cache_index', function ($mountPoint, $storage) {
if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
return new \OC\Files\Storage\Wrapper\Cache(['storage' => $storage]);
}
return $storage;
});

OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user]);
\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true);

Expand Down