Allows caching of any method in code.

1.0.3 2020-05-15 14:25 UTC

This package is not auto-updated.

Last update: 2020-09-18 16:02:10 UTC


What is this?

This library allows caching the result of methods for a specified amount of time. This can be on a global level or per user session.


Using composer:

composer require "genesis/method-persister"


namespace ABC\Example;

use Genesis\MethodPersister;

// Ideally done using a DI library.
$centralStoragePath = '/tmp/cache/';
$persistenceRepository = new MethodPersister\PersistenceRepository($centralStoragePath);
$persister = new MethodPersister\Persister($persistenceRepository);


Consider your code like this:

$result = $myObject->myMethod($arg1, $arg2);

return $result;

The above can be rewritten with the cacher as follows yielding the same but faster results.

$result = $persister->persist($myObject, 'myMethod')
	->withParameters($arg1, $arg2)
	->overAPeriodOf('+10 seconds')

// Use result as normal
return $result;

This will persist the cache for 10 seconds from the first call, after that the cache will be refreshed. The default storage method is centralised but can be changed to distributed i.e session caching using the ->in() method.

$result = $persister->persist($myObject, 'userSpecificData')
    ->withParameters($arg1, $arg2)
    ->overAPeriodOf('+10 seconds')


This project is in its early stages, forks are welcome :)


Library is tested using phpunit. To run tests first install dependencies using composer then run command: