From 3d9cb2896a3b61616443a0be9fe6886add8561dd Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Wed, 18 Dec 2024 10:02:46 -0600 Subject: [PATCH 1/3] Add MemoryCacheEngine.php --- phpunit.xml.dist | 40 ++++++++++++++++----------------- src/Psr16/MemoryCacheEngine.php | 19 ++++++++++++++++ tests/BaseCacheTest.php | 3 +++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 src/Psr16/MemoryCacheEngine.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3621f92..13600e9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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. --> - - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + + + + + + - - - - - + + + ./src + + - - - ./src - - - - - - ./tests/ - - - + + + ./tests/ + + diff --git a/src/Psr16/MemoryCacheEngine.php b/src/Psr16/MemoryCacheEngine.php new file mode 100644 index 0000000..318937b --- /dev/null +++ b/src/Psr16/MemoryCacheEngine.php @@ -0,0 +1,19 @@ + [ new \ByJG\Cache\Psr16\RedisCacheEngine($redisCacheServer, $redisPassword) + ], + 'Memory' => [ + new \ByJG\Cache\Psr16\MemoryCacheEngine() ] ]; } From 14caeba51b6bf2fd0b657f38d57bc0a595ee24c2 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Wed, 18 Dec 2024 10:49:22 -0600 Subject: [PATCH 2/3] Add TmpfsCacheEngine.php --- README.md | 102 ++++-------------- docs/basic-usage-psr16-simplecache.md | 31 +++++- docs/class-tmpfs-cache-engine.md | 28 +++++ docs/psr11-usage.md | 20 ++++ src/Factory.php | 8 ++ ...ryCacheEngine.php => TmpfsCacheEngine.php} | 2 +- tests/BaseCacheTest.php | 2 +- 7 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 docs/class-tmpfs-cache-engine.md create mode 100644 docs/psr11-usage.md rename src/Psr16/{MemoryCacheEngine.php => TmpfsCacheEngine.php} (88%) diff --git a/README.md b/README.md index ceb456a..4d9dcc8 100644 --- a/README.md +++ b/README.md @@ -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 -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 - 524288, 'default-permission' => '0700' ]` ## Logging cache commands @@ -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 -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 diff --git a/docs/basic-usage-psr16-simplecache.md b/docs/basic-usage-psr16-simplecache.md index eb23982..9311580 100644 --- a/docs/basic-usage-psr16-simplecache.md +++ b/docs/basic-usage-psr16-simplecache.md @@ -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 get($key); -if (!empty($result)) +if (empty($result)) { // Do the operations will be cached // .... @@ -22,3 +24,30 @@ if (!empty($result)) return $result; ``` +## Check if an element exists in cache + +```php +has($key)) { + // ... +} +``` + + +## Delete an element from cache + +```php +delete($key); +``` + +## Clear all cache + +```php +clear(); +``` + diff --git a/docs/class-tmpfs-cache-engine.md b/docs/class-tmpfs-cache-engine.md new file mode 100644 index 0000000..157bbce --- /dev/null +++ b/docs/class-tmpfs-cache-engine.md @@ -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)); +``` + + diff --git a/docs/psr11-usage.md b/docs/psr11-usage.md new file mode 100644 index 0000000..304b039 --- /dev/null +++ b/docs/psr11-usage.md @@ -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 +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. diff --git a/src/Factory.php b/src/Factory.php index 6e1858d..e381b9d 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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; @@ -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) + ); + } + } diff --git a/src/Psr16/MemoryCacheEngine.php b/src/Psr16/TmpfsCacheEngine.php similarity index 88% rename from src/Psr16/MemoryCacheEngine.php rename to src/Psr16/TmpfsCacheEngine.php index 318937b..4476fbb 100644 --- a/src/Psr16/MemoryCacheEngine.php +++ b/src/Psr16/TmpfsCacheEngine.php @@ -9,7 +9,7 @@ use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; -class MemoryCacheEngine extends FileSystemCacheEngine +class TmpfsCacheEngine extends FileSystemCacheEngine { public function __construct(?LoggerInterface $logger = null) diff --git a/tests/BaseCacheTest.php b/tests/BaseCacheTest.php index 27bb57f..956ee61 100644 --- a/tests/BaseCacheTest.php +++ b/tests/BaseCacheTest.php @@ -49,7 +49,7 @@ public function CachePoolProvider() new \ByJG\Cache\Psr16\RedisCacheEngine($redisCacheServer, $redisPassword) ], 'Memory' => [ - new \ByJG\Cache\Psr16\MemoryCacheEngine() + new \ByJG\Cache\Psr16\TmpfsCacheEngine() ] ]; } From 24a4325345b2dbd4c279cf1e948cab0b3310006a Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Wed, 18 Dec 2024 10:51:29 -0600 Subject: [PATCH 3/3] Add TmpfsCacheEngine.php --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d9dcc8..4369e56 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ PSR-6 Getting Started: [here](docs/basic-usage-psr6-cachepool.md) | [\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-memcached-engine.md) | Uses the Tmpfs 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 |