Skip to content

Commit 529a372

Browse files
committed
Add PSR-11 as key store.
1 parent d7948a7 commit 529a372

13 files changed

+143
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ You can add a PSR Log compatible to the constructor in order to get Log of the o
9595

9696
See log examples [here](docs/setup-log-handler.md)
9797

98+
## Use a PSR-11 container to retrieve the cache keys
99+
100+
You can use a PSR-11 compatible to retrieve the cache keys. Once is defined, only the keys defined
101+
in the PSR-11 will be used to cache.
102+
103+
```php
104+
<?php
105+
$fileCache = new \ByJG\Cache\Psr16\FileSystemCacheEngine()
106+
$fileCache->withKeysFromContainer(new SomePsr11Implementation());
107+
```
108+
109+
After the PSR-11 container is defined, when I run:
110+
111+
```php
112+
$value = $fileCache->get('my-key');
113+
```
114+
115+
The key `my-key` will be retrieved from the PSR-11 container and
116+
the value retrieved will be used as the cache key.
117+
If it does not exist in the PSR-11 container, an exception will be thrown.
118+
119+
98120
## Install
99121

100122
Just type:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"php": ">=7.4",
1111
"psr/cache": "^1.0",
1212
"psr/log": "^1.1",
13-
"psr/simple-cache": "^1.0"
13+
"psr/simple-cache": "^1.0",
14+
"psr/container": "^2.0"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"

src/Psr16/ArrayCacheEngine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct($logger = null)
3434
*/
3535
public function has($key)
3636
{
37+
$key = $this->getKeyFromContainer($key);
3738
if (isset($this->cache[$key])) {
3839
if (isset($this->cache["$key.ttl"]) && time() >= $this->cache["$key.ttl"]) {
3940
$this->delete($key);
@@ -55,6 +56,7 @@ public function has($key)
5556
public function get($key, $default = null)
5657
{
5758
if ($this->has($key)) {
59+
$key = $this->getKeyFromContainer($key);
5860
$this->logger->info("[Array cache] Get '$key' from L1 Cache");
5961
return $this->cache[$key];
6062
} else {
@@ -78,6 +80,8 @@ public function get($key, $default = null)
7880
*/
7981
public function set($key, $value, $ttl = null)
8082
{
83+
$key = $this->getKeyFromContainer($key);
84+
8185
$this->logger->info("[Array cache] Set '$key' in L1 Cache");
8286

8387
$this->cache[$key] = $value;
@@ -101,6 +105,8 @@ public function clear()
101105
*/
102106
public function delete($key)
103107
{
108+
$key = $this->getKeyFromContainer($key);
109+
104110
unset($this->cache[$key]);
105111
unset($this->cache["$key.ttl"]);
106112
return true;

src/Psr16/BaseCacheEngine.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use ByJG\Cache\Exception\InvalidArgumentException;
77
use DateInterval;
88
use DateTime;
9+
use Psr\Container\ContainerInterface;
910
use Psr\SimpleCache\CacheInterface;
1011

1112
abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
1213
{
14+
protected ?ContainerInterface $container;
15+
1316
/**
1417
* @param $keys
1518
* @param null $default
@@ -82,4 +85,24 @@ protected function convertToSeconds($ttl)
8285

8386
throw new InvalidArgumentException('Invalid TTL');
8487
}
88+
89+
90+
protected function getKeyFromContainer($key)
91+
{
92+
if (empty($this->container)) {
93+
return $key;
94+
}
95+
96+
if (!$this->container->has($key)) {
97+
throw new InvalidArgumentException("Key '$key' not found in container");
98+
}
99+
100+
return $this->container->get($key);
101+
}
102+
103+
public function withKeysFromContainer(?ContainerInterface $container)
104+
{
105+
$this->container = $container;
106+
return $this;
107+
}
85108
}

src/Psr16/FileSystemCacheEngine.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public function isAvailable()
165165

166166
protected function fixKey($key)
167167
{
168+
$key = $this->getKeyFromContainer($key);
169+
168170
return $this->path . '/'
169171
. $this->prefix
170172
. '-' . preg_replace("/[\/\\\]/", "#", $key)

src/Psr16/MemcachedEngine.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct($servers = null, $logger = null)
3636
}
3737

3838
protected function fixKey($key) {
39+
$key = $this->getKeyFromContainer($key);
3940
return "cache-" . $key;
4041
}
4142

src/Psr16/NoCacheEngine.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class NoCacheEngine extends BaseCacheEngine implements CacheLockInterface
1313
*/
1414
public function get($key, $default = null)
1515
{
16+
$key = $this->getKeyFromContainer($key);
1617
return $default;
1718
}
1819

@@ -24,6 +25,7 @@ public function get($key, $default = null)
2425
*/
2526
public function set($key, $value, $ttl = 0)
2627
{
28+
$key = $this->getKeyFromContainer($key);
2729
return true;
2830
}
2931

@@ -33,6 +35,7 @@ public function set($key, $value, $ttl = 0)
3335
*/
3436
public function delete($key)
3537
{
38+
$key = $this->getKeyFromContainer($key);
3639
return true;
3740
}
3841

@@ -83,6 +86,7 @@ public function clear()
8386
*/
8487
public function has($key)
8588
{
89+
$key = $this->getKeyFromContainer($key);
8690
return false;
8791
}
8892
}

src/Psr16/RedisCacheEngine.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected function lazyLoadRedisServer()
5151
}
5252

5353
protected function fixKey($key) {
54+
$key = $this->getKeyFromContainer($key);
5455
return "cache:$key";
5556
}
5657

src/Psr16/SessionCacheEngine.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function checkSession()
2727

2828
protected function keyName($key)
2929
{
30+
$key = $this->getKeyFromContainer($key);
3031
return $this->prefix . '-' . $key;
3132
}
3233

src/Psr16/ShmopCacheEngine.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct($config = [], $logger = null)
4646

4747
protected function getFilenameToken($key)
4848
{
49+
$key = $this->getKeyFromContainer($key);
4950
return sys_get_temp_dir() . '/shmop-' . sha1($key) . '.cache';
5051
}
5152

0 commit comments

Comments
 (0)