Skip to content

Commit

Permalink
Support custom prefixes for cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed May 14, 2024
1 parent ce2e16a commit 6d8753f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
17 changes: 12 additions & 5 deletions src/DialogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,36 @@
final class DialogRepository
{
private CacheInterface $cache;
private string $prefix;

public function __construct(CacheInterface $cache)
public function __construct(CacheInterface $cache, string $prefix = '')
{
$this->cache = $cache;
$this->prefix = $prefix;
}

public function put(string $key, Dialog $dialog, \DateTime | int $seconds): void
{
$this->cache->set($key, serialize($dialog), $seconds);
$this->cache->set($this->prefixKey($key), serialize($dialog), $seconds);
}

public function has(string $key): bool
{
return $this->cache->has($key);
return $this->cache->has($this->prefixKey($key));
}

public function get(string $key): Dialog
{
return unserialize($this->cache->get($key), ['allowed_classes' => true]);
return unserialize($this->cache->get($this->prefixKey($key)), ['allowed_classes' => true]);
}

public function forget(string $key): bool
{
return $this->cache->delete($key);
return $this->cache->delete($this->prefixKey($key));
}

private function prefixKey(string $key): string
{
return "{$this->prefix}{$key}";
}
}
25 changes: 6 additions & 19 deletions src/Laravel/DialogsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,13 @@ private function offerPublishing(): void

private function registerBindings(): void
{
$this->app->when(DialogRepository::class)
->needs(\Psr\SimpleCache\CacheInterface::class)
->give(function (Container $app): \Illuminate\Contracts\Cache\Repository {
$config = $app->make('config');
$store = $app->make('cache')->store($config->get('telegramdialogs.cache_store'));
assert($store instanceof \Illuminate\Contracts\Cache\Repository);
$this->app->bind(DialogRepository::class, function (Container $app): DialogRepository {
$config = $app->make('config');
$store = $app->make('cache')->store($config->get('telegramdialogs.cache_store'));
assert($store instanceof \Illuminate\Contracts\Cache\Repository);

// @todo Find a way to set a custom cache prefix for the store (default is tg:dialog:). E.g. create DialogRepository class that will have $store as dependency
// $prefix = $config->get('telegramdialogs.cache_prefix');
// if (is_string($prefix) && $prefix !== '' && method_exists($store, 'setPrefix')) {
// $store->setPrefix($prefix);
// }

$tags = $config->get('telegramdialogs.cache_tag');
if ($tags !== '' && $tags !== [] && method_exists($store, 'tags')) {
return $store->tags($config->get('telegramdialogs.cache_tag'));
}

return $store;
});
return new DialogRepository($store, $config->get('telegramdialogs.cache_prefix'));
});

$this->app->alias(DialogManager::class, 'telegram.dialogs');
}
Expand Down
14 changes: 6 additions & 8 deletions src/Laravel/config/telegramdialogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
declare(strict_types=1);

return [
/**
* Stores to store Dialog states.
/*
* Cache store that should be used to store Dialog states between steps/requests.
* This can be the name of any store that is configured in config/cache.php ("stores" key).
*/
'stores' => [
'redis' => [
'connection' => env('TELEGRAM_DIALOGS_REDIS_CONNECTION', 'default'),
],
],
'cache_store' => env('TELEGRAM_DIALOGS_CACHE_DRIVER', 'redis'),

/*
* If the cache driver you configured supports tags, you may specify a tag name here.
* All stored dialogs will be tagged. When clearing, the dialogs only items with that tag will be flushed.
* @see https://laravel.com/docs/9.x/cache#cache-tags
* You may use a string or an array here.
* This prefix will be used in addition to Laravel’s prefix: "$laravelPrefix$telegramPrefix$key"
*/
'cache_tag' => '',
'cache_tag' => env('TELEGRAM_DIALOGS_CACHE_TAG', 'tg_dialog_'),
];
3 changes: 2 additions & 1 deletion tests/DialogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace KootLabs\TelegramBotDialogs\Tests;

use KootLabs\TelegramBotDialogs\DialogManager;
use KootLabs\TelegramBotDialogs\DialogRepository;
use KootLabs\TelegramBotDialogs\Dialogs\HelloExampleDialog;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -23,7 +24,7 @@ private function instantiateDialogManager(): DialogManager
{
return new DialogManager(
new Api('fake-token'),
new Psr16Cache(new ArrayAdapter()), // use array/in-memory store
new DialogRepository(new Psr16Cache(new ArrayAdapter()), 'some_prefix_'), // use array/in-memory store
);
}

Expand Down

0 comments on commit 6d8753f

Please sign in to comment.