From 7f077d673eaacca69e76b314b502c440322da47f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 7 Aug 2018 16:04:09 +0200 Subject: [PATCH] cache root cache entry Signed-off-by: Robin Appelman --- lib/private/Files/Cache/Cache.php | 29 ++++++++++++++++++++++++++++- tests/lib/Files/Cache/CacheTest.php | 9 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 007bccf0a54bb..e308e45d55865 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -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 */ @@ -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); @@ -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; } } @@ -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); @@ -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 @@ -382,6 +396,15 @@ protected function buildParts(array $data) { * @return int */ public function getId($file) { + if ($file === '') { + $entry = $this->get($file); + if (!$entry instanceof ICacheEntry) { + return -1; + } else { + return $entry->getId(); + } + } + // normalize file $file = $this->normalize($file); @@ -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'])); @@ -480,6 +504,7 @@ private function removeChildren($entry) { * @param string $target */ public function move($source, $target) { + $this->rootData = null; $this->moveFromCache($this, $source, $target); } @@ -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); @@ -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())); diff --git a/tests/lib/Files/Cache/CacheTest.php b/tests/lib/Files/Cache/CacheTest.php index bc95a9004f8bf..ce62fd389aac2 100644 --- a/tests/lib/Files/Cache/CacheTest.php +++ b/tests/lib/Files/Cache/CacheTest.php @@ -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();