From 67d5380b91a1c970a62d04dc82e1ccae10d0d862 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Sun, 30 Sep 2018 21:05:53 +0200 Subject: Reuse information from ListObjects for stat / filetype Signed-off-by: Daniel Kesselberg --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'apps/files_external') diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index 097b196b2d4..94ada029809 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -301,6 +301,11 @@ class AmazonS3 extends \OC\Files\Storage\Common { isset($object['Key']) ? $object['Key'] : $object['Prefix'] ); $files[] = $file; + + $this->objectCache->set($file, [ + 'ContentLength' => $object['Size'], + 'LastModified' => (string)$object['LastModified'], + ]); } } } -- cgit v1.2.3 From 5cf38254bbf8d689a3205a098405e9e9120ec01d Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 1 Oct 2018 20:18:36 +0200 Subject: Use helper method for content length and last modified Signed-off-by: Daniel Kesselberg --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 65 ++++++++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) (limited to 'apps/files_external') diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index 94ada029809..c1b68abf7fb 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -61,10 +61,14 @@ class AmazonS3 extends \OC\Files\Storage\Common { /** @var CappedMemoryCache|Result[] */ private $objectCache; + /** @var CappedMemoryCache|array */ + private $filesCache; + public function __construct($parameters) { parent::__construct($parameters); $this->parseParams($parameters); $this->objectCache = new CappedMemoryCache(); + $this->filesCache = new CappedMemoryCache(); } /** @@ -302,10 +306,11 @@ class AmazonS3 extends \OC\Files\Storage\Common { ); $files[] = $file; - $this->objectCache->set($file, [ + // store this information for later usage + $this->filesCache[$file] = [ 'ContentLength' => $object['Size'], 'LastModified' => (string)$object['LastModified'], - ]); + ]; } } } @@ -321,20 +326,14 @@ class AmazonS3 extends \OC\Files\Storage\Common { $path = $this->normalizePath($path); try { - $stat = array(); + $stat = []; if ($this->is_dir($path)) { //folders don't really exist $stat['size'] = -1; //unknown $stat['mtime'] = time() - $this->rescanDelay * 1000; } else { - $result = $this->headObject($path); - - $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; - if (isset($result['Metadata']['lastmodified'])) { - $stat['mtime'] = strtotime($result['Metadata']['lastmodified']); - } else { - $stat['mtime'] = strtotime($result['LastModified']); - } + $stat['size'] = $this->getContentLength($path); + $stat['mtime'] = strtotime($this->getLastModified($path)); } $stat['atime'] = time(); @@ -345,6 +344,50 @@ class AmazonS3 extends \OC\Files\Storage\Common { } } + /** + * Return content length for object + * + * When the information is already present (e.g. opendir has been called before) + * this value is return. Otherwise a headObject is emitted. + * + * @param $path + * @return int|mixed + */ + private function getContentLength($path) { + if (isset($this->filesCache[$path])) { + return $this->filesCache[$path]['ContentLength']; + } + + $result = $this->headObject($path); + if (isset($result['ContentLength'])) { + return $result['ContentLength']; + } + + return 0; + } + + /** + * Return last modified for object + * + * When the information is already present (e.g. opendir has been called before) + * this value is return. Otherwise a headObject is emitted. + * + * @param $path + * @return mixed|string + */ + private function getLastModified($path) { + if (isset($this->filesCache[$path])) { + return $this->filesCache[$path]['LastModified']; + } + + $result = $this->headObject($path); + if (isset($result['LastModified'])) { + return $result['LastModified']; + } + + return 'now'; + } + public function is_dir($path) { $path = $this->normalizePath($path); try { -- cgit v1.2.3 From 4cbea5f7e123136f081dbe56da7f37cf9b9e41c4 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 8 Oct 2018 16:40:39 +0200 Subject: Remove value from cache on delete/clear Signed-off-by: Daniel Kesselberg --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'apps/files_external') diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index c1b68abf7fb..9d2e0c91099 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -98,6 +98,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { private function clearCache() { $this->objectCache = new CappedMemoryCache(); + $this->filesCache = new CappedMemoryCache(); } private function invalidateCache($key) { @@ -109,6 +110,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { unset($this->objectCache[$existingKey]); } } + unset($this->filesCache[$key]); } /** -- cgit v1.2.3