Skip to content

Commit

Permalink
Rename Cache to CacheAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Feb 26, 2024
1 parent 095d934 commit 0dcd635
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 117 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -251,5 +251,5 @@ If you have your own cache system, you can use the `Pkl::setCache()` method to s
Phikl comes with the following cache backends:

* `PersistentCache`, which is the default one used by Phikl. It uses a file to store the cache ;
* `ApcuCache`, which uses the APCu extension to store the cache in memory ;
* `MemcachedCache`, which uses the Memcached extension to store the cache in memory.
* `ApcuCacheAdapter`, which uses the APCu extension to store the cache in memory ;
* `MemcachedCacheAdapter`, which uses the Memcached extension to store the cache in memory.
53 changes: 53 additions & 0 deletions src/Cache/Adapter/AbstractRemoteCacheAdapter.php
@@ -0,0 +1,53 @@
<?php

namespace Phikl\Cache\Adapter;

use Phikl\Cache\Entry;
use Psr\SimpleCache\CacheInterface;

abstract class AbstractRemoteCacheAdapter implements CacheInterface
{
/**
* @param iterable<non-empty-string> $keys
*
* @return array<non-empty-string, Entry|null>
*/
public function getMultiple(iterable $keys, mixed $default = null): array
{
$entries = [];
foreach ($keys as $key) {
$entries[$key] = $this->get($key, $default);
}

return $entries;
}

/**
* @param iterable<string, Entry> $values
*/
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
{
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
return false;
}
}

return true;
}

/**
* @param iterable<string> $keys
*/
public function deleteMultiple(iterable $keys): bool
{
$success = true;
foreach ($keys as $key) {
if (!$this->delete($key)) {
$success = false;
}
}

return $success;
}
}
50 changes: 3 additions & 47 deletions src/Cache/ApcuCache.php → src/Cache/Adapter/ApcuCacheAdapter.php
@@ -1,14 +1,14 @@
<?php

namespace Phikl\Cache;
namespace Phikl\Cache\Adapter;

use Psr\SimpleCache\CacheInterface;
use Phikl\Cache\Entry;

/**
* Simple implementation of the PSR-16 CacheInterface using APCu
* for the Pkl modules evaluation cache.
*/
final class ApcuCache implements CacheInterface
final class ApcuCacheAdapter extends AbstractRemoteCacheAdapter
{
public function __construct()
{
Expand Down Expand Up @@ -69,50 +69,6 @@ public function clear(): bool
return apcu_clear_cache();
}

/**
* @param iterable<non-empty-string> $keys
*
* @return array<non-empty-string, Entry|null>
*/
public function getMultiple(iterable $keys, mixed $default = null): array
{
$entries = [];
foreach ($keys as $key) {
$entries[$key] = $this->get($key, $default);
}

return $entries;
}

/**
* @param iterable<string, Entry> $values
*/
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
{
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
return false;
}
}

return true;
}

/**
* @param iterable<string> $keys
*/
public function deleteMultiple(iterable $keys): bool
{
$success = true;
foreach ($keys as $key) {
if (!$this->delete($key)) {
$success = false;
}
}

return $success;
}

public function has(string $key): bool
{
return apcu_exists($key);
Expand Down
@@ -1,25 +1,27 @@
<?php

namespace Phikl\Cache;
namespace Phikl\Cache\Adapter;

use Psr\SimpleCache\CacheInterface;
use Phikl\Cache\Entry;

final class MemcachedCache implements CacheInterface
final class MemcachedCacheAdapter extends AbstractRemoteCacheAdapter
{
private \Memcached $memcached;

/**
* @param MemcachedServer|array<MemcachedServer> $servers
* @param string $persistentId the persistent_id is used to create a unique connection
* pool for the specified servers
*/
public function __construct(MemcachedServer|array $servers)
public function __construct(MemcachedServer|array $servers, string $persistentId = 'phikl')
{
if (!\extension_loaded('memcached')) {
throw new \RuntimeException('Memcached extension is not loaded');
}

$servers = \is_array($servers) ? $servers : [$servers];

$this->memcached = new \Memcached('phikl');
$this->memcached = new \Memcached($persistentId);
foreach ($servers as $server) {
$this->memcached->addServer($server->host, $server->port);
}
Expand Down Expand Up @@ -73,50 +75,6 @@ public function clear(): bool
return $this->memcached->flush();
}

/**
* @param iterable<non-empty-string> $keys
*
* @return array<non-empty-string, Entry|null>
*/
public function getMultiple(iterable $keys, mixed $default = null): array
{
$entries = [];
foreach ($keys as $key) {
$entries[$key] = $this->get($key, $default);
}

return $entries;
}

/**
* @param iterable<string, Entry> $values
*/
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
{
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
return false;
}
}

return true;
}

