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 6f104d1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 41 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"php": ">=7.4",
"ext-ldap": ">=7.4",
"phpmailer/phpmailer": "^6.5.0",
"symfony/cache": "^v5.4.42"
"symfony/cache": "^v5.4.42",
"predis/predis": "^v2.2.2"
},
"require-dev": {
"phpunit/phpunit": ">=8",
Expand Down
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,
);

}

}

?>
27 changes: 8 additions & 19 deletions tests/Ltb/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@
final class CacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

public function test_construct(): void
{
$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);
$this->assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\FilesystemAdapter, "Error while initializing cache object");
}


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 +54,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 +91,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 +126,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 +161,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 +200,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 +242,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 +283,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
18 changes: 18 additions & 0 deletions tests/Ltb/FileCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

final class FileCacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

public function test_construct(): void
{
$cacheInstance = new \Ltb\Cache\FileCache(
"testCache",
0,
null
);
$this->assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\FilesystemAdapter, "Error while initializing cache object");
}

}
38 changes: 38 additions & 0 deletions tests/Ltb/RedisCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

final class RedisCacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

public function test_construct(): void
{
$redis_url = "dummy";
$redis_connection = "redis_connection";
$namespace = "ltbCache";
$defaultLifetime = 0;

$redisAdapterMock = Mockery::mock('overload:Symfony\Component\Cache\Adapter\RedisAdapter');

$redisAdapterMock->shouldreceive('createConnection')
->with( $redis_url )
->andReturn( $redis_connection );

$redisAdapterMock->shouldReceive('__construct')
->once()
->with(
$redis_connection,
$namespace,
$defaultLifetime
);

$cacheInstance = new \Ltb\Cache\RedisCache(
$redis_url,
$namespace,
$defaultLifetime
);
$this->assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\RedisAdapter, "Error while initializing Redis cache object");
}


}

0 comments on commit 6f104d1

Please sign in to comment.