diff options
author | Robin Appelman <robin@icewind.nl> | 2022-04-04 17:50:23 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 17:50:23 +0300 |
commit | cc5e26bb149b85cc0cf01092623bd74c6dacbca2 (patch) | |
tree | 85fd34cc8ae7170d664bf6c8ad920d64a4abef28 /lib/private/Files/Cache | |
parent | 57ecf55e0f91f28b7b1665bb9765a1f58a285aca (diff) | |
parent | ac657b0f06fea0b24c01eb47b40a28591c0b4528 (diff) |
Merge pull request #31776 from nextcloud/storage-id-cache-bi-directional
cache storage id mapping both ways
Diffstat (limited to 'lib/private/Files/Cache')
-rw-r--r-- | lib/private/Files/Cache/Storage.php | 16 | ||||
-rw-r--r-- | lib/private/Files/Cache/StorageGlobal.php | 32 |
2 files changed, 32 insertions, 16 deletions
diff --git a/lib/private/Files/Cache/Storage.php b/lib/private/Files/Cache/Storage.php index 2de2c2f84d7..e977e29ce7d 100644 --- a/lib/private/Files/Cache/Storage.php +++ b/lib/private/Files/Cache/Storage.php @@ -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; } /** diff --git a/lib/private/Files/Cache/StorageGlobal.php b/lib/private/Files/Cache/StorageGlobal.php index 7162f8e4908..a898c435415 100644 --- a/lib/private/Files/Cache/StorageGlobal.php +++ b/lib/private/Files/Cache/StorageGlobal.php @@ -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() { |