/**
* @param iterable<string> $keys
*/
public function deleteMultiple(iterable $keys): bool
{
$success = true;
foreach ($keys as $key) {
if (!$this->delete($key)) {
$success = false;
}
}

return $success;
}

public function has(string $key): bool
{
return $this->memcached->get($key) !== false;
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace Phikl\Cache;
namespace Phikl\Cache\Adapter;

final readonly class MemcachedServer
{
Expand Down
@@ -1,15 +1,15 @@
<?php

namespace Cache;
namespace Phikl\Tests\Cache\Adapter;

use Phikl\Cache\ApcuCache;
use Phikl\Cache\Adapter\ApcuCacheAdapter;
use Phikl\Cache\Entry;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

#[RequiresPhpExtension('apcu')]
#[CoversClass(ApcuCache::class)]
#[CoversClass(ApcuCacheAdapter::class)]
class ApcuCacheTest extends TestCase
{
protected function tearDown(): void
Expand All @@ -19,7 +19,7 @@ protected function tearDown(): void

public function testGetWithDefaultOtherThanEntryInstance(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Default value must be null or an instance of Entry');
Expand All @@ -29,7 +29,7 @@ public function testGetWithDefaultOtherThanEntryInstance(): void

public function testGetReturnsDefaultIfKeyDoesNotExist(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry = new Entry('content', 'hash', 0);

Expand All @@ -40,7 +40,7 @@ public function testGetReturnsDefaultIfKeyDoesNotExist(): void

public function testGetOnValidSetEntry(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry = new Entry('content', 'hash', $time = \time());

Expand All @@ -55,14 +55,14 @@ public function testGetOnValidSetEntry(): void

public function testSetReturnsFalseOnInvalidEntry(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$this->assertFalse($cache->set('key', 'invalid'));
}

public function testDeleteEntry(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry = new Entry('content', 'hash', 0);
$cache->set('key', $entry);
Expand All @@ -73,7 +73,7 @@ public function testDeleteEntry(): void

public function testClear(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry = new Entry('content', 'hash', 0);
$cache->set('key', $entry);
Expand All @@ -84,7 +84,7 @@ public function testClear(): void

public function testGetSetMultiple(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry1 = new Entry('content1', 'hash1', 0);
$entry2 = new Entry('content2', 'hash2', 0);
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testGetSetMultiple(): void

public function testDeleteMultiple(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry1 = new Entry('content1', 'hash1', 0);
$entry2 = new Entry('content2', 'hash2', 0);
Expand All @@ -137,7 +137,7 @@ public function testDeleteMultiple(): void

public function testHas(): void
{
$cache = new ApcuCache();
$cache = new ApcuCacheAdapter();

$entry = new Entry('content', 'hash', 0);
$cache->set('key', $entry);
Expand Down
@@ -1,21 +1,28 @@
<?php

namespace Phikl\Tests\Cache;
namespace Phikl\Tests\Cache\Adapter;

use Phikl\Cache\Adapter\MemcachedCacheAdapter;
use Phikl\Cache\Adapter\MemcachedServer;
use Phikl\Cache\Entry;
use Phikl\Cache\MemcachedCache;
use Phikl\Cache\MemcachedServer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

#[RequiresPhpExtension('memcached')]
#[CoversClass(MemcachedCache::class)]
#[CoversClass(MemcachedCacheAdapter::class)]
class MemcachedCacheTest extends TestCase
{
private function createMemcachedCache(): MemcachedCache
protected function setUp(): void
{
return new MemcachedCache(new MemcachedServer('localhost', 11211));
if (false === \fsockopen('localhost', 11211)) {
$this->markTestSkipped('Memcached is not running');
}
}

private function createMemcachedCache(): MemcachedCacheAdapter
{
return new MemcachedCacheAdapter(new MemcachedServer('localhost', 11211));
}

public function testGetWithDefaultOtherThanEntryInstance(): void
Expand Down

0 comments on commit 0dcd635

Please sign in to comment.