Skip to content

Commit

Permalink
Merge pull request #22 from koot-labs/laravel-cache
Browse files Browse the repository at this point in the history
Use Laravel Cache and PSR-16 (Remove custom Store interface and implementations)
  • Loading branch information
lptn authored May 14, 2024
2 parents 05add3c + 6d8753f commit a9bf07c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 282 deletions.
8 changes: 0 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@
<code><![CDATA[config_path('telegramdialogs.php')]]></code>
</UndefinedFunction>
</file>
<file src="src/Laravel/Stores/RedisStoreAdapter.php">
<UndefinedInterfaceMethod>
<code><![CDATA[del]]></code>
<code><![CDATA[exists]]></code>
<code><![CDATA[get]]></code>
<code><![CDATA[setEx]]></code>
</UndefinedInterfaceMethod>
</file>
</files>
19 changes: 9 additions & 10 deletions src/DialogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

use KootLabs\TelegramBotDialogs\Exceptions\ControlFlow\SwitchToAnotherDialog;
use KootLabs\TelegramBotDialogs\Objects\BotInitiatedUpdate;
use KootLabs\TelegramBotDialogs\Storages\Store;
use Psr\SimpleCache\CacheInterface;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;

/** @api */
final class DialogManager
{
/** Bot instance to use for all API calls. */
private Api $bot;

/** Storage to store Dialog state between requests. */
private Store | CacheInterface $store;
private DialogRepository $repository;

public function __construct(Api $bot, Store | CacheInterface $store)
public function __construct(Api $bot, DialogRepository $repository)
{
$this->bot = $bot;
$this->store = $store;
$this->repository = $repository;
}

/** @api Use non-default Bot for API calls */
Expand Down Expand Up @@ -93,12 +92,12 @@ public function proceed(Update $update): void
private function findDialogKeyForStore(Update $update): ?string
{
$sharedDialogKey = $this->generateDialogKeySharedBetweenUsers($update);
if ($this->store->has($sharedDialogKey)) {
if ($this->repository->has($sharedDialogKey)) {
return $sharedDialogKey;
}

$userBoundedDialogKey = $this->generateDialogKeyUserBounded($update);
if ($this->store->has($userBoundedDialogKey)) {
if ($this->repository->has($userBoundedDialogKey)) {
return $userBoundedDialogKey;
}

Expand Down Expand Up @@ -128,19 +127,19 @@ private function getDialogInstance(Update $update): ?Dialog
/** Forget Dialog state. */
private function forgetDialogState(Dialog $dialog): void
{
$this->store->delete($this->getDialogKey($dialog));
$this->repository->forget($this->getDialogKey($dialog));
}

/** Store all Dialog. */
private function storeDialogState(Dialog $dialog): void
{
$this->store->set($this->getDialogKey($dialog), $dialog, $dialog->ttl());
$this->repository->put($this->getDialogKey($dialog), $dialog, $dialog->ttl());
}

/** Restore Dialog. */
private function readDialogState(string $key): Dialog
{
return $this->store->get($key);
return $this->repository->get($key);
}

/** @internal This method is a subject for changes in further releases < 1.0 */
Expand Down
45 changes: 45 additions & 0 deletions src/DialogRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace KootLabs\TelegramBotDialogs;

use Psr\SimpleCache\CacheInterface;

/** @api */
final class DialogRepository
{
private CacheInterface $cache;
private string $prefix;

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($this->prefixKey($key), serialize($dialog), $seconds);
}

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

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

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

private function prefixKey(string $key): string
{
return "{$this->prefix}{$key}";
}
}
17 changes: 7 additions & 10 deletions src/Laravel/DialogsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use KootLabs\TelegramBotDialogs\DialogManager;
use KootLabs\TelegramBotDialogs\Laravel\Stores\RedisStoreAdapter;
use KootLabs\TelegramBotDialogs\Storages\Store;
use KootLabs\TelegramBotDialogs\DialogRepository;

/** @api */
final class DialogsServiceProvider extends ServiceProvider implements DeferrableProvider
Expand All @@ -35,14 +34,12 @@ private function offerPublishing(): void

private function registerBindings(): void
{
$this->app->singleton(Store::class, static function (Container $app): Store {
$config = $app->get('config');
$connection = $app->make('redis')->connection($config->get('telegramdialogs.stores.redis.connection'));
return new RedisStoreAdapter($connection);
});
$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);

$this->app->singleton(DialogManager::class, static function (Container $app): DialogManager {
return new DialogManager($app->make('telegram.bot'), $app->make(Store::class));
return new DialogRepository($store, $config->get('telegramdialogs.cache_prefix'));
});

$this->app->alias(DialogManager::class, 'telegram.dialogs');
Expand All @@ -57,7 +54,7 @@ public function provides(): array
return [
'telegram.dialogs',
DialogManager::class,
Store::class,
DialogRepository::class,
];
}
}
72 changes: 0 additions & 72 deletions src/Laravel/Stores/RedisStoreAdapter.php

This file was deleted.

20 changes: 13 additions & 7 deletions src/Laravel/config/telegramdialogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +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' => env('TELEGRAM_DIALOGS_CACHE_TAG', 'tg_dialog_'),
];
106 changes: 0 additions & 106 deletions src/Storages/Drivers/RedisStore.php

This file was deleted.

Loading

0 comments on commit a9bf07c

Please sign in to comment.