Skip to content

Commit

Permalink
improve cache modularity (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Sep 16, 2024
1 parent f80d3c5 commit 5be1909
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
22 changes: 1 addition & 21 deletions src/Ltb/Cache.php → src/Ltb/Cache/Cache.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
<?php namespace Ltb;

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
<?php namespace Ltb\Cache;

class Cache {

// symfony cache instance
public $cache = null;

public function __construct(
$namespace = 'ltbCache',
$defaultLifetime = 0,
$directory = null
)
{

$this->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)
Expand Down
27 changes: 27 additions & 0 deletions src/Ltb/Cache/FileCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Ltb\Cache;

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class FileCache extends \Ltb\Cache\Cache{

public function __construct(
$namespace = 'ltbCache',
$defaultLifetime = 0,
$directory = null
)
{

$this->cache = new FilesystemAdapter(
$namespace,
$defaultLifetime,
$directory
);

// Clean cache from expired entries
$this->cache->prune();

}

}

?>
26 changes: 26 additions & 0 deletions src/Ltb/Cache/RedisCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace Ltb\Cache;

use Symfony\Component\Cache\Adapter\RedisAdapter;

class RedisCache extends \Ltb\Cache\Cache{

public function __construct(
$redis_url,
$namespace = 'ltbCache',
$defaultLifetime = 0,
)
{

$redis_connection = RedisAdapter::createConnection( $redis_url );

$this->cache = new RedisAdapter(
$redis_connection,
$namespace,
$defaultLifetime,
);

}

}

?>
20 changes: 10 additions & 10 deletions tests/Ltb/CacheTest.php → tests/Ltb/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5be1909

Please sign in to comment.