]> source.dussan.org Git - nextcloud-server.git/commitdiff
cache storage id mapping both ways 31776/head
authorRobin Appelman <robin@icewind.nl>
Thu, 31 Mar 2022 12:10:20 +0000 (14:10 +0200)
committerRobin Appelman <robin@icewind.nl>
Thu, 31 Mar 2022 12:35:42 +0000 (14:35 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/Files/Cache/Storage.php
lib/private/Files/Cache/StorageGlobal.php

index 2de2c2f84d711fc8b9d8bfa8ee9fbc7d04b902b3..e977e29ce7d88ad7485fed9ed3701467bf49824c 100644 (file)
@@ -126,19 +126,9 @@ class Storage {
         * @param int $numericId
         * @return string|null either the storage id string or null if the numeric id is not known
         */
-       public static function getStorageId($numericId) {
-               $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
-               $query->select('id')
-                       ->from('storages')
-                       ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
-               $result = $query->execute();
-               $row = $result->fetch();
-               $result->closeCursor();
-               if ($row) {
-                       return $row['id'];
-               } else {
-                       return null;
-               }
+       public static function getStorageId(int $numericId): ?string {
+               $storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId);
+               return $storage['id'] ?? null;
        }
 
        /**
index 7162f8e49082a97d7bb68e7a312222f7fae16660..a898c4354157eee0c288e9c2d814aa26062a00ad 100644 (file)
@@ -41,8 +41,10 @@ class StorageGlobal {
        /** @var IDBConnection */
        private $connection;
 
-       /** @var array[] */
+       /** @var array<string, array> */
        private $cache = [];
+       /** @var array<int, array> */
+       private $numericIdCache = [];
 
        public function __construct(IDBConnection $connection) {
                $this->connection = $connection;
@@ -68,7 +70,7 @@ class StorageGlobal {
         * @param string $storageId
         * @return array|null
         */
-       public function getStorageInfo($storageId) {
+       public function getStorageInfo(string $storageId): ?array {
                if (!isset($this->cache[$storageId])) {
                        $builder = $this->connection->getQueryBuilder();
                        $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
@@ -81,9 +83,33 @@ class StorageGlobal {
 
                        if ($row) {
                                $this->cache[$storageId] = $row;
+                               $this->numericIdCache[(int)$row['numeric_id']] = $row;
                        }
                }
-               return isset($this->cache[$storageId]) ? $this->cache[$storageId] : null;
+               return $this->cache[$storageId] ?? null;
+       }
+
+       /**
+        * @param int $numericId
+        * @return array|null
+        */
+       public function getStorageInfoByNumericId(int $numericId): ?array {
+               if (!isset($this->numericIdCache[$numericId])) {
+                       $builder = $this->connection->getQueryBuilder();
+                       $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
+                               ->from('storages')
+                               ->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
+
+                       $result = $query->execute();
+                       $row = $result->fetch();
+                       $result->closeCursor();
+
+                       if ($row) {
+                               $this->numericIdCache[$numericId] = $row;
+                               $this->cache[$row['id']] = $row;
+                       }
+               }
+               return $this->numericIdCache[$numericId] ?? null;
        }
 
        public function clearCache() {