feat: add redis cache driver

This commit is contained in:
2025-12-09 21:28:33 +03:00
parent 6aa258385e
commit 2b0f04eb94
5 changed files with 172 additions and 9 deletions

View File

@@ -32,8 +32,19 @@ services:
- PHP_IDE_CONFIG=serverName=telecart
- php.session.gc_maxlifetime=28800
- php.session.cookie_lifetime=0
- TELECART_CACHE_DRIVER=database
- TELECART_REDIS_HOST=redis
- TELECART_REDIS_PORT=6379
- TELECART_REDIS_DATABASE=0
depends_on:
- mysql
- redis
redis:
image: redis:latest
restart: unless-stopped
healthcheck:
test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
mysql:
image: mariadb:10.2.7

View File

@@ -28,6 +28,7 @@
"monolog/monolog": "^2.10",
"nesbot/carbon": "^2.73",
"php": "^7.4",
"predis/predis": "^2.0",
"psr/container": "^2.0",
"psr/log": "^1.1",
"symfony/cache": "^5.4",

View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "73f60d8ed1037cbbd7e6368936ed1dc7",
"content-hash": "26d74793eddc9f89bbc5d5294e07ed6c",
"packages": [
{
"name": "brick/math",
@@ -1208,6 +1208,68 @@
],
"time": "2025-08-21T11:53:16+00:00"
},
{
"name": "predis/predis",
"version": "v2.4.1",
"source": {
"type": "git",
"url": "https://github.com/predis/predis.git",
"reference": "07105e050622ed80bd60808367ced9e379f31530"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/predis/predis/zipball/07105e050622ed80bd60808367ced9e379f31530",
"reference": "07105e050622ed80bd60808367ced9e379f31530",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.3",
"phpstan/phpstan": "^1.9",
"phpunit/phpcov": "^6.0 || ^8.0",
"phpunit/phpunit": "^8.0 || ^9.4"
},
"suggest": {
"ext-relay": "Faster connection with in-memory caching (>=0.6.2)"
},
"type": "library",
"autoload": {
"psr-4": {
"Predis\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Till Krüss",
"homepage": "https://till.im",
"role": "Maintainer"
}
],
"description": "A flexible and feature-complete Redis/Valkey client for PHP.",
"homepage": "http://github.com/predis/predis",
"keywords": [
"nosql",
"predis",
"redis"
],
"support": {
"issues": "https://github.com/predis/predis/issues",
"source": "https://github.com/predis/predis/tree/v2.4.1"
},
"funding": [
{
"url": "https://github.com/sponsors/tillkruss",
"type": "github"
}
],
"time": "2025-11-12T18:00:11+00:00"
},
{
"name": "psr/cache",
"version": "1.0.1",
@@ -4266,12 +4328,12 @@
"source": {
"type": "git",
"url": "https://github.com/Roave/SecurityAdvisories.git",
"reference": "10c1e6abcb8094a428b92e7d8c3126371f9f9126"
"reference": "75d4ccd9c135c4ac904cd4211a43e51d12feb1ef"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/10c1e6abcb8094a428b92e7d8c3126371f9f9126",
"reference": "10c1e6abcb8094a428b92e7d8c3126371f9f9126",
"url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/75d4ccd9c135c4ac904cd4211a43e51d12feb1ef",
"reference": "75d4ccd9c135c4ac904cd4211a43e51d12feb1ef",
"shasum": ""
},
"conflict": {
@@ -4523,6 +4585,7 @@
"feehi/feehicms": "<=2.1.1",
"fenom/fenom": "<=2.12.1",
"filament/actions": ">=3.2,<3.2.123",
"filament/filament": ">=4,<4.3.1",
"filament/infolists": ">=3,<3.2.115",
"filament/tables": ">=3,<3.2.115",
"filegator/filegator": "<7.8",
@@ -4777,6 +4840,7 @@
"netgen/tagsbundle": ">=3.4,<3.4.11|>=4,<4.0.15",
"nette/application": ">=2,<2.0.19|>=2.1,<2.1.13|>=2.2,<2.2.10|>=2.3,<2.3.14|>=2.4,<2.4.16|>=3,<3.0.6",
"nette/nette": ">=2,<2.0.19|>=2.1,<2.1.13",
"neuron-core/neuron-ai": "<=2.8.11",
"nilsteampassnet/teampass": "<3.1.3.1-dev",
"nitsan/ns-backup": "<13.0.1",
"nonfiction/nterchange": "<4.1.1",
@@ -4796,7 +4860,7 @@
"october/system": "<3.7.5",
"oliverklee/phpunit": "<3.5.15",
"omeka/omeka-s": "<4.0.3",
"onelogin/php-saml": "<2.10.4",
"onelogin/php-saml": "<2.21.1|>=3,<3.8.1|>=4,<4.3.1",
"oneup/uploader-bundle": ">=1,<1.9.3|>=2,<2.1.5",
"open-web-analytics/open-web-analytics": "<1.8.1",
"opencart/opencart": ">=0",
@@ -4911,7 +4975,7 @@
"reportico-web/reportico": "<=8.1",
"rhukster/dom-sanitizer": "<1.0.7",
"rmccue/requests": ">=1.6,<1.8",
"robrichards/xmlseclibs": ">=1,<3.0.4",
"robrichards/xmlseclibs": "<=3.1.3",
"roots/soil": "<4.1",
"roundcube/roundcubemail": "<1.5.10|>=1.6,<1.6.11",
"rudloff/alltube": "<3.0.3",
@@ -4930,8 +4994,8 @@
"shopware/core": "<6.6.10.9-dev|>=6.7,<6.7.4.1-dev",
"shopware/platform": "<6.6.10.7-dev|>=6.7,<6.7.3.1-dev",
"shopware/production": "<=6.3.5.2",
"shopware/shopware": "<=5.7.17|>=6.7,<6.7.2.1-dev",
"shopware/storefront": "<=6.4.8.1|>=6.5.8,<6.5.8.7-dev",
"shopware/shopware": "<=5.7.17|>=6.4.6,<6.6.10.10-dev|>=6.7,<6.7.5.1-dev",
"shopware/storefront": "<6.6.10.10-dev|>=6.7,<6.7.5.1-dev",
"shopxo/shopxo": "<=6.4",
"showdoc/showdoc": "<2.10.4",
"shuchkin/simplexlsx": ">=1.0.12,<1.1.13",
@@ -5182,6 +5246,7 @@
"yoast-seo-for-typo3/yoast_seo": "<7.2.3",
"yourls/yourls": "<=1.8.2",
"yuan1994/tpadmin": "<=1.3.12",
"yungifez/skuul": "<=2.6.5",
"z-push/z-push-dev": "<2.7.6",
"zencart/zencart": "<=1.5.7.0-beta",
"zendesk/zendesk_api_client_php": "<2.2.11",
@@ -5257,7 +5322,7 @@
"type": "tidelift"
}
],
"time": "2025-12-05T21:05:14+00:00"
"time": "2025-12-09T18:07:05+00:00"
},
{
"name": "sebastian/cli-parser",

View File

@@ -5,11 +5,14 @@ namespace Openguru\OpenCartFramework\Cache;
use Openguru\OpenCartFramework\Container\Container;
use Openguru\OpenCartFramework\Container\ServiceProvider;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Predis\Client as PredisClient;
class CacheServiceProvider extends ServiceProvider
{
public function register(): void
{
// Регистрация DoctrineDbalAdapter для MySQL кеша
$this->container->singleton(DoctrineDbalAdapter::class, function (Container $container) {
$host = $container->getConfigValue('database.host');
$username = $container->getConfigValue('database.username');
@@ -26,7 +29,40 @@ class CacheServiceProvider extends ServiceProvider
return new DoctrineDbalAdapter($dsn, $namespace, $defaultLifetime, $options);
});
// Регистрация RedisAdapter для Redis кеша
$this->container->singleton(RedisAdapter::class, function (Container $container) {
$host = env('TELECART_REDIS_HOST', '127.0.0.1');
$port = (int) env('TELECART_REDIS_PORT', 6379);
$password = env('TELECART_REDIS_PASSWORD', null);
$database = (int) env('TELECART_REDIS_DATABASE', 0);
$redisConfig = [
'host' => $host,
'port' => $port,
'database' => $database,
];
if ($password !== null) {
$redisConfig['password'] = $password;
}
$redisClient = new PredisClient($redisConfig);
$namespace = $container->getConfigValue('cache.namespace');
$defaultLifetime = $container->getConfigValue('cache.default_lifetime');
return new RedisAdapter($redisClient, $namespace, $defaultLifetime);
});
// Регистрация CacheInterface с выбором драйвера
$this->container->singleton(CacheInterface::class, function (Container $container) {
$driver = env('TELECART_CACHE_DRIVER', 'mysql');
if ($driver === 'redis') {
return new SymfonyRedisCache($container->get(RedisAdapter::class));
}
// По умолчанию используем MySQL
return new SymfonyMySqlCache($container->get(DoctrineDbalAdapter::class));
});
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Openguru\OpenCartFramework\Cache;
use Symfony\Component\Cache\Adapter\RedisAdapter;
class SymfonyRedisCache implements CacheInterface
{
protected RedisAdapter $cache;
public function __construct(RedisAdapter $adapter)
{
$this->cache = $adapter;
}
public function get(string $key)
{
$item = $this->cache->getItem($key);
return $item->isHit() ? $item->get() : null;
}
public function set(string $key, $value, ?int $ttlSeconds = null): void
{
$item = $this->cache->getItem($key);
$item->set($value);
if ($ttlSeconds) {
$item->expiresAfter($ttlSeconds);
}
$this->cache->save($item);
}
public function delete(string $key): void
{
$this->cache->deleteItem($key);
}
public function clear(): void
{
$this->cache->clear();
}
public function prune(): void
{
$this->cache->prune();
}
}