Skip to content

Commit b27df71

Browse files
authored
Merge pull request #8 from byjg/4.9
Release 4.9 changes
2 parents 495b435 + ee5d1f9 commit b27df71

14 files changed

+196
-27
lines changed

.github/workflows/phpunit.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19+
- "8.2"
1920
- "8.1"
2021
- "8.0"
2122
- "7.4"
22-
- "7.3"
23-
- "7.2"
24-
- "7.1"
2523

2624
# Service containers to run
2725
services:
@@ -36,16 +34,16 @@ jobs:
3634
--health-retries 5
3735
3836
steps:
39-
- uses: actions/checkout@v3
37+
- uses: actions/checkout@v4
4038
- run: composer install
4139
- run: ./vendor/bin/phpunit --stderr
4240

4341
Documentation:
44-
runs-on: 'ubuntu-latest'
45-
needs: Build
4642
if: github.ref == 'refs/heads/master'
47-
env:
48-
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
49-
steps:
50-
- uses: actions/checkout@v3
51-
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php cache-engine-php
43+
needs: Build
44+
uses: byjg/byjg.github.io/.github/workflows/add-doc.yaml@master
45+
with:
46+
folder: php
47+
project: ${{ github.event.repository.name }}
48+
secrets: inherit
49+

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Cache Engine
22

