From 8d368179106245ac1484f704092c0a93ac000e09 Mon Sep 17 00:00:00 2001 From: Matthias von Bargen Date: Tue, 20 Sep 2022 15:17:07 +0200 Subject: [PATCH] add redis option (#11) * add redis option --- src/Config/Apps/Cache.php | 23 ++++++++--- src/Config/Apps/Cache/Redis.php | 22 +++++++++++ tests/Integration/Apps/CacheTest.php | 15 +++++++ tests/Unit/Apps/CacheTest.php | 59 ++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/Config/Apps/Cache/Redis.php diff --git a/src/Config/Apps/Cache.php b/src/Config/Apps/Cache.php index 789dd3a..f8c1b40 100644 --- a/src/Config/Apps/Cache.php +++ b/src/Config/Apps/Cache.php @@ -6,6 +6,7 @@ use mattvb91\CaddyPhp\Config\Apps\Cache\Cdn; use mattvb91\CaddyPhp\Config\Apps\Cache\Key; use mattvb91\CaddyPhp\Config\Apps\Cache\Nuts; +use mattvb91\CaddyPhp\Config\Apps\Cache\Redis; use mattvb91\CaddyPhp\Config\Logs\LogLevel; use mattvb91\CaddyPhp\Interfaces\App; @@ -17,6 +18,8 @@ class Cache implements App private ?Nuts $_nuts; + private ?Redis $_redis; + private LogLevel $_logLevel = LogLevel::INFO; /** @var Key[] */ @@ -32,10 +35,6 @@ public function __construct( { $this->_api = $_api; $this->_cdn = $_cdn; - - //This shouldnt be here, we need to add optional named parameters - //to constructor instead - $this->_nuts = new Nuts(); } public function setApi(Api $api): static @@ -80,6 +79,13 @@ public function setNuts(Nuts $nuts): static return $this; } + public function setRedis(?Redis $redis): static + { + $this->_redis = $redis; + + return $this; + } + public function addCacheKey(Key $key): static { if (!isset($this->_cacheKeys)) { @@ -96,12 +102,19 @@ public function toArray(): array $array = [ 'api' => $this->_api->toArray(), 'cdn' => $this->_cdn->toArray(), - 'nuts' => $this->_nuts->toArray(), 'log_level' => $this->_logLevel->value, 'stale' => $this->_stale, 'ttl' => $this->_ttl, ]; + if (isset($this->_nuts)) { + $array['nuts'] = $this->_nuts->toArray(); + } + + if (isset($this->_redis)) { + $array['redis'] = $this->_redis->toArray(); + } + if (isset($this->_cacheKeys)) { $array['cache_keys'] = array_map(static function (Key $key) { return [$key->getPattern() => $key->toArray()]; diff --git a/src/Config/Apps/Cache/Redis.php b/src/Config/Apps/Cache/Redis.php new file mode 100644 index 0000000..864dff1 --- /dev/null +++ b/src/Config/Apps/Cache/Redis.php @@ -0,0 +1,22 @@ +_url = $_url; + } + + public function toArray(): array + { + return [ + 'url' => $this->_url, + ]; + } +} \ No newline at end of file diff --git a/tests/Integration/Apps/CacheTest.php b/tests/Integration/Apps/CacheTest.php index 8a79ea6..5597bf3 100644 --- a/tests/Integration/Apps/CacheTest.php +++ b/tests/Integration/Apps/CacheTest.php @@ -118,4 +118,19 @@ public function test_surrogate_key_flush() $this->assertStringContainsString('Souin; fwd=uri-miss; stored', $request->getHeader('cache-status')[0]); } + + /** + * @coversNothing + */ + public function test_can_load_with_redis() + { + $caddy = new Caddy(); + + $cacheApi = new Cache\Api(); + $cache = new Cache($cacheApi); + $cache->setRedis((new Cache\Redis('redis:6379'))); + + $caddy->addApp($cache); + $this->assertCaddyConfigLoaded($caddy); + } } \ No newline at end of file diff --git a/tests/Unit/Apps/CacheTest.php b/tests/Unit/Apps/CacheTest.php index 4972298..a95ed1f 100644 --- a/tests/Unit/Apps/CacheTest.php +++ b/tests/Unit/Apps/CacheTest.php @@ -27,9 +27,68 @@ public function testCacheInit() 'strategy' => 'hard', ], 'log_level' => 'INFO', + 'stale' => '3600s', + 'ttl' => '3600s', + ], $cache->ToArray()); + } + + /** + * @covers \mattvb91\CaddyPhp\Config\Apps\Cache::setNuts + * @covers \mattvb91\CaddyPhp\Config\Apps\Cache::toArray + */ + public function testSetNutsCache() + { + $cache = new Cache(); + $nuts = new Cache\Nuts(); + + $cache->setNuts($nuts); + $this->assertEquals([ + 'api' => [ + 'basepath' => '/cache', + 'souin' => [ + 'basepath' => '/souin', + 'enable' => true, + ], + ], + 'cdn' => [ + 'dynamic' => true, + 'strategy' => 'hard', + ], 'nuts' => [ 'path' => '/tmp/nuts-souin', ], + 'log_level' => 'INFO', + 'stale' => '3600s', + 'ttl' => '3600s', + ], $cache->ToArray()); + } + + /** + * @covers \mattvb91\CaddyPhp\Config\Apps\Cache::setRedis + * @covers \mattvb91\CaddyPhp\Config\Apps\Cache::toArray + */ + public function testSetRedisCache() + { + $cache = new Cache(); + $redis = new Cache\Redis('localhost:6379'); + + $cache->setRedis($redis); + $this->assertEquals([ + 'api' => [ + 'basepath' => '/cache', + 'souin' => [ + 'basepath' => '/souin', + 'enable' => true, + ], + ], + 'cdn' => [ + 'dynamic' => true, + 'strategy' => 'hard', + ], + 'redis' => [ + 'url' => 'localhost:6379', + ], + 'log_level' => 'INFO', 'stale' => '3600s', 'ttl' => '3600s', ], $cache->ToArray());