Skip to content

Commit

Permalink
Merge pull request #8 from byjg/4.9
Browse files Browse the repository at this point in the history
Release 4.9 changes
  • Loading branch information
byjg authored Jan 4, 2024
2 parents 495b435 + ee5d1f9 commit b27df71
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 27 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ jobs:
strategy:
matrix:
php-version:
- "8.2"
- "8.1"
- "8.0"
- "7.4"
- "7.3"
- "7.2"
- "7.1"

# Service containers to run
services:
Expand All @@ -36,16 +34,16 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/phpunit --stderr

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

41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Cache Engine

[![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)
[![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)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/cache-engine-php/)
[![GitHub license](https://img.shields.io/github/license/byjg/cache-engine-php.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/cache-engine-php.svg)](https://github.com/byjg/cache-engine-php/releases/)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-cache-engine/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-cache-engine.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-cache-engine.svg)](https://github.com/byjg/php-cache-engine/releases/)


A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers.
Expand Down Expand Up @@ -95,12 +95,34 @@ You can add a PSR Log compatible to the constructor in order to get Log of the o

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

## Use a PSR-11 container to retrieve the cache keys

You can use a PSR-11 compatible to retrieve the cache keys. Once is defined, only the keys defined
in the PSR-11 will be used to cache.

```php
<?php
$fileCache = new \ByJG\Cache\Psr16\FileSystemCacheEngine()
$fileCache->withKeysFromContainer(new SomePsr11Implementation());
```

After the PSR-11 container is defined, when I run:

```php
$value = $fileCache->get('my-key');
```

The key `my-key` will be retrieved from the PSR-11 container and
the value retrieved will be used as the cache key.
If it does not exist in the PSR-11 container, an exception will be thrown.


## Install

Just type:

```
composer require "byjg/cache-engine=4.9.*"
composer require "byjg/cache-engine"
```


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

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

## Dependencies

```mermaid
flowchart TD
byjg/cache-engine --> psr/cache
byjg/cache-engine --> psr/log
byjg/cache-engine --> psr/simple-cache
byjg/cache-engine --> psr/container
```
----
[Open source ByJG](http://opensource.byjg.com)
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
}
},
"require": {
"php": ">=7.0",
"php": ">=7.4",
"psr/cache": "^1.0",
"psr/log": "^1.1",
"psr/simple-cache": "^1.0"
"psr/simple-cache": "^1.0",
"psr/container": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
Expand Down
10 changes: 8 additions & 2 deletions src/Psr16/ArrayCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\Cache\Psr16;

use DateInterval;
use Psr\Log\NullLogger;

class ArrayCacheEngine extends BaseCacheEngine
Expand Down Expand Up @@ -33,6 +34,7 @@ public function __construct($logger = null)
*/
public function has($key)
{
$key = $this->getKeyFromContainer($key);
if (isset($this->cache[$key])) {
if (isset($this->cache["$key.ttl"]) && time() >= $this->cache["$key.ttl"]) {
$this->delete($key);
Expand All @@ -54,6 +56,7 @@ public function has($key)
public function get($key, $default = null)
{
if ($this->has($key)) {
$key = $this->getKeyFromContainer($key);
$this->logger->info("[Array cache] Get '$key' from L1 Cache");
return $this->cache[$key];
} else {
Expand All @@ -67,17 +70,18 @@ public function get($key, $default = null)
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null)
{
$key = $this->getKeyFromContainer($key);

$this->logger->info("[Array cache] Set '$key' in L1 Cache");

$this->cache[$key] = $value;
Expand All @@ -101,6 +105,8 @@ public function clear()
*/
public function delete($key)
{
$key = $this->getKeyFromContainer($key);

unset($this->cache[$key]);
unset($this->cache["$key.ttl"]);
return true;
Expand Down
42 changes: 40 additions & 2 deletions src/Psr16/BaseCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use ByJG\Cache\CacheAvailabilityInterface;
use ByJG\Cache\Exception\InvalidArgumentException;
use DateInterval;
use DateTime;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;

abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
{
protected ?ContainerInterface $container;

/**
* @param $keys
* @param null $default
Expand Down Expand Up @@ -59,12 +64,45 @@ protected function addToNow($ttl)
return strtotime("+$ttl second");
}

if ($ttl instanceof \DateInterval) {
$now = new \DateTime();
if ($ttl instanceof DateInterval) {
$now = new DateTime();
$now->add($ttl);
return $now->getTimestamp();
}

return null;
}

protected function convertToSeconds($ttl)
{
if (empty($ttl) || is_numeric($ttl)) {
return $ttl;
}

if ($ttl instanceof DateInterval) {
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
}

throw new InvalidArgumentException('Invalid TTL');
}


protected function getKeyFromContainer($key)
{
if (empty($this->container)) {
return $key;
}

if (!$this->container->has($key)) {
throw new InvalidArgumentException("Key '$key' not found in container");
}

return $this->container->get($key);
}

public function withKeysFromContainer(?ContainerInterface $container)
{
$this->container = $container;
return $this;
}
}
6 changes: 4 additions & 2 deletions src/Psr16/FileSystemCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ByJG\Cache\Psr16;

use ByJG\Cache\CacheLockInterface;
use DateInterval;
use Exception;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -69,13 +70,12 @@ public function get($key, $default = null)
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null)
Expand Down Expand Up @@ -165,6 +165,8 @@ public function isAvailable()

protected function fixKey($key)
{
$key = $this->getKeyFromContainer($key);

return $this->path . '/'
. $this->prefix
. '-' . preg_replace("/[\/\\\]/", "#", $key)
Expand Down
8 changes: 6 additions & 2 deletions src/Psr16/MemcachedEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ByJG\Cache\Psr16;

use ByJG\Cache\Exception\StorageErrorException;
use DateInterval;
use Memcached;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -35,6 +36,7 @@ public function __construct($servers = null, $logger = null)
}

protected function fixKey($key) {
$key = $this->getKeyFromContainer($key);
return "cache-" . $key;
}

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

/**
* @param string $key The object Key
* @param object $value The object to be cached
* @param int $ttl The time to live in seconds of this objects
* @param mixed $value The object to be cached
* @param DateInterval|int|null $ttl The time to live in seconds of this objects
* @return bool If the object is successfully posted
* @throws StorageErrorException
*/
public function set($key, $value, $ttl = null)
{
$this->lazyLoadMemCachedServers();

$ttl = $this->convertToSeconds($ttl);

$this->memCached->set($this->fixKey($key), serialize($value), is_null($ttl) ? 0 : $ttl);
$this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode());
if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) {
Expand Down
4 changes: 4 additions & 0 deletions src/Psr16/NoCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class NoCacheEngine extends BaseCacheEngine implements CacheLockInterface
*/
public function get($key, $default = null)
{
$key = $this->getKeyFromContainer($key);
return $default;
}

Expand All @@ -24,6 +25,7 @@ public function get($key, $default = null)
*/
public function set($key, $value, $ttl = 0)
{
$key = $this->getKeyFromContainer($key);
return true;
}

Expand All @@ -33,6 +35,7 @@ public function set($key, $value, $ttl = 0)
*/
public function delete($key)
{
$key = $this->getKeyFromContainer($key);
return true;
}

Expand Down Expand Up @@ -83,6 +86,7 @@ public function clear()
*/
public function has($key)
{
$key = $this->getKeyFromContainer($key);
return false;
}
}
5 changes: 4 additions & 1 deletion src/Psr16/RedisCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function lazyLoadRedisServer()
}

protected function fixKey($key) {
$key = $this->getKeyFromContainer($key);
return "cache:$key";
}

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

$ttl = $this->convertToSeconds($ttl);

$this->redis->set($this->fixKey($key), serialize($value), $ttl);
$this->logger->info("[Redis Cache] Set '$key' result ");

Expand All @@ -98,7 +101,7 @@ public function clear()
{
$keys = $this->redis->keys('cache:*');
foreach ((array)$keys as $key) {
if (preg_match('/^cache\:(?<key>.*)/', $key, $matches)) {
if (preg_match('/^cache:(?<key>.*)/', $key, $matches)) {
$this->delete($matches['key']);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Psr16/SessionCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function checkSession()

protected function keyName($key)
{
$key = $this->getKeyFromContainer($key);
return $this->prefix . '-' . $key;
}

Expand Down
1 change: 1 addition & 0 deletions src/Psr16/ShmopCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct($config = [], $logger = null)

protected function getFilenameToken($key)
{
$key = $this->getKeyFromContainer($key);
return sys_get_temp_dir() . '/shmop-' . sha1($key) . '.cache';
}

Expand Down
3 changes: 3 additions & 0 deletions tests/BaseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ abstract class BaseCacheTest extends TestCase

protected function tearDown(): void
{
if (empty($this->cacheEngine)) {
return;
}
$this->cacheEngine->clear();
$this->cacheEngine = null;
}
Expand Down
Loading

0 comments on commit b27df71

Please sign in to comment.