From a122d902c7abffcd19ae23e5d6a1c6ab8e3b1383 Mon Sep 17 00:00:00 2001 From: bnomei Date: Thu, 29 Aug 2024 09:29:22 +0200 Subject: [PATCH] :sparkles: added automatic unlocking --- README.md | 1 + classes/Nitro/SingleFileCache.php | 15 ++++++++++++--- composer.json | 2 +- index.php | 1 + vendor/composer/installed.php | 8 ++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d4e60da..1d3d1ef 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ return [ | global | `true` | all HTTP_HOSTs will share the same cache | | atomic | `true` | will lock the cache while a request is processed to achieve data consistency | | sleep | `1000` | duration in MICRO seconds before checking the lock again | +| auto-unlock-cache | `true` | will forcibly unlock the cache if it could not get a lock within set time | | auto-clean-cache | `true` | will clean the cache once before the first get() | | patch-dir-class | always on | monkey-patch the \Kirby\Filesystem\Dir class to use Nitro for caching | | patch-files-class | `true` | monkey-patch the \Kirby\CMS\Files class to use Nitro for caching its content | diff --git a/classes/Nitro/SingleFileCache.php b/classes/Nitro/SingleFileCache.php index f2c1d8d..a50c2b3 100644 --- a/classes/Nitro/SingleFileCache.php +++ b/classes/Nitro/SingleFileCache.php @@ -24,6 +24,7 @@ public function __construct(array $options = []) 'global' => option('bnomei.nitro.global'), 'atomic' => option('bnomei.nitro.atomic'), 'sleep' => option('bnomei.nitro.sleep'), + 'auto-unlock-cache' => option('bnomei.nitro.auto-unlock-cache'), 'auto-clean-cache' => option('bnomei.nitro.auto-clean-cache'), 'json-encode-flags' => option('bnomei.nitro.json-encode-flags'), 'cacheDir' => realpath(__DIR__.'/../').'/cache', // must be here as well for when used without nitro like as uuid cache @@ -175,6 +176,7 @@ protected function file(?string $key = null): string public function write(bool $lock = true): bool { // if is atomic but has no file, don't write + // this might happen if other request force unlocked the cache if ($this->options['atomic'] && ! F::exists($this->file().'.lock')) { return false; } @@ -271,16 +273,23 @@ private function atomic(): bool if ($maxExecutionTime === 0) { $maxExecutionTime = 30; // default, might happen in xdebug mode } + // leave 5 seconds for script execution + $maxExecutionTime = $maxExecutionTime - 5 > 0 ? $maxExecutionTime - 5 : ceil($maxExecutionTime / 2); $maxCycles = $maxExecutionTime * 1000 * 1000; // seconds to microseconds $sleep = $this->options['sleep']; while ($this->isLocked()) { + usleep($sleep); $maxCycles -= $sleep; + if ($maxCycles <= 0) { - throw new \Exception('Something is very wrong. SingleFileCache could not get lock within '.$maxExecutionTime.' seconds! Are using xdebug breakpoints or maybe you need to forcibly `kirby nitro:unlock`?'); + if ($this->options['auto-unlock-cache']) { + $this->unlock(); + break; + } else { + throw new \Exception('Something is very wrong. SingleFileCache could not get lock within ' . $maxExecutionTime . ' seconds! Are using xdebug breakpoints or maybe you need to forcibly `kirby nitro:unlock`?'); + } } - - usleep($sleep); } return $this->lock(); diff --git a/composer.json b/composer.json index 7824d49..1c565ea 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bnomei/kirby-nitro", "type": "kirby-plugin", - "version": "2.0.1", + "version": "2.1.0", "description": "Nitro speeds up the loading of content in your Kirby project.", "license": "MIT", "authors": [ diff --git a/index.php b/index.php index 867d327..6573b29 100644 --- a/index.php +++ b/index.php @@ -19,6 +19,7 @@ function nitro(): \Bnomei\Nitro 'atomic' => true, 'sleep' => 1_000, // MICROSECONDS with usleep, 1ms 'patch-files-class' => true, + 'auto-unlock-cache' => true, 'auto-clean-cache' => true, 'max-dirty-cache' => 512, // write every N changes or on destruct 'json-encode-flags' => JSON_THROW_ON_ERROR, // | JSON_INVALID_UTF8_IGNORE, diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index f05ced9..fbc2a94 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,8 +1,8 @@ array( 'name' => 'bnomei/kirby-nitro', - 'pretty_version' => '2.0.1', - 'version' => '2.0.1.0', + 'pretty_version' => '2.1.0', + 'version' => '2.1.0.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -11,8 +11,8 @@ ), 'versions' => array( 'bnomei/kirby-nitro' => array( - 'pretty_version' => '2.0.1', - 'version' => '2.0.1.0', + 'pretty_version' => '2.1.0', + 'version' => '2.1.0.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',