Skip to content

Commit

Permalink
Drop PSR SimpleCache
Browse files Browse the repository at this point in the history
The way that interface works with multiple versions has in practice
become a nightmare of dependency management.

V1, V2 and V3 of simple cache are not compatible with each other yet
use the same name and same namespace. So that you can't have a project
using 2 libraries when one expects and requires V1 and the other expects
and requires another version.

Had those interfaces been named CacheInterface, CacheInterfaceV2 and
CacheInterfaceV3 then it would have been possible to have multiple
versions, and even type hints like `CacheInterface | CacheInterfaceV2`
would've worked.

Currently none of this is possible and it's a mess.
  • Loading branch information
khepin committed Dec 16, 2023
1 parent a48a53c commit f3e1238
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 79 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
}
],
"require": {
"php": ">=8.0",
"psr/simple-cache": "^2|^3",
"psr/cache": "^3.0"
"php": ">=8.0"
},
"suggest": {
"ext-memcached": "Needed to use Square\\TTCache\\Store\\MemcachedStore and Square\\TTCache\\Store\\ShardedMemcachedStore implementations"
Expand Down
7 changes: 4 additions & 3 deletions src/Store/CacheStoreException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Square\TTCache\Store;

use Psr\SimpleCache\CacheException;
use Exception;

class CacheStoreException extends Exception implements CacheException
class CacheStoreException extends Exception
{
}
20 changes: 20 additions & 0 deletions src/Store/CacheStoreInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Square\TTCache\Store;

interface CacheStoreInterface
{
public function get(string $key, mixed $default = null): mixed;

public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool;

public function delete(string $key): bool;

public function getMultiple(iterable $keys, mixed $default = null): iterable;

public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool;

public function deleteMultiple(iterable $keys): bool;
}
16 changes: 11 additions & 5 deletions src/Store/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace Square\TTCache\Store;

use Psr\SimpleCache\CacheInterface;
use Memcached;

/**
* PSR Cache interface implementation of memcache.
*/
class MemcachedStore implements CacheInterface
class MemcachedStore implements CacheStoreInterface
{
protected Memcached $mc;

private const MC_SUCCESS = 0;

private const MC_NOT_FOUND = 16;

private const MC_VALID_CODES = [
Expand All @@ -29,7 +29,7 @@ public function __construct(Memcached $mc)

protected function checkResultCode(): void
{
if (!in_array($this->mc->getResultCode(), self::MC_VALID_CODES)) {
if (! in_array($this->mc->getResultCode(), self::MC_VALID_CODES)) {
throw new CacheStoreException('invalid MC return code', $this->mc->getResultCode());
}
}
Expand All @@ -38,10 +38,11 @@ public function get(string $key, mixed $default = null): mixed
{
$result = $this->mc->get($key);
$this->checkResultCode();

return $result;
}

public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool
{
if (is_null($ttl)) {
$ttl = 0;
Expand All @@ -51,13 +52,15 @@ public function set(string $key, mixed $value, null|int|\DateInterval $ttl = nul
}
$result = $this->mc->set($key, $value, $ttl);
$this->checkResultCode();

return $result;
}

public function delete(string $key): bool
{
$result = $this->mc->delete($key);
$this->checkResultCode();

return $result;
}

Expand All @@ -70,20 +73,23 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$result = $this->mc->getMulti($keys);
$this->checkResultCode();

return $result;
}

public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool
{
$result = $this->mc->setMulti($values, $ttl ?? 0);
$this->checkResultCode();

return $result;
}

public function deleteMultiple(iterable $keys): bool
{
$result = $this->mc->deleteMulti($keys);
$this->checkResultCode();

return true;
}

Expand Down
24 changes: 16 additions & 8 deletions src/Store/ShardedMemcachedStore.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Square\TTCache\Store;

use Psr\SimpleCache\CacheInterface;
use Memcached;

/**
* PSR Cache interface implementation of memcache that needs to receive a sharding key
* and ensures all the values stored through this instance end up on the same MC server.
*/
class ShardedMemcachedStore implements CacheInterface
class ShardedMemcachedStore implements CacheStoreInterface
{
protected Memcached $mc;

protected string $shardingKey;

private const MC_SUCCESS = 0;

private const MC_NOT_FOUND = 16;

private const MC_VALID_CODES = [
Expand All @@ -28,14 +30,14 @@ public function __construct(Memcached $mc)
$this->mc = $mc;
}

public function setShardingKey(string $key) : void
public function setShardingKey(string $key): void
{
$this->shardingKey = $key;
}

protected function checkResultCode() : void
protected function checkResultCode(): void
{
if (!in_array($this->mc->getResultCode(), self::MC_VALID_CODES)) {
if (! in_array($this->mc->getResultCode(), self::MC_VALID_CODES)) {
throw new CacheStoreException('invalid MC return code', $this->mc->getResultCode());
}
}
Expand All @@ -44,20 +46,23 @@ public function get(string $key, mixed $default = null): mixed
{
$result = $this->mc->getByKey($this->shardingKey, $key);
$this->checkResultCode();

return $result;
}

public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool
{
$result = $this->mc->setByKey($this->shardingKey, $key, $value, $ttl ?? 0);
$this->checkResultCode();

return $result;
}

public function delete(string $key): bool
{
$result = $this->mc->deleteByKey($this->shardingKey, $key);
$this->checkResultCode();

return $result;
}

Expand All @@ -70,20 +75,23 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$result = $this->mc->getMultiByKey($this->shardingKey, $keys);
$this->checkResultCode();

return $result;
}

public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool
{
$result = $this->mc->setMultiByKey($this->shardingKey, $values, $ttl ?? 0);
$this->checkResultCode();

return $result;
}

public function deleteMultiple(iterable $keys): bool
{
$result = $this->mc->deleteMultiByKey($this->shardingKey, $keys);
$this->checkResultCode();

return $result;
}

Expand Down
Loading

0 comments on commit f3e1238

Please sign in to comment.