drupol/memoize

Memoize functions with a trait or by extending an object.

1.3.1 2017-09-22 19:02 UTC

README

Build Status Scrutinizer Code Quality Code Coverage StyleCI Latest Stable Version Total Downloads License

Memoize

Description

Memoize functions or methods.

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

Features

  • Provides a Trait, a Memoize object and a Memoizer helper,
  • Allows you to set a cache provider (PSR-16 compliant only),
  • Allows you to set a "TTL" (time to live).

Installation

composer require drupol/memoize

Usage

Using the trait:

include 'vendor/autoload.php';

use drupol\Memoize\MemoizeTrait;

class myObject {
    use MemoizeTrait;
}

$myObject = new myObject();

$closure = function($second = 5) {
    sleep($second);
    return uniqid();
};

echo $myObject->memoize($closure, [1]) . "\n"; // 59c41136b38e9
echo $myObject->memoize($closure, [1]) . "\n"; // 59c41136b38e9
echo $myObject->memoize($closure, [2]) . "\n"; // 59c41138c4765
echo $myObject->memoize($closure, [2]) . "\n"; // 59c41138c4765

Using the Memoize class:

include 'vendor/autoload.php';

class myObject extends \drupol\Memoize\Memoize {
}

$myObject = new myObject();

$closure = function($second = 5) {
    sleep($second);
    return uniqid();
};

echo $myObject->memoize($closure, [1]) . "\n"; // 59c411a2c6566
echo $myObject->memoize($closure, [1]) . "\n"; // 59c411a2c6566
echo $myObject->memoize($closure, [2]) . "\n"; // 59c411a4c8bb1
echo $myObject->memoize($closure, [2]) . "\n"; // 59c411a4c8bb1

Using the Memoizer class:

include 'vendor/autoload.php';

$closure = function($second = 5) {
    sleep($second);
    return uniqid();
};

$memoizer = new \drupol\Memoize\Memoizer($closure);

echo $memoizer(1) . "\n"; // 59c4123661459
echo $memoizer(1) . "\n"; // 59c4123661459
echo $memoizer(2) . "\n"; // 59c4123862a4e
echo $memoizer(2) . "\n"; // 59c4123862a4e

API

/**
 * Set the cache.
 *
 * @param \Psr\SimpleCache\CacheInterface $cache
 */
MemoizeTrait::setMemoizeCacheProvider(CacheInterface $cache);
/**
 * Get the cache.
 *
 * @return \Psr\SimpleCache\CacheInterface
 */
MemoizeTrait::getMemoizeCacheProvider();
/**
 * Clear the cache.
 */
MemoizeTrait::clearMemoizeCacheProvider();
/**
 * Memoize a closure.
 *
 * @param \Closure $func
 *   The closure.
 * @param array $parameters
 *   The closure's parameters.
 * @param null|int|DateInterval $ttl
 *   Optional. The TTL value of this item. If no value is sent and
 *   the driver supports TTL then the library may set a default value
 *   for it or let the driver take care of that.
 *
 * @return mixed|null
 *   The return of the closure.
 *
 * @throws \Psr\SimpleCache\InvalidArgumentException
 */
MemoizeTrait::memoize(\Closure $func, array $parameters = [], $ttl = null);

Technical notes

During the tests and investigations doing this library, I noticed that you must disable the serialization on the default ArrayCache() cache object.

This is why it is initialized as such: new ArrayCache(null, false);

If you use a cache object, make sure that you can disable the serialization or you won't be able to memoize methods that returns objects.

For example, the FilesystemCache() cache is unable to disable serialization and if you try to memoize functions like this example, it won't work, it's better to use the ArrayCache().

$function = function() {
  return new stdClass;
};

Contributing

Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)

Sponsors