Skip to content

Commit

Permalink
cache root cache entry
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Aug 7, 2018
1 parent f8de12c commit 47c2983
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class Cache implements ICache {
/** @var QuerySearchHelper */
protected $querySearchHelper;

/** @var array|null */
private $rootData = null;

/**
* @param \OC\Files\Storage\Storage|string $storage
*/
Expand Down Expand Up @@ -121,6 +124,10 @@ public function getNumericStorageId() {
* @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
*/
public function get($file) {
if ($file === '' && $this->rootData !== null) {
return new CacheEntry($this->rootData);
}

if (is_string($file) or $file == '') {
// normalize file
$file = $this->normalize($file);
Expand Down Expand Up @@ -150,7 +157,11 @@ public function get($file) {
}
return $data;
} else {
return self::cacheEntryFromData($data, $this->mimetypeLoader);
$entry = self::cacheEntryFromData($data, $this->mimetypeLoader);
if ($file === '') {
$this->rootData = $entry->getData();
}
return $entry;
}
}

Expand Down Expand Up @@ -240,6 +251,8 @@ public function put($file, array $data) {
* @throws \RuntimeException
*/
public function insert($file, array $data) {
$this->rootData = null;

// normalize file
$file = $this->normalize($file);

Expand Down Expand Up @@ -292,6 +305,7 @@ public function insert($file, array $data) {
* @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
*/
public function update($id, array $data) {
$this->rootData = null;

if (isset($data['path'])) {
// normalize path
Expand Down Expand Up @@ -382,6 +396,15 @@ protected function buildParts(array $data) {
* @return int
*/
public function getId($file) {
if ($file === '') {
$entry = $this->get($file);
if (!$entry) {
return -1;
} else {
return $entry->getId();
}
}

// normalize file
$file = $this->normalize($file);

Expand Down Expand Up @@ -437,6 +460,7 @@ public function inCache($file) {
* @param string $file
*/
public function remove($file) {
$this->rootData = null;
$entry = $this->get($file);
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
$this->connection->executeQuery($sql, array($entry['fileid']));
Expand Down Expand Up @@ -480,6 +504,7 @@ private function removeChildren($entry) {
* @param string $target
*/
public function move($source, $target) {
$this->rootData = null;
$this->moveFromCache($this, $source, $target);
}

Expand All @@ -504,6 +529,7 @@ protected function getMoveInfo($path) {
* @suppress SqlInjectionChecker
*/
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
$this->rootData = null;
if ($sourceCache instanceof Cache) {
// normalize source and target
$sourcePath = $this->normalize($sourcePath);
Expand Down Expand Up @@ -561,6 +587,7 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
* remove all entries for files that are stored on the storage from the cache
*/
public function clear() {
$this->rootData = null;
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
$this->connection->executeQuery($sql, array($this->getNumericStorageId()));

Expand Down
9 changes: 9 additions & 0 deletions tests/lib/Files/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,15 @@ public function testEscaping($name) {
}
}

public function testMutateRootEntry() {
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder', 'etag' => 'asd');
$this->cache->put('', $data1);
$entry = $this->cache->get('');
$entry['etag'] = 'foobar';
$newEntry = $this->cache->get('');
$this->assertNotEquals('foobar', $newEntry->getEtag());
}

protected function tearDown() {
if ($this->cache) {
$this->cache->clear();
Expand Down

0 comments on commit 47c2983

Please sign in to comment.