2017 © Pedro Peláez
 

library memorize

Simple cache for pure functions

image

solodkiy/memorize

Simple cache for pure functions

  • Tuesday, December 13, 2016
  • by solodkiy
  • Repository
  • 5 Watchers
  • 21 Stars
  • 305 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 2 Versions
  • 4 % Grown

The README.md

Memorize

Build Status Latest Stable Version Total Downloads, (*1)

Memorize is php analog of python @lazy decorator., (*2)

Memorize provides simple in-var cache for closures. It can be used to create lazy functions. Function takes closure and optional argument paramsHash. If the closure with the same arguments was run before memorize will return result from cache without the closure call. At the first call result will be calculated and stored in cache., (*3)

Cache is stored in global space for static methods and simple functions. For closures defined in object method cache will be stored in this object (two objects have different cache). By design cache is stored only while script is running. There is no way to set ttl for cache or invalidate it. Memorize automatically calculates paramsHash for closures with scalar arguments. If your closure has objects as arguments you must calculate and pass paramsHash as the second argument. (see MemorizeInObjectTest::correctAddDaysToTime test), (*4)

Notice that memorize calculates closure hash by file name, start line and end line of closure declaration. Memorize will not work correctly if you declare two closures in one line (e.g: after code obfuscation)., (*5)

Install

composer require solodkiy/memorize

Examples

Singleton with memorize

Before:, (*6)

class Singleton
{
    /**
     * @var Singleton
     */
    private static $instance;

    private function __construct()
    {
        // private
    }

    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new Singleton();
        }
        return self::$instance;
    }
}

After:, (*7)

class Singleton
{
    private function __construct()
    {
        // private
    }

    public static function getInstance()
    {
        return memorize(function() {
            return new Singleton();
        });
    }
}

Lazy recursive factorial function

function factorial($number)
{
    return memorize(function () use ($number) {
        if ($number <= 0) {
            throw new \InvalidArgumentException('Number must be natural');
        }
        return ($number == 1)
            ? 1
            : $number * factorial($number - 1);
    });
}

See MemorizeInFunctionTest, (*8)

Also it correct works in objects.

Before:, (*9)

class TableGateway
{
    /**
     * @var array
     */ 
    private $cacheStatistic = [];

    public function getA($accountId)
    {
        return $this->calculateStatistic($accountId)['a'];
    }

    public function getB($accountId)
    {
        return $this->calculateStatistic($accountId)['b'];
    }

    private function calculateStatistic($accountId)
    {
        if (!isset($this->cacheStatistic[$accountId])) {
            $sql = 'SELECT AVG(price) AS a ...';
            $result = $this->db->getRows($sql, [$accountId]);
            $this->cacheStatistic[$accountId] = $result;
        }
        return $this->cacheStatistic[$accountId];
    }
}

After:, (*10)

class TableGateway
{
    public function getA($accountId)
    {
        return $this->calculateStatistic($accountId)['a'];
    }

    public function getB($accountId)
    {
        return $this->calculateStatistic($accountId)['b'];
    }

    private function calculateStatistic($accountId)
    {
        return memorize(function () use ($accountId) {
            $sql = 'SELECT AVG(price) AS a ...';
            return $this->db->getRows($sql, [$accountId]);
        });
    }
}

See MemorizeFunctionInObjectTest::testTwoObjects, (*11)

The Versions

13/12 2016

dev-master

9999999-dev

Simple cache for pure functions

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Alexey Solodkiy

21/11 2016

0.1.0

0.1.0.0

Simple cache for pure functions

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Alexey Solodkiy