Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache storage id mapping in memcache #31905

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 7 additions & 1 deletion lib/private/Files/Cache/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Storage\IStorage;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

Expand All @@ -56,7 +57,10 @@
*/
public static function getGlobalCache() {
if (is_null(self::$globalCache)) {
self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
self::$globalCache = new StorageGlobal(
\OC::$server->getDatabaseConnection(),
\OC::$server->get(ICacheFactory::class),
);
}
return self::$globalCache;
}
Expand Down Expand Up @@ -180,6 +184,7 @@
->set('last_checked', $query->createNamedParameter(time() + $delay))
->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
$query->execute();
self::getGlobalCache()->removeFromMemcache($this->numericId, $this->storageId);
}

/**
Expand All @@ -200,13 +205,14 @@
public static function remove($storageId) {
$storageId = self::adjustStorageId($storageId);
$numericId = self::getNumericStorageId($storageId);
self::getGlobalCache()->removeFromMemcache($numericId, $storageId);

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('storages')
->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
$query->execute();

if (!is_null($numericId)) {

Check failure on line 215 in lib/private/Files/Cache/Storage.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

lib/private/Files/Cache/Storage.php:215:8: RedundantCondition: Type int for $numericId is never null (see https://psalm.dev/122)
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
Expand Down
83 changes: 56 additions & 27 deletions lib/private/Files/Cache/StorageGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
namespace OC\Files\Cache;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IDBConnection;

/**
Expand All @@ -38,16 +40,17 @@
* @package OC\Files\Cache
*/
class StorageGlobal {
/** @var IDBConnection */
private $connection;
private IDBConnection $connection;
private ICache $memCache;

/** @var array<string, array> */
private $cache = [];
/** @var array<int, array> */
private $numericIdCache = [];

public function __construct(IDBConnection $connection) {
public function __construct(IDBConnection $connection, ICacheFactory $cacheFactory) {
$this->connection = $connection;
$this->memCache = $cacheFactory->createLocal("storage_id::");
}

/**
Expand All @@ -72,19 +75,13 @@ public function loadForStorageIds(array $storageIds) {
*/
public function getStorageInfo(string $storageId): ?array {
if (!isset($this->cache[$storageId])) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
->from('storages')
->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));

$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();

if ($row) {
$this->cache[$storageId] = $row;
$this->numericIdCache[(int)$row['numeric_id']] = $row;
$data = $this->memCache->get("id::$storageId");
if (!$data) {
$data = $this->getByIdFromDb($storageId);
$this->memCache->set("id::$storageId", $data);
}
$this->cache[$storageId] = $data;
$this->numericIdCache[(int)$data['numeric_id']] = $data;
}
return $this->cache[$storageId] ?? null;
}
Expand All @@ -95,24 +92,56 @@ public function getStorageInfo(string $storageId): ?array {
*/
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;
$data = $this->memCache->get("numeric::$numericId");
if (!$data) {
$data = $this->getByNumericFromDb($numericId);
$this->memCache->set("numeric::$numericId", $data);
}
$this->numericIdCache[$numericId] = $data;
$this->cache[$data['id']] = $data;
}
return $this->numericIdCache[$numericId] ?? null;
}

private function getByIdFromDb(string $storageId): ?array {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
->from('storages')
->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));

$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
return $row;
} else {
return null;
}
}

private function getByNumericFromDb(int $numericId): ?array {
$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) {
return $row;
} else {
return null;
}
}

public function removeFromMemcache(int $numericId, string $id) {
$this->memCache->remove("id::$id");
$this->memCache->remove("numeric::$numericId");
}

public function clearCache() {
$this->cache = [];
$this->memCache->clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be something to check where this is being called, clearing redis with a prefix might be a bit slow due to the keys command having a O(n) runtime.

}
}
Loading