Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion System/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use TeleBot\System\Core\Enums\DataSource;
use TeleBot\System\Interfaces\ICacheDriver;
use TeleBot\System\Cache\Drivers\{FileDriver, RedisDriver};
use TeleBot\System\Cache\Drivers\{DbDriver, FileDriver, RedisDriver};

class Cache
{
Expand All @@ -31,6 +31,7 @@ private function init(): ICacheDriver
$driver = env('CACHE_DRIVER', DataSource::FILESYSTEM);
$this->client = match ($driver) {
DataSource::REDIS => new RedisDriver(),
DataSource::DATABASE => new DbDriver(),
DataSource::FILESYSTEM => new FileDriver(),
};
}
Expand Down
38 changes: 38 additions & 0 deletions System/Cache/Drivers/DbDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TeleBot\System\Cache\Drivers;

use TeleBot\System\Drivers\DatabaseDriver as Store;
use TeleBot\System\Interfaces\ICacheDriver;

class DbDriver implements ICacheDriver
{
/** @var Store $store */
private Store $store;

public function __construct()
{
$this->store = new Store('cache', 'cache_key', 'cache_value', 'ttl', true);
}

public function getAll(int $cursor = 0, int $count = 100): array
{
return $this->store->getAll($cursor, $count);
}

public function read(string $key): mixed
{
return $this->store->get($key);
}

public function write(string $key, mixed $data, ?string $ttl = null): bool
{
$ttl = $ttl ? iso8601_to_seconds($ttl) : null;
return $this->store->set($key, $data, $ttl);
}

public function delete(string $key): bool
{
return (bool)$this->store->delete($key);
}
}
67 changes: 13 additions & 54 deletions System/Cache/Drivers/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,37 @@

namespace TeleBot\System\Cache\Drivers;

use TeleBot\System\Core\Traits\Expirable;
use TeleBot\System\Drivers\FilesystemDriver as Store;
use TeleBot\System\Interfaces\ICacheDriver;

class FileDriver implements ICacheDriver
{
/** @var Store $store */
private Store $store;

use Expirable;
public function __construct()
{
$this->store = new Store(env('CACHE_DIR'));
}

/**
* @inheritDoc
*/
public function getAll(int $cursor = 0, int $count = 100): array
{
$dir = implode('/', [env('CACHE_DIR'), '*']);
return array_filter(glob($dir) ?? [], 'is_file');
return $this->store->getAll($cursor, $count);
}

/**
* @inheritDoc
*/
public function read(string $key): mixed
{
$cachePath = env('CACHE_DIR');
$cacheFilePath = $cachePath . '/' . $key;
if (!file_exists($cacheFilePath)) {
return null;
}

$content = file_get_contents($cacheFilePath);
if ($content === false) {
return null;
}

if (($json = json_decode($content, true))) {
if ($this->hasExpired($json)) {
$this->delete($key);
return null;
}

$content = $this->restore($json);
}

return $content;
return $this->store->get($key);
}

/**
* @inheritDoc
*/
public function write(string $key, mixed $data, ?string $ttl = null): bool
{
$data = [
self::TTL_KEY => $ttl ? iso8601_to_timestamp($ttl) : null,
self::CONTENT_KEY => $data,
];

$cachePath = env('CACHE_DIR');
$cacheFilePath = $cachePath . '/' . $key;
return (bool)file_put_contents($cacheFilePath, json_encode($data));
$ttl = $ttl ? iso8601_to_seconds($ttl) : null;
return $this->store->set($key, $data, $ttl);
}

/**
* @inheritDoc
*/
public function delete(string $key): bool
{
$cachePath = env('CACHE_DIR');
$cacheFilePath = $cachePath . '/' . $key;
if (file_exists($cacheFilePath)) {
return @unlink($cacheFilePath);
}

return true;
return (bool)$this->store->delete($key);
}

}
}
76 changes: 10 additions & 66 deletions System/Cache/Drivers/RedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@

namespace TeleBot\System\Cache\Drivers;

use Predis\Client;
use TeleBot\System\Drivers\RedisDriver as Store;
use TeleBot\System\Exceptions\MissingToken;
use TeleBot\System\Interfaces\ICacheDriver;

class RedisDriver implements ICacheDriver
{

/** @var Client $client redis client */
protected Client $client;

/** @var mixed $cache cache value of cache content */
protected mixed $cache = [];

/** @var string $prefix redis key prefix */
protected string $prefix = 'tg:bots';
/** @var Store $store */
private Store $store;

/**
* default constructor
*
* @throws MissingToken
*/
public function __construct()
Expand All @@ -38,74 +29,27 @@ public function __construct()
}

$botId = explode(':', $botToken, 2)[0];
$this->prefix = "tg:bots:$botId:cache";
$this->client = new Client([
'scheme' => 'tcp',
'host' => env('REDIS_HOST'),
'port' => env('REDIS_PORT'),
'user' => env('REDIS_USER'),
'password' => env('REDIS_PASSWORD')
]);
$this->store = new Store("tg:bots:{$botId}:cache");
}

/**
* @inheritDoc
*/
public function getAll(int $cursor = 0, int $count = 100): array
{
$keys = [];
do {
$options = [
'count' => $count,
'match' => $this->prefix . ':*',
];

[$page, $_keys] = $this->client->scan($cursor, $options);
if (!empty($_keys)) {
$keys = array_merge($keys, $_keys);
}
} while ($page !== '0');
return $keys;
return $this->store->getAll($cursor, $count);
}

/**
* @inheritDoc
*/
public function read(string $key): mixed
{
if (empty($this->cache)) {
$data = $this->client->get("$this->prefix:$key");
if (!empty($data) && ($json = json_decode($data, true))) {
$this->cache = $json;
}
}

return $this->cache;
return $this->store->get($key);
}

/**
* @inheritDoc
*/
public function write(string $key, mixed $data, ?string $ttl = null): bool
{
$this->cache = $data;
if (is_array($data) || is_object($data)) {
$data = json_encode($data);
}

return !!$this->client->set(
"$this->prefix:$key",
$data,
($ttl ? 'EX' : null),
($ttl ? iso8601_to_seconds($ttl) : null)
);
$ttl = $ttl ? iso8601_to_seconds($ttl) : null;
return $this->store->set($key, $data, $ttl);
}

/**
* @inheritDoc
*/
public function delete(string $key): bool
{
return $this->client->del("$this->prefix:$key");
return (bool)$this->store->delete($key);
}
}
}
Loading