byjg / cache-engine
A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers.
Fund package maintenance!
byjg
Installs: 50 035
Dependents: 8
Suggesters: 2
Security: 0
Stars: 10
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=8.1 <8.4
- psr/cache: ^1.0|^2.0|^3.0
- psr/container: ^1.0|^1.1|^2.0
- psr/log: ^1.0|^1.1|^2.0
- psr/simple-cache: ^1.0|^2.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.9
Suggests
- ext-memcached: *
- ext-redis: *
- ext-shmop: *
Provides
README
A multi-purpose 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:
To create a new Cache Instance just create the proper cache engine and use it:
<?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
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.
You can use one of the factory methods to create a instance of the CachePool implementation:
<?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:
$cachePool = new CachePool(new FileSystemCacheEngine());
See more PSR-6 examples here
List of Available Factory Commands
Note: All parameters are optional
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
You can add a PSR Log compatible to the constructor in order to get Log of the operations
See log examples here
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 $fileCache = new \ByJG\Cache\Psr16\FileSystemCacheEngine() $fileCache->withKeysFromContainer(new SomePsr11Implementation());
After the PSR-11 container is defined, when I run:
$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"
Running Unit Testes
vendor/bin/phpunit --stderr
Note: the parameter --stderr
after phpunit
is to permit run the tests on SessionCacheEngine.