antevenio/memoize

In memory function memoizing, can be capped by total memory consumption and per function ttl.

0.0.16 2019-09-09 07:48 UTC

This package is auto-updated.

Last update: 2024-04-19 18:42:45 UTC


README

Latest Stable Version Total Downloads License Travis build Coverage Status Maintainability

Yet another in memory function memoizing library.

Features

  • Can specify a maximum number of items for the cache to hold.
  • Can place a TTL (time to live) per callable. (meaning that memoize will be returning the callable cached results until the TTL expires, in which case it will call the function again and generate a new cached result)
  • Can override the default callable argument cache indexing with a custom one. (you can reuse a cached callable even passing different arguments if you want to do so)
  • Caches thrown exceptions.

Behaviour

  • When reaching the maximum number of items in cache, it will evict the oldest (first cached) callable first.

Requirements

The following versions of PHP are supported.

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • PHP 7.2
  • PHP 7.3

Installation

composer require antevenio/memoize

Usage

Common code

<?php
require_once('./vendor/autoload.php');

use Antevenio\Memoize\Memoizable;
use Antevenio\Memoize\Memoize;
use Antevenio\Memoize\Cache;

class Toolbox
{
    public function multiply($argument)
    {
        echo "Called with {$argument}\n";

        return $argument * 2;
    }
    
    public function throwException($argument)
    {
        echo "Called with {$argument}\n";

        throw new \Exception($argument);
    }
}

$toolbox = new Toolbox();
$memoize = new Memoize((new Cache())->setEntryLimit(100));

Basic

for ($i = 0; $i < 10; $i++) {
    $result = $memoize->memoize(
        (new Memoizable([$toolbox, 'multiply'], [10]))->withTtl(5)
    );
    echo "Result: $result\n";
    sleep(1);
}

->>>

Called with 10
Result: 20
Result: 20
Result: 20
Result: 20
Result: 20
Called with 10
Result: 20
Result: 20
Result: 20
Result: 20
Result: 20

Changing arguments

for ($i = 0; $i < 10; $i++) {
    $result = $memoize->memoize(
        (new Memoizable([$toolbox, 'multiply'], [$i % 2]))->withTtl(5)
    );
    echo "Result: $result\n";
    sleep(1);
}

->>>

Called with 0
Result: 0
Called with 1
Result: 2
Result: 0
Result: 2
Result: 0
Result: 2
Called with 0
Result: 0
Called with 1
Result: 2
Result: 0
Result: 2

Custom indexing

for ($i = 0; $i < 10; $i++) {
    $result = $memoize->memoize(
        (new Memoizable([$toolbox, 'multiply'], [$i]))->withTtl(5)->withCustomIndex('myFixedIndex')
    );
    echo "Result: $result\n";
    sleep(1);
}

->>>

Called with 0
Result: 0
Result: 0
Result: 0
Result: 0
Result: 0
Called with 5
Result: 10
Result: 10
Result: 10
Result: 10
Result: 10

Exceptions

for ($i = 0; $i < 10; $i++) {
    $result = $memoize->memoize(
        (new Memoizable([$toolbox, 'throwException'], ['foo']))->withTtl(5)
    );
    echo "Result: $result\n";
    sleep(1);
}

->>>

Called with foo
Thrown exception foo
Thrown exception foo
Thrown exception foo
Thrown exception foo
Thrown exception foo
Called with foo
Thrown exception foo
Thrown exception foo
Thrown exception foo
Thrown exception foo
Thrown exception foo