Skip to content

Commit

Permalink
[Feature] Persistence clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Apr 7, 2023
1 parent 6a5d882 commit 137b8d1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/DependencyInjection/KreyuDataTableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function prepend(ContainerBuilder $container): void
'pools' => [
'kreyu_data_table.persistence.cache.default' => [
'adapter' => 'cache.adapter.filesystem',
'tags' => true,
],
],
],
Expand Down
25 changes: 23 additions & 2 deletions src/Persistence/CachePersistenceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@
use function Symfony\Component\String\u;

use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

class CachePersistenceAdapter implements PersistenceAdapterInterface
{
public const TAG_PREFIX = 'kreyu_data_table_persistence_';

public function __construct(
private CacheInterface $cache,
private string $prefix,
) {
}

public static function getTagName(PersistenceSubjectInterface $subject): string
{
return self::TAG_PREFIX.$subject->getDataTablePersistenceIdentifier();
}

/**
* @throws InvalidArgumentException
*/
Expand All @@ -27,7 +36,8 @@ public function write(DataTableInterface $dataTable, PersistenceSubjectInterface
$cacheKey = $this->getCacheKey($dataTable, $subject);

$this->cache->delete($cacheKey);
$this->cache->get($cacheKey, fn () => $data);

$this->getCacheValue($cacheKey, $this->getTagName($subject), $data);
}

/**
Expand All @@ -37,7 +47,7 @@ public function read(DataTableInterface $dataTable, PersistenceSubjectInterface
{
$cacheKey = $this->getCacheKey($dataTable, $subject);

return $this->cache->get($cacheKey, fn () => $default);
return $this->getCacheValue($cacheKey, $this->getTagName($subject), $default);
}

private function getCacheKey(DataTableInterface $dataTable, PersistenceSubjectInterface $subject): string
Expand All @@ -50,4 +60,15 @@ private function getCacheKey(DataTableInterface $dataTable, PersistenceSubjectIn

return u(implode('_', array_filter($parts)))->snake()->toString();
}

private function getCacheValue(string $key, string $tag, mixed $default = null): mixed
{
return $this->cache->get($key, function (ItemInterface $item) use ($tag, $default) {
if ($this->cache instanceof TagAwareCacheInterface) {
$item->tag($tag);
}

return $default;
});
}
}
29 changes: 29 additions & 0 deletions src/Persistence/CachePersistenceClearer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Persistence;

use Psr\Cache\InvalidArgumentException;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

class CachePersistenceClearer implements PersistenceClearerInterface
{
public function __construct(
private CacheInterface $cache,
) {
}

/**
* @throws InvalidArgumentException
*/
public function clear(PersistenceSubjectInterface $subject): void
{
if (!$this->cache instanceof TagAwareCacheInterface) {
throw new \LogicException(sprintf('Cache instance must be an instance of %s', TagAwareCacheInterface::class));
}

$this->cache->invalidateTags([CachePersistenceAdapter::getTagName($subject)]);
}
}
10 changes: 10 additions & 0 deletions src/Persistence/PersistenceClearerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Persistence;

interface PersistenceClearerInterface
{
public function clear(PersistenceSubjectInterface $subject): void;
}
9 changes: 9 additions & 0 deletions src/Resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Kreyu\Bundle\DataTableBundle\DataTableRegistry;
use Kreyu\Bundle\DataTableBundle\DataTableRegistryInterface;
use Kreyu\Bundle\DataTableBundle\Maker\MakeDataTable;
use Kreyu\Bundle\DataTableBundle\Persistence\CachePersistenceClearer;
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceClearerInterface;
use Kreyu\Bundle\DataTableBundle\Persistence\StaticPersistenceSubjectProvider;
use Kreyu\Bundle\DataTableBundle\Persistence\TokenStoragePersistenceSubjectProvider;
use Kreyu\Bundle\DataTableBundle\Query\ChainProxyQueryFactory;
Expand Down Expand Up @@ -81,6 +83,13 @@
->tag('kreyu_data_table.proxy_query.factory')
;

$services
->set('kreyu_data_table.persistence.clearer.cache', CachePersistenceClearer::class)
->tag('kreyu_data_table.persistence.clearer')
->args([service('kreyu_data_table.persistence.cache.default')])
->alias(PersistenceClearerInterface::class, 'kreyu_data_table.persistence.clearer.cache')
;

$services
->set('kreyu_data_table.maker', MakeDataTable::class)
->tag('maker.command')
Expand Down

0 comments on commit 137b8d1

Please sign in to comment.