|
| 1 | +# Memoize |
| 2 | +A PHP library for memoizing repeated function calls. |
| 3 | + |
| 4 | +## Requirements |
| 5 | +This library requires PHP 5.3, or newer. |
| 6 | + |
| 7 | +## Installation |
| 8 | +This package uses [composer](https://getcomposer.org) so you can just add |
| 9 | +`dominionenterprises/memoize` as a dependency to your `composer.json` file. |
| 10 | + |
| 11 | +## Memoization |
| 12 | +[Memoization](http://en.wikipedia.org/wiki/Memoization) is a way of optimizing a function that is called repeatedly by caching the results of a function call. |
| 13 | + |
| 14 | +## Memoization Providers |
| 15 | +This library includes several built-in providers for memoization. Each one |
| 16 | +implements the `\DominionEnterprises\Memoize\Memoize` interface: |
| 17 | +```php |
| 18 | +interface Memoize |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Gets the value stored in the cache or uses the passed function to |
| 22 | + * compute the value and save to cache. |
| 23 | + * |
| 24 | + * @param string $key The key to fetch |
| 25 | + * @param callable $compute A function to run if the value was not cached |
| 26 | + * that will return the result. |
| 27 | + * @param int $cacheTime The number of seconds to cache the response for, |
| 28 | + * or null to not expire it ever. |
| 29 | + * @return mixed The data requested, optionally pulled from cache |
| 30 | + */ |
| 31 | + public function memoizeCallable($key, $compute, $cacheTime = null); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The `$compute` callable must not take any parameters - if you need parameters, |
| 36 | +consider wrapping your function in a closure that pulls the required parameters |
| 37 | +into scope. For example, given the function: |
| 38 | +```php |
| 39 | +$getUser = function($database, $userId) { |
| 40 | + $query = $database->select('*')->from('user')->where(['id' => $userId]); |
| 41 | + return $query->fetchOne(); |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +You could wrap this in a closure like so: |
| 46 | +```php |
| 47 | +$getLoggedInUser = function() use($database, $loggedInUserId, $getUser) { |
| 48 | + return $getUser($database, $loggedInUserId); |
| 49 | +}; |
| 50 | + |
| 51 | +$memoize->memoizeCallable("getUser-{$loggedInUserId}", $getLoggedInUser); |
| 52 | +``` |
| 53 | + |
| 54 | +Future versions of this library may add support for parameters, as it can be a |
| 55 | +common usecase (especially when it comes to recursive functions. |
| 56 | + |
| 57 | +Also worth noting, is that you need to make sure you define your cache keys |
| 58 | +uniquely for anything using the memoizer. |
| 59 | + |
| 60 | +### Predis |
| 61 | +The predis provider uses the [predis](https://github.com/nrk/predis) library to |
| 62 | +cache the results in Redis. It supports the `$cacheTime` parameter so that |
| 63 | +results can be recomputed after the time expires. |
| 64 | + |
| 65 | +This memoizer can be used in a way that makes it persistent between processes |
| 66 | +rather than only caching computation for the current process. |
| 67 | + |
| 68 | +#### Example |
| 69 | +```php |
| 70 | +$predis = new \Predis\Client($redisUrl); |
| 71 | +$memoize = new \DominionEnterprises\Memoize\Predis($predis); |
| 72 | + |
| 73 | +$compute = function() { |
| 74 | + // Perform some long operation that you want to memoize |
| 75 | +}; |
| 76 | + |
| 77 | +// Cache he results of $compute for 1 hour. |
| 78 | +$result = $memoize->memoizeCallable('myLongOperation', $compute, 3600); |
| 79 | +``` |
| 80 | + |
| 81 | +### Memory |
| 82 | +This is a standard in-memory memoizer. It does not support `$cacheTime` at the |
| 83 | +moment and only keeps the results around as long as the memoizer is in memory. |
| 84 | + |
| 85 | +#### Example |
| 86 | +```php |
| 87 | +$memoize = new \DominionEnterprises\Memoize\Memory(); |
| 88 | + |
| 89 | +$compute = function() { |
| 90 | + // Perform some long operation that you want to memoize |
| 91 | +}; |
| 92 | + |
| 93 | +$result = $memoize->memoizeCallable('myLongOperation', $compute); |
| 94 | +``` |
| 95 | + |
| 96 | +### Null |
| 97 | +This memoizer does not actually memoize anything - it always calls the |
| 98 | +`$compute` function. It is useful for testing and can also be used when you |
| 99 | +disable memoization for debugging, etc. because you can swap your real memoizer |
| 100 | +out for this one and everything will still work. |
| 101 | + |
| 102 | +#### Example |
| 103 | +```php |
| 104 | +$memoize = new \DominionEnterprises\Memoize\Null(); |
| 105 | + |
| 106 | +$compute = function() { |
| 107 | + // Perform some long operation that you want to memoize |
| 108 | +}; |
| 109 | + |
| 110 | +// This will never actually memoize the results - they will be recomputed every |
| 111 | +// time. |
| 112 | +$result = $memoize->memoizeCallable('myLongOperation', $compute); |
| 113 | +``` |
0 commit comments