Git Product home page Git Product logo

memoize-php's Introduction

Memoize

A PHP library for memoizing repeated function calls.

Build Status Scrutinizer Code Quality Code Coverage

Latest Stable Version Total Downloads License

Requirements

This library requires PHP 5.4, or newer.

Installation

This package uses composer so you can just add dominionenterprises/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 \DominionEnterprises\Memoize\Memoize interface:

interface Memoize
{
    /**
     * Gets the value stored in the cache or uses the passed function to
     * compute the value and save to cache.
     *
     * @param string $key The key to fetch
     * @param callable $compute A function to run if the value was not cached
     *     that will return the result.
     * @param int $cacheTime The number of seconds to cache the response for,
     *     or null to not expire it ever.
     * @return mixed The data requested, optionally pulled from cache
     */
    public function memoizeCallable($key, $compute, $cacheTime = null);
}

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:

$getUser = function($database, $userId) {
  $query = $database->select('*')->from('user')->where(['id' => $userId]);
  return $query->fetchOne();
};

You could wrap this in a closure like so:

$getLoggedInUser = function() use($database, $loggedInUserId, $getUser) {
    return $getUser($database, $loggedInUserId);
};

$memoize->memoizeCallable("getUser-{$loggedInUserId}", $getLoggedInUser);

Alternatively, you could invert this and return the closure instead, like so:

$getUserLocator = function($database, $userId) use($getUser) {
    return function() use($database, $userId, $getUser) {
        return $getUser($database, $userId);
    };
};

$getLoggedInUser = $getUserLocator($database, $loggedInUserId);
$memoize->memoizeCallable("getUser-{$loggedInUserId}", $getLoggedInUser);

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

$predis = new \Predis\Client($redisUrl);
$memoize = new \DominionEnterprises\Memoize\Predis($predis);

$compute = function() {
    // Perform some long operation that you want to memoize
};

// Cache he results of $compute for 1 hour.
$result = $memoize->memoizeCallable('myLongOperation', $compute, 3600);

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

$memoize = new \DominionEnterprises\Memoize\Memory();

$compute = function() {
    // Perform some long operation that you want to memoize
};

$result = $memoize->memoizeCallable('myLongOperation', $compute);

Null

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.

Example

$memoize = new \DominionEnterprises\Memoize\Null();

$compute = function() {
    // Perform some long operation that you want to memoize
};

// This will never actually memoize the results - they will be recomputed every
// time.
$result = $memoize->memoizeCallable('myLongOperation', $compute);

memoize-php's People

Contributors

nubs avatar raybot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.