Skip to content

Commit cc010ce

Browse files
authored
Merge pull request #14 from byjg/5.0
Add TmpfsCacheEngine
2 parents c25e8a0 + fb26ecb commit cc010ce

File tree

8 files changed

+147
-104
lines changed

8 files changed

+147
-104
lines changed

README.md

Lines changed: 19 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,38 @@
77
[![GitHub release](https://img.shields.io/github/release/byjg/php-cache-engine.svg)](https://github.com/byjg/php-cache-engine/releases/)
88

99

10-
A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers.
10+
A multipurpose cache engine PSR-6 and PSR-16 implementation with several drivers.
1111

1212
## PSR-16
1313

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

17-
| Class | Description |
18-
|:----------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------|
19-
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
20-
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
21-
| [\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) |
22-
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
23-
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
24-
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
25-
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
26-
| [\ByJG\Cache\Psr16\ShmopCachedEngine](docs/class-shmop-cache-engine.md) | uses the shared memory area for cache |
27-
28-
To create a new Cache Instance just create the proper cache engine and use it:
29-
30-
```php
31-
<?php
32-
$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
33-
34-
// And use it:
35-
if ($cache->has('key')) {
36-
// Do the complex code to get the value to be cached
37-
$object = callComplexCode();
38-
39-
// Save to cache
40-
$cache->set('key', $object);
41-
};
42-
$object = $cache->get('key');
43-
```
44-
45-
See more PSR-16 examples [here](docs/basic-usage-psr16-simplecache.md)
17+
PSR-16 Getting Started: [here](docs/basic-usage-psr16-simplecache.md)
4618

47-
## PSR-6
19+
## PSR-6
4820

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

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

54-
```php
55-
<?php
56-
$cachePool = \ByJG\Cache\Factory::createFilePool();
57-
```
58-
59-
OR just create a new CachePool and pass to the constructor an instance of a PSR-16 compliant class:
60-
61-
```php
62-
$cachePool = new CachePool(new FileSystemCacheEngine());
63-
```
64-
65-
See more PSR-6 examples [here](docs/basic-usage-psr6-cachepool.md)
66-
67-
## List of Available Factory Commands
26+
PSR-6 Getting Started: [here](docs/basic-usage-psr6-cachepool.md)
6827

69-
**Note: All parameters are optional**
28+
## List of Cache Engines
7029

71-
| Engine | Factory Command |
72-
|:--------------|:-------------------------------------------------------------------------------|
73-
| No Cache | Factory::createNullPool($prefix, $bufferSize, $logger); |
74-
| Aws S3 | See [Anydataset-NoSql](https://github.com/byjg/php-anydataset-nosql) component |
75-
| Array | Factory::createArrayPool($bufferSize, $logger); |
76-
| Cloudflare KV | See [Anydataset-NoSql](https://github.com/byjg/php-anydataset-nosql) component |
77-
| File System | Factory::createFilePool($prefix, $bufferSize, $logger); |
78-
| Memcached | Factory::createMemcachedPool($servers[], $bufferSize, $logger); |
79-
| Session | Factory::createSessionPool($prefix, $bufferSize, $logger); |
80-
| Redis | Factory::createRedisCacheEngine($server, $pwd, $bufferSize, $logger); |
81-
| Shmop | Factory::createShmopPool($config[], $bufferSize, $logger); |
30+
| Class | Description |
31+
|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------|
32+
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
33+
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
34+
| [\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) |
35+
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
36+
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
37+
| [\ByJG\Cache\Psr16\TmpfsCacheEngine](docs/class-tmpfs-cache-engine.md) | Uses the Tmpfs as the cache engine |
38+
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
39+
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
40+
| [\ByJG\Cache\Psr16\ShmopCachedEngine](docs/class-shmop-cache-engine.md) | uses the shared memory area for cache |
8241

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

9143
## Logging cache commands
9244

@@ -96,25 +48,9 @@ See log examples [here](docs/setup-log-handler.md)
9648

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

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

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

11955
## Install
12056

docs/basic-usage-psr16-simplecache.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Psr16 is a standard for cache in PHP with less verbosity than Psr6.
44

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

7+
## Get an element from cache and set if not exists
8+
79
```php
810
<?php
911
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
1012

1113
$result = $cacheEngine->get($key);
12-
if (!empty($result))
14+
if (empty($result))
1315
{
1416
// Do the operations will be cached
1517
// ....
@@ -22,3 +24,30 @@ if (!empty($result))
2224
return $result;
2325
```
2426

27+
## Check if an element exists in cache
28+
29+
```php
30+
<?php
31+
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
32+
if ($cacheEngine->has($key)) {
33+
// ...
34+
}
35+
```
36+
37+
38+
## Delete an element from cache
39+
40+
```php
41+
<?php
42+
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
43+
$cacheEngine->delete($key);
44+
```
45+
46+
## Clear all cache
47+
48+
```php
49+
<?php
50+
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
51+
$cacheEngine->clear();
52+
```
53+

docs/class-tmpfs-cache-engine.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Class TmpfsCacheEngine
2+
3+
This class uses the Tmpfs as the cache engine.
4+
5+
## Defining the Path
6+
7+
The TmpfsCacheEngine allows to store the cache files in the `/dev/shm` tmpfs.
8+
9+
10+
## PSR-16 Constructor
11+
12+
```php
13+
$cache = new \ByJG\Cache\Psr16\TmpfsCacheEngine($path, $prefix)
14+
```
15+
16+
## PSR-6 Constructor
17+
18+
```php
19+
$cachePool = \ByJG\Cache\Factory::createTmpfsCachePool($path, $prefix, $bufferSize = 10)
20+
```
21+
22+
or
23+
24+
```php
25+
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\createTmpfsCachePool($path, $prefix));
26+
```
27+
28+

docs/psr11-usage.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Use a PSR-11 container to retrieve the cache keys
2+
3+
You can use a PSR-11 compatible to retrieve the cache keys. Once is defined, only the keys defined
4+
in the PSR-11 will be used to cache.
5+
6+
```php
7+
<?php
8+
$fileCache = new \ByJG\Cache\Psr16\FileSystemCacheEngine()
9+
$fileCache->withKeysFromContainer(new SomePsr11Implementation());
10+
```
11+
12+
After the PSR-11 container is defined, when I run:
13+
14+
```php
15+
$value = $fileCache->get('my-key');
16+
```
17+
18+
The key `my-key` will be retrieved from the PSR-11 container and
19+
the value retrieved will be used as the cache key.
20+
If it does not exist in the PSR-11 container, an exception will be thrown.

phpunit.xml.dist

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ To change this license header, choose License Headers in Project Properties.
44
To change this template file, choose Tools | Templates
55
and open the template in the editor.
66
-->
7-
87
<!-- see http://www.phpunit.de/wiki/Documentation -->
9-
<phpunit bootstrap="./vendor/autoload.php"
8+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
bootstrap="./vendor/autoload.php"
1010
colors="true"
1111
testdox="true"
1212
convertErrorsToExceptions="true"
1313
convertNoticesToExceptions="true"
1414
convertWarningsToExceptions="true"
1515
convertDeprecationsToExceptions="true"
16-
stopOnFailure="false">
16+
stopOnFailure="false"
17+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
18+
19+
<php>
20+
<ini name="display_errors" value="On"/>
21+
<ini name="display_startup_errors" value="On"/>
22+
<ini name="error_reporting" value="E_ALL"/>
23+
</php>
1724

18-
<php>
19-
<ini name="display_errors" value="On" />
20-
<ini name="display_startup_errors" value="On" />
21-
<ini name="error_reporting" value="E_ALL" />
22-
</php>
25+
<coverage>
26+
<include>
27+
<directory>./src</directory>
28+
</include>
29+
</coverage>
2330

24-
<filter>
25-
<whitelist>
26-
<directory>./src</directory>
27-
</whitelist>
28-
</filter>
29-
30-
<testsuites>
31-
<testsuite name="Test Suite">
32-
<directory>./tests/</directory>
33-
</testsuite>
34-
</testsuites>
35-
31+
<testsuites>
32+
<testsuite name="Test Suite">
33+
<directory>./tests/</directory>
34+
</testsuite>
35+
</testsuites>
3636
</phpunit>

src/Factory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ByJG\Cache\Psr16\ArrayCacheEngine;
66
use ByJG\Cache\Psr16\FileSystemCacheEngine;
77
use ByJG\Cache\Psr16\MemcachedEngine;
8+
use ByJG\Cache\Psr16\TmpfsCacheEngine;
89
use ByJG\Cache\Psr16\NoCacheEngine;
910
use ByJG\Cache\Psr16\RedisCacheEngine;
1011
use ByJG\Cache\Psr16\SessionCacheEngine;
@@ -69,4 +70,11 @@ public static function createRedisCacheEngine(?string $servers = null, ?string $
6970
);
7071
}
7172

73+
public static function createTmpfsCachePool(?LoggerInterface $logger = null): CachePool
74+
{
75+
return new CachePool(
76+
new TmpfsCacheEngine($logger)
77+
);
78+
}
79+
7280
}

src/Psr16/TmpfsCacheEngine.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ByJG\Cache\Psr16;
4+
5+
use ByJG\Cache\CacheLockInterface;
6+
use ByJG\Cache\Exception\InvalidArgumentException;
7+
use DateInterval;
8+
use Psr\Container\ContainerExceptionInterface;
9+
use Psr\Container\NotFoundExceptionInterface;
10+
use Psr\Log\LoggerInterface;
11+
12+
class TmpfsCacheEngine extends FileSystemCacheEngine
13+
{
14+
15+
public function __construct(?LoggerInterface $logger = null)
16+
{
17+
parent::__construct('cache', '/dev/shm', $logger);
18+
}
19+
}

tests/BaseCacheTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function CachePoolProvider()
4747
],
4848
'Redis' => [
4949
new \ByJG\Cache\Psr16\RedisCacheEngine($redisCacheServer, $redisPassword)
50+
],
51+
'Memory' => [
52+
new \ByJG\Cache\Psr16\TmpfsCacheEngine()
5053
]
5154
];
5255
}

0 commit comments

Comments
 (0)