Wallogit.com
2017 © Pedro Peláez
PHP library for memoization function results based on it's arguments., (*1)
You need to have acpu extension installed for php >= 7.0.0, (*2)
composer require ksamborski/apcu-memo
Functional paradigm teaches us that pure functions are the way to go. They provide two main features:, (*3)
Consider this example:, (*4)
use APCuMemo\APCuMemo; function sumrange($a, $b) { return APCuMemo::memoize( function(...$_) use ($a, $b) { return array_reduce(range($a, $b), function ($product, $item) { return $product + $item; }, 1); }, [ 'ttl' => 5 ], "sumrange", $a, $b ); } $start = microtime(true); echo sumrange((int) $_GET['a'], (int) $_GET['b']); $elapsed = microtime(true) - $start; echo " " . ($elapsed * 1000) . " ms";
We have a simple function that sums integers from $a to $b. It can take some time but when we compute it for the first time then we can cache it. Notice the 'ttl' parameter. It is set to 5 seconds and means that if nobody asks us for the same range within 5 seconds it will forget the result. But every request for cached value will reset the ttl. That way we can have only mostly used values in cache., (*5)
Let's see it in action. For $a = 1 and $b = 1831233 it will return something like:, (*6)
1676708065762 283.94412994385 ms
And the followed requests will return:, (*7)
1676708065762 0.030040740966797 ms