PHPackages                             solodkiy/memorize - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. solodkiy/memorize

ActiveLibrary

solodkiy/memorize
=================

Simple cache for pure functions

0.1.0(9y ago)2747.1k—1.9%1MITPHPPHP &gt;=5.4

Since Nov 21Pushed 9y ago5 watchersCompare

[ Source](https://github.com/solodkiy/memorize)[ Packagist](https://packagist.org/packages/solodkiy/memorize)[ RSS](/packages/solodkiy-memorize/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Memorize
--------

[](#memorize)

[![Build Status](https://camo.githubusercontent.com/f34ec545170faa90dd59645c3e5451c7bde6c1d170f863c704d120440c41bbe1/68747470733a2f2f7472617669732d63692e6f72672f536f6c6f646b69792f6d656d6f72697a652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Solodkiy/memorize)[![Latest Stable Version](https://camo.githubusercontent.com/098bb7bc38aa2aff288195efb1f25f660d82b297b38f86707ec1b46ce47fb2a8/68747470733a2f2f706f7365722e707567782e6f72672f736f6c6f646b69792f6d656d6f72697a652f762f737461626c65)](https://packagist.org/packages/solodkiy/memorize)[![Total Downloads](https://camo.githubusercontent.com/9ffcc29a65edd726972ec88efa84174e32169f40941efce544a02e74c8191721/68747470733a2f2f706f7365722e707567782e6f72672f736f6c6f646b69792f6d656d6f72697a652f646f776e6c6f616473)](https://packagist.org/packages/solodkiy/memorize)

Memorize is php analog of python [@lazy decorator](https://pypi.python.org/pypi/lazy).

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.

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](tests/Memorize/MemorizeInObjectTest.php) test)

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).

Install
-------

[](#install)

```
composer require solodkiy/memorize

```

Examples
--------

[](#examples)

### Singleton with memorize

[](#singleton-with-memorize)

Before:

```
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:

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

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

### Lazy recursive factorial function

[](#lazy-recursive-factorial-function)

```
function factorial($number)
{
    return memorize(function () use ($number) {
        if ($number 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:

```
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](tests/Memorize/MemorizeInObjectTest.php)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3465d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1427813?v=4)[Alexey Solodkiy](/maintainers/solodkiy)[@solodkiy](https://github.com/solodkiy)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/solodkiy-memorize/health.svg)

```
[![Health](https://phpackages.com/badges/solodkiy-memorize/health.svg)](https://phpackages.com/packages/solodkiy-memorize)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
