Download the PHP package germania-kg/cachecallable without Composer
On this page you can find all versions of the php package germania-kg/cachecallable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download germania-kg/cachecallable
More information about germania-kg/cachecallable
Files in germania-kg/cachecallable
Package cachecallable
Short Description Callable convenience wrapper around PSR-6 Cache Item Pools: Seamlessly create, return, and store your data.
License MIT
Informations about the package cachecallable
Germania KG · CacheCallable
Callable convenience wrapper around PSR-6 Cache Item Pools: Seamlessly creates, returns, and stores your data.
Caching business is pretty much always similar and can be outlined like this:
- Is caching enabled at all? If not, delete any according older entry first.
Create and return fresh content anyway, ending up here. - Does a given item exist? If so, return item content;
Otherwise, create, store and return content.
The CacheCallable class reduces these steps to a handy and customizable Callable.
Installation with Composer
Example
Although this example uses phpfastcache, you should be able to pass in any Cache Item Pool. Use your favourite PSR-3 Logger; this example will use the well-known Monolog.
A word on cache keys
According to the PSR-6 specs, cache keys should be limited to A-Z, a-z, 0-9, _, and .
to ensure maximum compatibilty. So if you pass in a PSR-6 Adapter from Symfony Cache component, class CacheCallable internally converts the given keys to a MD5 representation.
In case you'd like to provide a custom cache key creation, you may use the setCacheKeyCreator method whoch accepts any callable:
PHP-FIG: PSR-6: Caching Interface
Implementing libraries MUST support keys consisting of the characters A-Z, a-z, 0-9, _, and . in any order in UTF-8 encoding and a length of up to 64 characters. Implementing libraries MAY support additional characters and encodings or longer lengths, but must support at least that minimum.
Symfony docs: “Cache Item Keys and Values”
The key of a cache item […] should only contain letters (A-Z, a-z), numbers (0-9) and the _ and . symbols.
The Cache lifetime
Think of a webpage that turns out to be not cached during script runtime — after we set up the Cache wrapper. For this reason, the Cache wrapper constructor also accepts a LifeTimeInterface implementation with a getValue method:
The LifeTime class is a simple implementation of this interface. It additionally enables you to change the lifetime value during runtime. Since we passed it to our Cache constructor by reference, the Cache wrapper can decide after content creation wether to cache or not.
Create a Lifetime object
Usage with CacheCallable
Your Logger will now output something like this:
How to change lifetime during script runtime
After instantation, you may use the setValue method:
The LifeTime class is a simple implementation of this interface. It additionally enables you to change the lifetime value during runtime. Since we passed it to our Cache constructor by reference, the Cache wrapper can decide after content creation wether to cache or not.
Create a Lifetime object:
Set time value after instantation
Usage with CacheCallable
Your Logger will now output something like this:
How to override content creation
If you prefer singleton services, you may invoke the CacheCallable with a custom content creator parameter to override the default one:
Issues
The PSR-6 Caching Interface mock in CacheCallableTest could need an overhaul. Discuss on #issue 3
Development
Unit tests
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this: