Skip to content

Commit f1c4223

Browse files
committed
chore: Remove windows-specific code and use faster hash
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 54c8481 commit f1c4223

1 file changed

Lines changed: 10 additions & 27 deletions

File tree

lib/private/PhpDumpCache.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
/**
66
* SPDX-FileCopyrightText: 2004 David Grudl (https://davidgrudl.com)
7-
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
88
* SPDX-License-Identifier: BSD-3-Clause
99
*/
1010

1111
namespace OC;
1212

1313
// TODO: cleanup? TTL?
14+
1415
class PhpDumpCache {
1516

1617
public function __construct(
@@ -36,38 +37,24 @@ public function setTempDirectory(string $dir): static {
3637
public function loadCache(array $cacheKey): ?array {
3738
$file = $this->generateCacheFileName($cacheKey);
3839

39-
// Solving atomicity to work everywhere
40-
// 1) We want to do as little as possible IO calls on production and also directory and file can be not writable (#19)
41-
// so on Linux we include the file directly without shared lock, therefore, the file must be created atomically by renaming.
42-
// 2) On Windows file cannot be renamed-to while is open (ie by include() #11), so we have to acquire a lock.
43-
$lock = defined('PHP_WINDOWS_VERSION_BUILD')
44-
? $this->acquireLock("$file.lock", LOCK_SH)
45-
: null;
46-
47-
try {
48-
$data = @include $file; // @ file may not exist
49-
if (is_array($data)) {
50-
return $data;
51-
}
52-
53-
return null;
54-
} finally {
55-
if ($lock) {
56-
flock($lock, LOCK_UN); // release shared lock
57-
}
40+
$data = @include $file; // @ file may not exist
41+
if (is_array($data)) {
42+
return $data;
5843
}
44+
45+
return null;
5946
}
6047

6148
/**
6249
* Writes class list to cache.
6350
* @param ?resource $lock
6451
*/
65-
public function saveCache(array $cacheKey, array $data, $lock = null): void {
52+
public function saveCache(array $cacheKey, array $data): void {
6653
// we have to acquire a lock to be able safely rename file
6754
// on Linux: that another thread does not rename the same named file earlier
6855
// on Windows: that the file is not read by another thread
6956
$file = $this->generateCacheFileName($cacheKey);
70-
$lock = $lock ?: $this->acquireLock("$file.lock", LOCK_EX);
57+
$lock = $this->acquireLock("$file.lock", LOCK_EX);
7158
$code = "<?php\nreturn " . var_export($data, true) . ";\n";
7259

7360
if (file_put_contents("$file.tmp", $code) !== strlen($code) || !rename("$file.tmp", $file)) {
@@ -103,10 +90,6 @@ private function acquireLock(string $file, int $mode) {
10390
}
10491

10592
private function generateCacheFileName(array $cacheKey): string {
106-
if (!$this->tempDirectory) {
107-
throw new \LogicException('Set path to temporary directory using setTempDirectory().');
108-
}
109-
110-
return $this->tempDirectory . '/' . md5(serialize($cacheKey)) . '.php';
93+
return $this->tempDirectory . '/' . hash('xxh3', serialize($cacheKey)) . '.php';
11194
}
11295
}

0 commit comments

Comments
 (0)