Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions lib/private/Files/Cache/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,9 @@ public function getNumericId() {
* @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;
}

/**
Expand Down
32 changes: 29 additions & 3 deletions lib/private/Files/Cache/StorageGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -68,7 +70,7 @@ public function loadForStorageIds(array $storageIds) {
* @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'])
Expand All @@ -81,9 +83,33 @@ public function getStorageInfo($storageId) {

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() {
Expand Down