-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft for a new MemoryContentStorageHandler
- Loading branch information
1 parent
d6fd82d
commit d89af45
Showing
1 changed file
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?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 | ||
{ | ||
$data = $this->cache->get($this->cacheId($versionId, $language)); | ||
|
||
// validate the cached data | ||
if ( | ||
isset($data['fields']) === true && | ||
is_array($data['fields']) === true && | ||
isset($data['modified']) === true && | ||
is_int($data['modified']) === true | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 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->get($this->cacheId($versionId, $language))['modified']; | ||
} | ||
|
||
/** | ||
* 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))['fields']; | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Updates the content fields of an existing version | ||
* | ||
* @param array<string, string> $fields Content fields | ||
* | ||
* @throws \Kirby\Exception\NotFoundException If the version does not exist | ||
*/ | ||
public function update(VersionId $versionId, Language $language, array $fields): void | ||
{ | ||
$this->ensure($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' => $fields, | ||
'modified' => time() | ||
]); | ||
} | ||
} |