Skip to content

Automatically memoize async function calls by caching function results, built on top of ReactPHP.

License

Notifications You must be signed in to change notification settings

clue/reactphp-memoize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clue/reactphp-memoize

Automatically memoize async function calls by caching function results, built on top of ReactPHP.

Table of contents

Support us

A clue·access project

This project is currently under active development, you're looking at a temporary placeholder repository.

The code is available in early access to my sponsors here: https://github.com/clue-access/reactphp-memoize

Do you sponsor me on GitHub? Thank you for supporting sustainable open-source, you're awesome! ❤️ Have fun with the code! 🎉

Seeing a 404 (Not Found)? Sounds like you're not in the early access group. Consider becoming a sponsor on GitHub for early access. Check out clue·access for more details.

This way, more people get a chance to take a look at the code before the public release.

Quickstart example

Once installed, you can use the following code to decorate a demo function and avoid sending unneeded HTTP requests:

<?php

require __DIR__ . '/vendor/autoload.php';

$browser = new React\Http\Browser();

$fetch = function (int $id) use ($browser) {
    return $browser->get(
        'http://httpbingo.org/status/' . $id
    )->then(function (Psr\Http\Message\ResponseInterface $response) {
        return $response->getStatusCode();
    });
}

$memoized = Clue\React\Memoize\memoize($fetch);

$memoized(200)->then('var_dump', 'printf');
$memoized(200)->then('var_dump', 'printf');

This example invokes the memoized function twice, but only sends a single HTTP request. The underlying function executes asynchronously and additional invocations will be locked until the first call is completed.

The return values will be cached for the respective function arguments. If you pass different arguments to each function call, it will invoke the underlying function multiple times.

Install

The recommended way to install this library is through Composer. New to Composer?

This project does not yet follow SemVer. This will install the latest supported version:

While in early access, you first have to manually change your composer.json to include these lines to access the supporters-only repository:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/clue-access/reactphp-memoize"
        }
    ]
}

Then install this package as usual:

$ composer require clue/reactphp-memoize:dev-main

This project aims to run on any platform and thus does not require any PHP extensions and supports running on PHP 7.3 through current PHP 8+.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ vendor/bin/phpunit

License

This project is released under the permissive MIT license.

Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.

About

Automatically memoize async function calls by caching function results, built on top of ReactPHP.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published