Skip to content

Commit ac657b0

Browse files
committed
cache storage id mapping both ways
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 39494fb commit ac657b0

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

lib/private/Files/Cache/Storage.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,9 @@ public function getNumericId() {
126126
* @param int $numericId
127127
* @return string|null either the storage id string or null if the numeric id is not known
128128
*/
129-
public static function getStorageId($numericId) {
130-
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
131-
$query->select('id')
132-
->from('storages')
133-
->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
134-
$result = $query->execute();
135-
$row = $result->fetch();
136-
$result->closeCursor();
137-
if ($row) {
138-
return $row['id'];
139-
} else {
140-
return null;
141-
}
129+
public static function getStorageId(int $numericId): ?string {
130+
$storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId);
131+
return $storage['id'] ?? null;
142132
}
143133

144134
/**

lib/private/Files/Cache/StorageGlobal.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ class StorageGlobal {
4141
/** @var IDBConnection */
4242
private $connection;
4343

44-
/** @var array[] */
44+
/** @var array<string, array> */
4545
private $cache = [];
46+
/** @var array<int, array> */
47+
private $numericIdCache = [];
4648

4749
public function __construct(IDBConnection $connection) {
4850
$this->connection = $connection;
@@ -68,7 +70,7 @@ public function loadForStorageIds(array $storageIds) {
6870
* @param string $storageId
6971
* @return array|null
7072
*/
71-
public function getStorageInfo($storageId) {
73+
public function getStorageInfo(string $storageId): ?array {
7274
if (!isset($this->cache[$storageId])) {
7375
$builder = $this->connection->getQueryBuilder();
7476
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
@@ -81,9 +83,33 @@ public function getStorageInfo($storageId) {
8183

8284
if ($row) {
8385
$this->cache[$storageId] = $row;
86+
$this->numericIdCache[(int)$row['numeric_id']] = $row;
8487
}
8588
}
86-
return isset($this->cache[$storageId]) ? $this->cache[$storageId] : null;
89+
return $this->cache[$storageId] ?? null;
90+
}
91+
92+
/**
93+
* @param int $numericId
94+
* @return array|null
95+
*/
96+
public function getStorageInfoByNumericId(int $numericId): ?array {
97+
if (!isset($this->numericIdCache[$numericId])) {
98+
$builder = $this->connection->getQueryBuilder();
99+
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
100+
->from('storages')
101+
->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
102+
103+
$result = $query->execute();
104+
$row = $result->fetch();
105+
$result->closeCursor();
106+
107+
if ($row) {
108+
$this->numericIdCache[$numericId] = $row;
109+
$this->cache[$row['id']] = $row;
110+
}
111+
}
112+
return $this->numericIdCache[$numericId] ?? null;
87113
}
88114

89115
public function clearCache() {

0 commit comments

Comments
 (0)