Skip to content

Commit

Permalink
First draft for a new MemoryContentStorageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed May 22, 2024
1 parent fa6b580 commit 9bbf067
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions src/Content/MemoryContentStorageHandler.php
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()
]);
}
}

0 comments on commit 9bbf067

Please sign in to comment.