3-
[![Build Status](https://github.com/byjg/cache-engine-php/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/cache-engine-php/actions/workflows/phpunit.yml)
3+
[![Build Status](https://github.com/byjg/php-cache-engine/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-cache-engine/actions/workflows/phpunit.yml)
44
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
5-
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/cache-engine-php/)
6-
[![GitHub license](https://img.shields.io/github/license/byjg/cache-engine-php.svg)](https://opensource.byjg.com/opensource/licensing.html)
7-
[![GitHub release](https://img.shields.io/github/release/byjg/cache-engine-php.svg)](https://github.com/byjg/cache-engine-php/releases/)
5+
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-cache-engine/)
6+
[![GitHub license](https://img.shields.io/github/license/byjg/php-cache-engine.svg)](https://opensource.byjg.com/opensource/licensing.html)
7+
[![GitHub release](https://img.shields.io/github/release/byjg/php-cache-engine.svg)](https://github.com/byjg/php-cache-engine/releases/)
88

99

1010
A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers.
@@ -95,12 +95,34 @@ 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:
101123

102124
```
103-
composer require "byjg/cache-engine=4.9.*"
125+
composer require "byjg/cache-engine"
104126
```
105127

106128

@@ -112,5 +134,14 @@ vendor/bin/phpunit --stderr
112134

113135
**Note:** the parameter `--stderr` after `phpunit` is to permit run the tests on SessionCacheEngine.
114136

137+
## Dependencies
138+
139+
```mermaid
140+
flowchart TD
141+
byjg/cache-engine --> psr/cache
142+
byjg/cache-engine --> psr/log
143+
byjg/cache-engine --> psr/simple-cache
144+
byjg/cache-engine --> psr/container
145+
```
115146
----
116147
[Open source ByJG](http://opensource.byjg.com)

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
}
88
},
99
"require": {
10-
"php": ">=7.0",
10+
"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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ByJG\Cache\Psr16;
44

5+
use DateInterval;
56
use Psr\Log\NullLogger;
67

78
class ArrayCacheEngine extends BaseCacheEngine
@@ -33,6 +34,7 @@ public function __construct($logger = null)
3334
*/
3435
public function has($key)
3536
{
37+
$key = $this->getKeyFromContainer($key);
3638
if (isset($this->cache[$key])) {
3739
if (isset($this->cache["$key.ttl"]) && time() >= $this->cache["$key.ttl"]) {
3840
$this->delete($key);
@@ -54,6 +56,7 @@ public function has($key)
5456
public function get($key, $default = null)
5557
{
5658
if ($this->has($key)) {
59+
$key = $this->getKeyFromContainer($key);
5760
$this->logger->info("[Array cache] Get '$key' from L1 Cache");
5861
return $this->cache[$key];
5962
} else {
@@ -67,17 +70,18 @@ public function get($key, $default = null)
6770
*
6871
* @param string $key The key of the item to store.
6972
* @param mixed $value The value of the item to store, must be serializable.
70-
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
73+
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
7174
* the driver supports TTL then the library may set a default value
7275
* for it or let the driver take care of that.
7376
*
7477
* @return bool True on success and false on failure.
7578
*
76-
* @throws \Psr\SimpleCache\InvalidArgumentException
7779
* MUST be thrown if the $key string is not a legal value.
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: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
use ByJG\Cache\CacheAvailabilityInterface;
66
use ByJG\Cache\Exception\InvalidArgumentException;
7+
use DateInterval;
8+
use DateTime;
9+
use Psr\Container\ContainerInterface;
710
use Psr\SimpleCache\CacheInterface;
811

912
abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
1013
{
14+
protected ?ContainerInterface $container;
15+
1116
/**
1217
* @param $keys
1318
* @param null $default
@@ -59,12 +64,45 @@ protected function addToNow($ttl)
5964
return strtotime("+$ttl second");
6065
}
6166

62-
if ($ttl instanceof \DateInterval) {
63-
$now = new \DateTime();
67+
if ($ttl instanceof DateInterval) {
68+
$now = new DateTime();
6469
$now->add($ttl);
6570
return $now->getTimestamp();
6671
}
6772

6873
return null;
6974
}
75+
76+
protected function convertToSeconds($ttl)
77+
{
78+
if (empty($ttl) || is_numeric($ttl)) {
79+
return $ttl;
80+
}
81+
82+
if ($ttl instanceof DateInterval) {
83+
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
84+
}
85+
86+
throw new InvalidArgumentException('Invalid TTL');
87+
}
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+
}
70108
}

src/Psr16/FileSystemCacheEngine.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ByJG\Cache\Psr16;
44

55
use ByJG\Cache\CacheLockInterface;
6+
use DateInterval;
67
use Exception;
78
use Psr\Log\NullLogger;
89

@@ -69,13 +70,12 @@ public function get($key, $default = null)
6970
*
7071
* @param string $key The key of the item to store.
7172
* @param mixed $value The value of the item to store, must be serializable.
72-
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
73+
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
7374
* the driver supports TTL then the library may set a default value
7475
* for it or let the driver take care of that.
7576
*
7677
* @return bool True on success and false on failure.
7778
*
78-
* @throws \Psr\SimpleCache\InvalidArgumentException
7979
* MUST be thrown if the $key string is not a legal value.
8080
*/
8181
public function set($key, $value, $ttl = null)
@@ -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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ByJG\Cache\Psr16;
44

55
use ByJG\Cache\Exception\StorageErrorException;
6+
use DateInterval;
67
use Memcached;
78
use Psr\Log\NullLogger;
89

@@ -35,6 +36,7 @@ public function __construct($servers = null, $logger = null)
3536
}
3637

3738
protected function fixKey($key) {
39+
$key = $this->getKeyFromContainer($key);
3840
return "cache-" . $key;
3941
}
4042

@@ -78,15 +80,17 @@ public function get($key, $default = null)
7880

7981
/**
8082
* @param string $key The object Key
81-
* @param object $value The object to be cached
82-
* @param int $ttl The time to live in seconds of this objects
83+
* @param mixed $value The object to be cached
84+
* @param DateInterval|int|null $ttl The time to live in seconds of this objects
8385
* @return bool If the object is successfully posted
8486
* @throws StorageErrorException
8587
*/
8688
public function set($key, $value, $ttl = null)
8789
{
8890
$this->lazyLoadMemCachedServers();
8991

92+
$ttl = $this->convertToSeconds($ttl);
93+
9094
$this->memCached->set($this->fixKey($key), serialize($value), is_null($ttl) ? 0 : $ttl);
9195
$this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode());
9296
if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) {

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: 4 additions & 1 deletion
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

@@ -79,6 +80,8 @@ public function set($key, $value, $ttl = null)
7980
{
8081
$this->lazyLoadRedisServer();
8182

83+
$ttl = $this->convertToSeconds($ttl);
84+
8285
$this->redis->set($this->fixKey($key), serialize($value), $ttl);
8386
$this->logger->info("[Redis Cache] Set '$key' result ");
8487

@@ -98,7 +101,7 @@ public function clear()
98101
{
99102
$keys = $this->redis->keys('cache:*');
100103
foreach ((array)$keys as $key) {
101-
if (preg_match('/^cache\:(?<key>.*)/', $key, $matches)) {
104+
if (preg_match('/^cache:(?<key>.*)/', $key, $matches)) {
102105
$this->delete($matches['key']);
103106
}
104107
}

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

0 commit comments

Comments
 (0)