andydune / object-cache-proxy
Allow cache results for execution object method. Transparent to working code.
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.7.15
- symfony/cache: ^3
This package is auto-updated.
Last update: 2024-11-21 21:21:32 UTC
README
Allow cache results for execution object method. Transparent to working code.
Installation
Installation with composer:
composer require andydune/object-cache-proxy
Or if composer didn't install globally:
php composer.phar require andydune/object-cache-proxy
Or edit your composer.json
:
"require" : {
"andydune/object-cache-proxy": "^1"
}
And execute command:
php composer.phar update
Problem
Here is instance of any class. We use setters for inject data and finally execute the last method for retrieve data. We do it often with narrow range of input data. How can we simple to change it for speedup?
$instance = new Adventures(); // any class // Use some setters $instance->setDirection('up'); $instance->setSpeed('low'); $instance->setLimit(13); // Next is results $ways = $instance->get();
We can use ObjectCacheProxy
to cache it with minimum work code change.
use AndyDune\ObjectCacheProxy\ObjectCacheProxy; use Symfony\Component\Cache\Simple\FilesystemCache; $instanceOrigin = new \Adventures(); // any class $instance = new ObjectCacheProxy(new FilesystemCache()); // inject object and method name witch results will be cached. $instance->setObject($instanceOrigin, 'get'); // Use some setters $instance->setDirection('up'); $instance->setSpeed('low'); $instance->setLimit(13); // Next is results $ways = $instance->get();
ObjectCacheProxy
gets work class instance and intercept methods are called to it.
It accumulates setter methods and parameters as cache key and executes them at ones before execution method get
.