Download the PHP package traderinteractive/memoize without Composer
On this page you can find all versions of the php package traderinteractive/memoize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download traderinteractive/memoize
More information about traderinteractive/memoize
Files in traderinteractive/memoize
Package memoize
Short Description A library for memoizing repeated function calls.
License MIT
Informations about the package memoize
Memoize
A PHP library for memoizing repeated function calls.
Requirements
This library requires PHP 7.0, or newer.
Installation
This package uses composer so you can just add
traderinteractive/memoize
as a dependency to your composer.json
file.
Memoization
Memoization is a way of optimizing a function that is called repeatedly by caching the results of a function call.
Memoization Providers
This library includes several built-in providers for memoization. Each one
implements the \TraderInteractive\Memoize\Memoize
interface:
The $compute
callable must not take any parameters - if you need parameters,
consider wrapping your function in a closure that pulls the required parameters
into scope. For example, given the function:
You could wrap this in a closure like so:
Alternatively, you could invert this and return the closure instead, like so:
Future versions of this library may add support for parameters, as it can be a common usecase (especially when it comes to recursive functions.
Also worth noting, is that you need to make sure you define your cache keys uniquely for anything using the memoizer.
Predis
The predis provider uses the predis library to
cache the results in Redis. It supports the $cacheTime
parameter so that
results can be recomputed after the time expires.
This memoizer can be used in a way that makes it persistent between processes rather than only caching computation for the current process.
Example
Memory
This is a standard in-memory memoizer. It does not support $cacheTime
at the
moment and only keeps the results around as long as the memoizer is in memory.
Example
None
This memoizer does not actually memoize anything - it always calls the
$compute
function. It is useful for testing and can also be used when you
disable memoization for debugging, etc. because you can swap your real memoizer
out for this one and everything will still work.