2017 © Pedro Peláez
 

library apcu-memo

image

ksamborski/apcu-memo

  • Tuesday, October 18, 2016
  • by ksamborski
  • Repository
  • 1 Watchers
  • 0 Stars
  • 604 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

apcumemo

PHP library for memoization function results based on it's arguments., (*1)

Requirements

You need to have acpu extension installed for php >= 7.0.0, (*2)

Installation

composer require ksamborski/apcu-memo

Basic usage

Functional paradigm teaches us that pure functions are the way to go. They provide two main features:, (*3)

  • because their result depends only on their arguments they can be easly tested (you don't need any mocks or stubs)
  • because they have no side effects they can be easly cached

Consider this example:, (*4)


use APCuMemo\APCuMemo; function sumrange($a, $b) { return APCuMemo::memoize( function(...$_) use ($a, $b) { return array_reduce(range($a, $b), function ($product, $item) { return $product + $item; }, 1); }, [ 'ttl' => 5 ], "sumrange", $a, $b ); } $start = microtime(true); echo sumrange((int) $_GET['a'], (int) $_GET['b']); $elapsed = microtime(true) - $start; echo " " . ($elapsed * 1000) . " ms";

We have a simple function that sums integers from $a to $b. It can take some time but when we compute it for the first time then we can cache it. Notice the 'ttl' parameter. It is set to 5 seconds and means that if nobody asks us for the same range within 5 seconds it will forget the result. But every request for cached value will reset the ttl. That way we can have only mostly used values in cache., (*5)

Let's see it in action. For $a = 1 and $b = 1831233 it will return something like:, (*6)

1676708065762 283.94412994385 ms

And the followed requests will return:, (*7)

1676708065762 0.030040740966797 ms

The Versions

18/10 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=7.0.0
  • ext-apcu *

 

18/10 2016

0.4

0.4.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0
  • ext-apcu *

 

14/10 2016

0.3

0.3.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0
  • ext-apcu *

 

14/10 2016

0.2

0.2.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0
  • ext-apcu *

 

13/10 2016

0.1

0.1.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0.0
  • ext-apcu *