diff --git a/src/Cache/Psr16CacheClient.php b/src/Cache/Psr16CacheClient.php index 2d2298e5..84f37893 100644 --- a/src/Cache/Psr16CacheClient.php +++ b/src/Cache/Psr16CacheClient.php @@ -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; @@ -29,12 +30,14 @@ 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; @@ -42,7 +45,8 @@ public function __construct( 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); @@ -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; @@ -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; @@ -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; diff --git a/tests/Cache/Psr16ClientTest.php b/tests/Cache/Psr16ClientTest.php index 22e296b2..ac7816ed 100644 --- a/tests/Cache/Psr16ClientTest.php +++ b/tests/Cache/Psr16ClientTest.php @@ -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; @@ -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 */