Skip to content

Commit

Permalink
fix(caching): Avoid checking existence before fetching
Browse files Browse the repository at this point in the history
The cache might expire between checking for key existence and fetching
the value. In this rare case the code continues with a null value when
it doesn't expect one.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst authored and szaimen committed Jun 12, 2023
1 parent c93be18 commit b8c61b3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions apps/files_external/lib/Lib/Storage/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ private function normalizePath(string $path) {
* @throws \OCP\Files\StorageNotAvailableException
*/
private function fetchObject(string $path) {
if ($this->objectCache->hasKey($path)) {
$cached = $this->objectCache->get($path);
if ($cached !== null) {
// might be "false" if object did not exist from last check
return $this->objectCache->get($path);
return $cached;
}
try {
$object = $this->getContainer()->getObject($path);
Expand Down
5 changes: 3 additions & 2 deletions apps/files_sharing/lib/External/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ protected function testRemote(): bool {

private function testRemoteUrl(string $url): bool {
$cache = $this->memcacheFactory->createDistributed('files_sharing_remote_url');
if ($cache->hasKey($url)) {
return (bool)$cache->get($url);
$cached = $cache->get($url);
if ($cached !== null) {
return (bool)$cached;
}

$client = $this->httpClient->newClient();
Expand Down
5 changes: 3 additions & 2 deletions apps/files_sharing/lib/SharedMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ private function verifyMountPoint(
$this->eventDispatcher->dispatchTyped($event);
$parent = $event->getParent();

if ($folderExistCache->hasKey($parent)) {
$parentExists = $folderExistCache->get($parent);
$cached = $folderExistCache->get($parent);
if ($cached) {
$parentExists = $cached;
} else {
$parentExists = $this->recipientView->is_dir($parent);
$folderExistCache->set($parent, $parentExists);
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Accounts/AccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,9 @@ private function parseAccountData(IUser $user, $data): Account {
}

public function getAccount(IUser $user): IAccount {
if ($this->internalCache->hasKey($user->getUID())) {
return $this->internalCache->get($user->getUID());
$cached = $this->internalCache->get($user->getUID());
if ($cached !== null) {
return $cached;
}
$account = $this->parseAccountData($user, $this->getUser($user));
$this->internalCache->set($user->getUID(), $account);
Expand Down

0 comments on commit b8c61b3

Please sign in to comment.