Skip to content

Commit

Permalink
✨ added automatic unlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Aug 29, 2024
1 parent 2316ee5 commit a122d90
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
15 changes: 12 additions & 3 deletions classes/Nitro/SingleFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => 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__ . '/../../',
Expand All @@ -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__ . '/../../',
Expand Down

0 comments on commit a122d90

Please sign in to comment.