-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6457 from getkirby/v5/changes/memory-storage-handler
- Loading branch information
Showing
11 changed files
with
730 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Kirby\Content; | ||
|
||
use Kirby\Cache\MemoryCache; | ||
use Kirby\Cms\Language; | ||
use Kirby\Cms\ModelWithContent; | ||
|
||
/** | ||
* @package Kirby Content | ||
* @author Bastian Allgeier <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
*/ | ||
class MemoryContentStorageHandler extends ContentStorageHandler | ||
{ | ||
/** | ||
* Cache instance, used to store content in memory | ||
*/ | ||
protected MemoryCache $cache; | ||
|
||
/** | ||
* Sets up the cache instance | ||
*/ | ||
public function __construct(protected ModelWithContent $model) | ||
{ | ||
parent::__construct($model); | ||
$this->cache = new MemoryCache(); | ||
} | ||
|
||
/** | ||
* Returns a unique id for a combination | ||
* of the version id, the language code and the model id | ||
*/ | ||
protected function cacheId(VersionId $versionId, Language $language): string | ||
{ | ||
return $versionId->value() . '/' . $language->code() . '/' . $this->model->id(); | ||
} | ||
|
||
/** | ||
* Deletes an existing version in an idempotent way if it was already deleted | ||
*/ | ||
public function delete(VersionId $versionId, Language $language): void | ||
{ | ||
$this->cache->remove($this->cacheId($versionId, $language)); | ||
} | ||
|
||
/** | ||
* Checks if a version exists | ||
*/ | ||
public function exists(VersionId $versionId, Language $language): bool | ||
{ | ||
return $this->cache->exists($this->cacheId($versionId, $language)); | ||
} | ||
|
||
/** | ||
* Returns the modification timestamp of a version if it exists | ||
*/ | ||
public function modified(VersionId $versionId, Language $language): int|null | ||
{ | ||
if ($this->exists($versionId, $language) === false) { | ||
return null; | ||
} | ||
|
||
return $this->cache->modified($this->cacheId($versionId, $language)); | ||
} | ||
|
||
/** | ||
* Returns the stored content fields | ||
* | ||
* @return array<string, string> | ||
* | ||
* @throws \Kirby\Exception\NotFoundException If the version does not exist | ||
*/ | ||
public function read(VersionId $versionId, Language $language): array | ||
{ | ||
$this->ensure($versionId, $language); | ||
return $this->cache->get($this->cacheId($versionId, $language)); | ||
} | ||
|
||
/** | ||
* Updates the modification timestamp of an existing version | ||
* | ||
* @throws \Kirby\Exception\NotFoundException If the version does not exist | ||
*/ | ||
public function touch(VersionId $versionId, Language $language): void | ||
{ | ||
$fields = $this->read($versionId, $language); | ||
$this->write($versionId, $language, $fields); | ||
} | ||
|
||
/** | ||
* Writes the content fields of an existing version | ||
* | ||
* @param array<string, string> $fields Content fields | ||
*/ | ||
protected function write(VersionId $versionId, Language $language, array $fields): void | ||
{ | ||
$this->cache->set($this->cacheId($versionId, $language), $fields); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.