Skip to content

Commit

Permalink
add redis option (#11)
Browse files Browse the repository at this point in the history
* add redis option
  • Loading branch information
mattvb91 authored Sep 20, 2022
1 parent eed3374 commit 8d36817
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Config/Apps/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +18,8 @@ class Cache implements App

private ?Nuts $_nuts;

private ?Redis $_redis;

private LogLevel $_logLevel = LogLevel::INFO;

/** @var Key[] */
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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()];
Expand Down
22 changes: 22 additions & 0 deletions src/Config/Apps/Cache/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace mattvb91\CaddyPhp\Config\Apps\Cache;

use mattvb91\CaddyPhp\Interfaces\Arrayable;

class Redis implements Arrayable
{
private string $_url;

public function __construct(string $_url)
{
$this->_url = $_url;
}

public function toArray(): array
{
return [
'url' => $this->_url,
];
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Apps/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Apps/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 8d36817

Please sign in to comment.