Skip to content

Commit

Permalink
fix: allow users to override PSR-16 cache name (#166)
Browse files Browse the repository at this point in the history
* fix: allow users to override PSR-16 cache name

* fix: make cache name argument nullable
  • Loading branch information
pgautier404 authored Jun 12, 2023
1 parent 7227c08 commit ca6412b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/Cache/Psr16CacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class Psr16CacheClient implements CacheInterface
{

private CacheClient $momento;
private string $cacheName;
// PSR-16 spec requires a default of as close to "forever" as the engine allows.
// The below is set to a week and will be truncated as necessary for the cache
// backend in use.
private const DEFAULT_TTL_SECONDS = 604800;
private const CACHE_NAME = "momento-psr16";
private const DEFAULT_CACHE_NAME = "momento-psr16";
private ?CacheException $lastError = null;
private bool $throwExceptions = true;

Expand All @@ -29,20 +30,23 @@ class Psr16CacheClient implements CacheInterface
* @param ICredentialProvider $authProvider
* @param int|null $defaultTtlSeconds
* @param bool|null $throwExceptions
* @param string|null $cacheName
*/
public function __construct(
IConfiguration $configuration,
ICredentialProvider $authProvider,
?int $defaultTtlSeconds,
?bool $throwExceptions = null
?bool $throwExceptions = null,
?string $cacheName = null
)
{
$ttlSeconds = $defaultTtlSeconds ?? self::DEFAULT_TTL_SECONDS;
$this->momento = new CacheClient($configuration, $authProvider, $ttlSeconds);
if (!is_null($throwExceptions)) {
$this->throwExceptions = $throwExceptions;
}
$response = $this->momento->createCache(self::CACHE_NAME);
$this->cacheName = $cacheName ?? self::DEFAULT_CACHE_NAME;
$response = $this->momento->createCache($this->cacheName);
if ($error = $response->asError()) {
$this->throwExceptions = true;
$this->handleCacheError($error);
Expand Down Expand Up @@ -110,7 +114,7 @@ public function getLastError(bool $clear_error = true): CacheException|null
public function get(string $key, mixed $default = null): mixed
{
validatePsr16Key($key);
$response = $this->momento->get(self::CACHE_NAME, $key);
$response = $this->momento->get($this->cacheName, $key);
if ($error = $response->asError()) {
$this->handleCacheError($error);
return $default;
Expand Down Expand Up @@ -139,7 +143,7 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
return $this->delete($key);
}

$response = $this->momento->set(self::CACHE_NAME, $key, serialize($value), $ttl);
$response = $this->momento->set($this->cacheName, $key, serialize($value), $ttl);
if ($error = $response->asError()) {
$this->handleCacheError($error);
return false;
Expand All @@ -153,7 +157,7 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
public function delete(string $key): bool
{
validatePsr16Key($key);
$response = $this->momento->delete(self::CACHE_NAME, $key);
$response = $this->momento->delete($this->cacheName, $key);
if ($error = $response->asError()) {
$this->handleCacheError($error);
return false;
Expand Down
32 changes: 29 additions & 3 deletions tests/Cache/Psr16ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
use Momento\Cache\Psr16CacheClient;
use Momento\Cache\CacheClient;
use Momento\Config\Configuration;
use Momento\Config\Configurations\Laptop;
use Momento\Config\Transport\StaticGrpcConfiguration;
use Momento\Config\Transport\StaticTransportStrategy;
use Momento\Logging\NullLoggerFactory;
use Psr\Log\NullLogger;
use PHPUnit\Framework\TestCase;

/**
* @covers Psr16CacheClient
*/
class Psr16ClientTest extends \PHPUnit\Framework\TestCase
class Psr16ClientTest extends TestCase
{
private string $TEST_CACHE_NAME = "momento-psr16-test";
private int $DEFAULT_TTL_SECONDS = 10;
private Psr16CacheClient $client;

Expand Down Expand Up @@ -91,6 +91,32 @@ public function dataTypeProvider(): array
];
}

public function testOverrideDefaultCacheName()
{
$testCacheName = "PSR16-test-cache";
$configuration = Laptop::latest();
$authProvider = new EnvMomentoTokenProvider("TEST_AUTH_TOKEN");
$client = new CacheClient(
$configuration, $authProvider, $this->DEFAULT_TTL_SECONDS
);
$psrClient = new Psr16CacheClient(
$configuration, $authProvider, $this->DEFAULT_TTL_SECONDS, cacheName: $testCacheName
);
$listResponse = $client->listCaches();
$this->assertNull($listResponse->asError());
$gotMatch = false;
foreach ($listResponse->asSuccess()->caches() as $cache) {
$cacheName = $cache->name();
if ($cacheName === $testCacheName) {
$gotMatch = true;
break;
}
}
$this->assertTrue($gotMatch);
$deleteResponse = $client->deleteCache($testCacheName);
$this->assertNull($deleteResponse->asError());
}

/**
* @dataProvider dataTypeProvider
*/
Expand Down

0 comments on commit ca6412b

Please sign in to comment.