Skip to content

Commit

Permalink
Merge pull request #14 from byjg/5.0
Browse files Browse the repository at this point in the history
Add TmpfsCacheEngine
  • Loading branch information
byjg authored Dec 18, 2024
2 parents c25e8a0 + fb26ecb commit cc010ce
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 104 deletions.
102 changes: 19 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,38 @@
[![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.
A multipurpose cache engine PSR-6 and PSR-16 implementation with several drivers.

## PSR-16

PSR-16 defines a Simple Cache interface with less verbosity than PSR-6. Below a list
of engines available in this library that is PSR-16 compliant:

| Class | Description |
|:----------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------|
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
| [\ByJG\AnyDataset\NoSql\Cache\KeyValueCacheEngine](https://github.com/byjg/php-anydataset-nosql) | Use S3-Like or ClouflareKV as a store for the cache (other repository) |
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
| [\ByJG\Cache\Psr16\ShmopCachedEngine](docs/class-shmop-cache-engine.md) | uses the shared memory area for cache |

To create a new Cache Instance just create the proper cache engine and use it:

```php
<?php
$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine();

// And use it:
if ($cache->has('key')) {
// Do the complex code to get the value to be cached
$object = callComplexCode();

// Save to cache
$cache->set('key', $object);
};
$object = $cache->get('key');
```

See more PSR-16 examples [here](docs/basic-usage-psr16-simplecache.md)
PSR-16 Getting Started: [here](docs/basic-usage-psr16-simplecache.md)

## PSR-6
## PSR-6

The PSR-6 implementation use the engines defined above. PSR-6 is more verbosity and
have an extra layer do get and set the cache values.
have an extra layer do get and set the cache values.

You can use one of the factory methods to create a instance of the CachePool implementation:

```php
<?php
$cachePool = \ByJG\Cache\Factory::createFilePool();
```

OR just create a new CachePool and pass to the constructor an instance of a PSR-16 compliant class:

```php
$cachePool = new CachePool(new FileSystemCacheEngine());
```

See more PSR-6 examples [here](docs/basic-usage-psr6-cachepool.md)

## List of Available Factory Commands
PSR-6 Getting Started: [here](docs/basic-usage-psr6-cachepool.md)

**Note: All parameters are optional**
## List of Cache Engines

| Engine | Factory Command |
|:--------------|:-------------------------------------------------------------------------------|
| No Cache | Factory::createNullPool($prefix, $bufferSize, $logger); |
| Aws S3 | See [Anydataset-NoSql](https://github.com/byjg/php-anydataset-nosql) component |
| Array | Factory::createArrayPool($bufferSize, $logger); |
| Cloudflare KV | See [Anydataset-NoSql](https://github.com/byjg/php-anydataset-nosql) component |
| File System | Factory::createFilePool($prefix, $bufferSize, $logger); |
| Memcached | Factory::createMemcachedPool($servers[], $bufferSize, $logger); |
| Session | Factory::createSessionPool($prefix, $bufferSize, $logger); |
| Redis | Factory::createRedisCacheEngine($server, $pwd, $bufferSize, $logger); |
| Shmop | Factory::createShmopPool($config[], $bufferSize, $logger); |
| Class | Description |
|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------|
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
| [\ByJG\AnyDataset\NoSql\Cache\KeyValueCacheEngine](https://github.com/byjg/php-anydataset-nosql) | Use S3-Like or ClouflareKV as a store for the cache (other repository) |
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
| [\ByJG\Cache\Psr16\TmpfsCacheEngine](docs/class-tmpfs-cache-engine.md) | Uses the Tmpfs as the cache engine |
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
| [\ByJG\Cache\Psr16\ShmopCachedEngine](docs/class-shmop-cache-engine.md) | uses the shared memory area for cache |

The Common parameters are:

- logger: A valid instance that implement the LoggerInterface defined by the PSR/LOG
- bufferSize: the Buffer of CachePool
- prefix: A prefix name to compose the KEY physically
- servers: An array of memcached servers. E.g.: `[ '127.0.0.1:11211' ]`
- config: Specific setup for shmop. E.g.: `[ 'max-size' => 524288, 'default-permission' => '0700' ]`

## Logging cache commands

Expand All @@ -96,25 +48,9 @@ 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.
You can use a PSR-11 compatible to retrieve the cache keys.

See more [here](docs/psr11-usage.md)

## Install

Expand Down
31 changes: 30 additions & 1 deletion docs/basic-usage-psr16-simplecache.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ Psr16 is a standard for cache in PHP with less verbosity than Psr6.

You can just instantiate the cache engine and use it as you can see below.

## Get an element from cache and set if not exists

```php
<?php
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();

$result = $cacheEngine->get($key);
if (!empty($result))
if (empty($result))
{
// Do the operations will be cached
// ....
Expand All @@ -22,3 +24,30 @@ if (!empty($result))
return $result;
```

## Check if an element exists in cache

```php
<?php
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
if ($cacheEngine->has($key)) {
// ...
}
```


## Delete an element from cache

```php
<?php
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
$cacheEngine->delete($key);
```

## Clear all cache

```php
<?php
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
$cacheEngine->clear();
```

28 changes: 28 additions & 0 deletions docs/class-tmpfs-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Class TmpfsCacheEngine

This class uses the Tmpfs as the cache engine.

## Defining the Path

The TmpfsCacheEngine allows to store the cache files in the `/dev/shm` tmpfs.


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\TmpfsCacheEngine($path, $prefix)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createTmpfsCachePool($path, $prefix, $bufferSize = 10)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\createTmpfsCachePool($path, $prefix));
```


20 changes: 20 additions & 0 deletions docs/psr11-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## 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.
40 changes: 20 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">

<php>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
<ini name="error_reporting" value="E_ALL"/>
</php>

<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 8 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ByJG\Cache\Psr16\ArrayCacheEngine;
use ByJG\Cache\Psr16\FileSystemCacheEngine;
use ByJG\Cache\Psr16\MemcachedEngine;
use ByJG\Cache\Psr16\TmpfsCacheEngine;
use ByJG\Cache\Psr16\NoCacheEngine;
use ByJG\Cache\Psr16\RedisCacheEngine;
use ByJG\Cache\Psr16\SessionCacheEngine;
Expand Down Expand Up @@ -69,4 +70,11 @@ public static function createRedisCacheEngine(?string $servers = null, ?string $
);
}

public static function createTmpfsCachePool(?LoggerInterface $logger = null): CachePool
{
return new CachePool(
new TmpfsCacheEngine($logger)
);
}

}
19 changes: 19 additions & 0 deletions src/Psr16/TmpfsCacheEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ByJG\Cache\Psr16;

use ByJG\Cache\CacheLockInterface;
use ByJG\Cache\Exception\InvalidArgumentException;
use DateInterval;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;

class TmpfsCacheEngine extends FileSystemCacheEngine
{

public function __construct(?LoggerInterface $logger = null)
{
parent::__construct('cache', '/dev/shm', $logger);
}
}
3 changes: 3 additions & 0 deletions tests/BaseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function CachePoolProvider()
],
'Redis' => [
new \ByJG\Cache\Psr16\RedisCacheEngine($redisCacheServer, $redisPassword)
],
'Memory' => [
new \ByJG\Cache\Psr16\TmpfsCacheEngine()
]
];
}
Expand Down

0 comments on commit cc010ce

Please sign in to comment.