From 5be19090d924351845ea0a963bdff3a6d8e8fc38 Mon Sep 17 00:00:00 2001 From: David Coutadeur Date: Mon, 16 Sep 2024 17:34:58 +0200 Subject: [PATCH] improve cache modularity (#40) --- src/Ltb/{ => Cache}/Cache.php | 22 +-------------- src/Ltb/Cache/FileCache.php | 27 +++++++++++++++++++ src/Ltb/Cache/RedisCache.php | 26 ++++++++++++++++++ .../Ltb/{CacheTest.php => FileCacheTest.php} | 20 +++++++------- 4 files changed, 64 insertions(+), 31 deletions(-) rename src/Ltb/{ => Cache}/Cache.php (83%) create mode 100644 src/Ltb/Cache/FileCache.php create mode 100644 src/Ltb/Cache/RedisCache.php rename tests/Ltb/{CacheTest.php => FileCacheTest.php} (95%) diff --git a/src/Ltb/Cache.php b/src/Ltb/Cache/Cache.php similarity index 83% rename from src/Ltb/Cache.php rename to src/Ltb/Cache/Cache.php index 5e39477..30df9d4 100644 --- a/src/Ltb/Cache.php +++ b/src/Ltb/Cache/Cache.php @@ -1,30 +1,10 @@ -cache = new FilesystemAdapter( - $namespace, - $defaultLifetime, - $directory - ); - - // Clean cache from expired entries - $this->cache->prune(); - - } - # Generate a cache entry containing a token, # expiring after $cache_form_expiration seconds function generate_form_token($cache_form_expiration) diff --git a/src/Ltb/Cache/FileCache.php b/src/Ltb/Cache/FileCache.php new file mode 100644 index 0000000..2791a25 --- /dev/null +++ b/src/Ltb/Cache/FileCache.php @@ -0,0 +1,27 @@ +cache = new FilesystemAdapter( + $namespace, + $defaultLifetime, + $directory + ); + + // Clean cache from expired entries + $this->cache->prune(); + + } + +} + +?> diff --git a/src/Ltb/Cache/RedisCache.php b/src/Ltb/Cache/RedisCache.php new file mode 100644 index 0000000..edbd81c --- /dev/null +++ b/src/Ltb/Cache/RedisCache.php @@ -0,0 +1,26 @@ +cache = new RedisAdapter( + $redis_connection, + $namespace, + $defaultLifetime, + ); + + } + +} + +?> diff --git a/tests/Ltb/CacheTest.php b/tests/Ltb/FileCacheTest.php similarity index 95% rename from tests/Ltb/CacheTest.php rename to tests/Ltb/FileCacheTest.php index b357d2d..b76b17b 100644 --- a/tests/Ltb/CacheTest.php +++ b/tests/Ltb/FileCacheTest.php @@ -2,12 +2,12 @@ require __DIR__ . '/../../vendor/autoload.php'; -final class CacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase +final class FileCacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { public function test_construct(): void { - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -19,7 +19,7 @@ public function test_construct(): void public function test_generate_form_token(): void { - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -65,7 +65,7 @@ public function test_verify_form_token_ok(): void $generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -102,7 +102,7 @@ public function test_verify_form_token_ko(): void $generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -137,7 +137,7 @@ public function test_get_token_ok(): void $tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; $token_content = "test"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -172,7 +172,7 @@ public function test_get_token_ko(): void $tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; $token_content = "test"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -211,7 +211,7 @@ public function test_save_token_new(): void ]; $cache_token_expiration = 3600; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -253,7 +253,7 @@ public function test_save_token_existing(): void ]; $cache_token_expiration = 3600; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -294,7 +294,7 @@ public function test_save_token_existing_noexpiration(): void 'par2' => 'val2' ]; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null