Skip to content

Commit a122d90

Browse files
committed
✨ added automatic unlocking
1 parent 2316ee5 commit a122d90

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ return [
163163
| global | `true` | all HTTP_HOSTs will share the same cache |
164164
| atomic | `true` | will lock the cache while a request is processed to achieve data consistency |
165165
| sleep | `1000` | duration in MICRO seconds before checking the lock again |
166+
| auto-unlock-cache | `true` | will forcibly unlock the cache if it could not get a lock within set time |
166167
| auto-clean-cache | `true` | will clean the cache once before the first get() |
167168
| patch-dir-class | always on | monkey-patch the \Kirby\Filesystem\Dir class to use Nitro for caching |
168169
| patch-files-class | `true` | monkey-patch the \Kirby\CMS\Files class to use Nitro for caching its content |

classes/Nitro/SingleFileCache.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(array $options = [])
2424
'global' => option('bnomei.nitro.global'),
2525
'atomic' => option('bnomei.nitro.atomic'),
2626
'sleep' => option('bnomei.nitro.sleep'),
27+
'auto-unlock-cache' => option('bnomei.nitro.auto-unlock-cache'),
2728
'auto-clean-cache' => option('bnomei.nitro.auto-clean-cache'),
2829
'json-encode-flags' => option('bnomei.nitro.json-encode-flags'),
2930
'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
175176
public function write(bool $lock = true): bool
176177
{
177178
// if is atomic but has no file, don't write
179+
// this might happen if other request force unlocked the cache
178180
if ($this->options['atomic'] && ! F::exists($this->file().'.lock')) {
179181
return false;
180182
}
@@ -271,16 +273,23 @@ private function atomic(): bool
271273
if ($maxExecutionTime === 0) {
272274
$maxExecutionTime = 30; // default, might happen in xdebug mode
273275
}
276+
// leave 5 seconds for script execution
277+
$maxExecutionTime = $maxExecutionTime - 5 > 0 ? $maxExecutionTime - 5 : ceil($maxExecutionTime / 2);
274278
$maxCycles = $maxExecutionTime * 1000 * 1000; // seconds to microseconds
275279
$sleep = $this->options['sleep'];
276280

277281
while ($this->isLocked()) {
282+
usleep($sleep);
278283
$maxCycles -= $sleep;
284+
279285
if ($maxCycles <= 0) {
280-
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`?');
286+
if ($this->options['auto-unlock-cache']) {
287+
$this->unlock();
288+
break;
289+
} else {
290+
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`?');
291+
}
281292
}
282-
283-
usleep($sleep);
284293
}
285294

286295
return $this->lock();

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby-nitro",
33
"type": "kirby-plugin",
4-
"version": "2.0.1",
4+
"version": "2.1.0",
55
"description": "Nitro speeds up the loading of content in your Kirby project.",
66
"license": "MIT",
77
"authors": [

index.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function nitro(): \Bnomei\Nitro
1919
'atomic' => true,
2020
'sleep' => 1_000, // MICROSECONDS with usleep, 1ms
2121
'patch-files-class' => true,
22+
'auto-unlock-cache' => true,
2223
'auto-clean-cache' => true,
2324
'max-dirty-cache' => 512, // write every N changes or on destruct
2425
'json-encode-flags' => JSON_THROW_ON_ERROR, // | JSON_INVALID_UTF8_IGNORE,

vendor/composer/installed.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php return array(
22
'root' => array(
33
'name' => 'bnomei/kirby-nitro',
4-
'pretty_version' => '2.0.1',
5-
'version' => '2.0.1.0',
4+
'pretty_version' => '2.1.0',
5+
'version' => '2.1.0.0',
66
'reference' => null,
77
'type' => 'kirby-plugin',
88
'install_path' => __DIR__ . '/../../',
@@ -11,8 +11,8 @@
1111
),
1212
'versions' => array(
1313
'bnomei/kirby-nitro' => array(
14-
'pretty_version' => '2.0.1',
15-
'version' => '2.0.1.0',
14+
'pretty_version' => '2.1.0',
15+
'version' => '2.1.0.0',
1616
'reference' => null,
1717
'type' => 'kirby-plugin',
1818
'install_path' => __DIR__ . '/../../',

0 commit comments

Comments
 (0)