PHPackages                             pinkcrab/memoize-trait - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. pinkcrab/memoize-trait

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pinkcrab/memoize-trait
======================

Simple Memoize based object cache, used as a trait.

1.0.1(5y ago)025MITPHPPHP &gt;=7.1.0

Since Jan 24Pushed 5y agoCompare

[ Source](https://github.com/Pink-Crab/Memoize)[ Packagist](https://packagist.org/packages/pinkcrab/memoize-trait)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-memoize-trait/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (5)Versions (5)Used By (0)

Memoize
=======

[](#memoize)

Simple trait for adding a Memoize object cache to your class

[![alt text](https://camo.githubusercontent.com/c242bab8b3257f296c0dc18e321cd02f4678a4854c051db3ecfeb64693c02629/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d312e302e302d677265656e2e7376673f7374796c653d666c6174 " ")](https://camo.githubusercontent.com/c242bab8b3257f296c0dc18e321cd02f4678a4854c051db3ecfeb64693c02629/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d312e302e302d677265656e2e7376673f7374796c653d666c6174)[![Open Source Love](https://camo.githubusercontent.com/2d4eac62d5f5830a5309100b88e0ecccb171aee1fbcd794149f4a580b0010c56/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313032)](https://github.com/ellerbrock/open-source-badge/)[![alt text](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174 " ")](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)[![alt text](https://camo.githubusercontent.com/9ba0aba5b38281f6168728647074b43b51126e9d6a7379a5fb9d664c04463f1e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d50415353494e472d627269676874677265656e2e7376673f7374796c653d666c6174 " ")](https://camo.githubusercontent.com/9ba0aba5b38281f6168728647074b43b51126e9d6a7379a5fb9d664c04463f1e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d50415353494e472d627269676874677265656e2e7376673f7374796c653d666c6174)

Version
-------

[](#version)

**Release 1.0.0**

Why?
----

[](#why)

Adds an internal cache with a memoize interface. Creates a cache of results based on a hash of the arguments used for the call.

Setup
-----

[](#setup)

```
$ composer require pinkcrab/memoize-trait
```

```
use PinkCrab\Memoize\Memoizable;

class CategoryRepository {

    /**
     * Gives access to the Memoize cache
     */
    use Memoizable;

    /**
     * Will call the database on the first call.
     * Creates a cache based on the hash of $id & $parent (concatinated)
     */
    public function getCategory($id, $parent): ?CategoryTerm {
        return $this->memoize(
            $this->generateHash($id, $parent),
            function () use ($id, $parent): ?CategoryTerm {
                return $this->slowDataSource->someExpensiveQuery($id, $parent);
            }
        )
    }
}
```

All the methods are protected and are not intended as part of an objects inteface.

Methods
-------

[](#methods)

### memoize( string $hash, callable $fetch ): mixed

[](#memoize-string-hash-callable-fetch--mixed)

The hash value should be unique and repeatable/pure, based on the parameters. You can use the built in generateHash(...$scarla). The callable passed, should carry out your operation, before the result is returned, it is passed to the internal cache.

```
public function doExpensiveCall($param1, $param2): Result
{
    return $this->memoize(
        // Generate Hash
        $this->generateHash($param1, $param2),
        // The fetch/call callable.
        function() use ($param1, $param2) : Result {
            return $this->service
                ->expensiveCall($param1, $param2);
        }
    );
}
```

### generateHash(...$parts): string

[](#generatehashparts-string)

Allows the passing of any number of serializable variables and being returned a repeatable hash. This only uses MD5 under the hood, you can create a custom hash generator

### flushMemoize(): void

[](#flushmemoize-void)

Clears the internal cache array.

Dependencies
------------

[](#dependencies)

- --NONE--

License
-------

[](#license)

### MIT License

[](#mit-license)

Change Log
----------

[](#change-log)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Every ~0 days

Total

3

Last Release

1931d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d82b9e8ef7816d3d0b9812ad233f61f6a313f529e0ac85721781b46ad292e1ea?d=identicon)[glynnquelch](/maintainers/glynnquelch)

---

Top Contributors

[![gin0115](https://avatars.githubusercontent.com/u/28779094?v=4)](https://github.com/gin0115 "gin0115 (7 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pinkcrab-memoize-trait/health.svg)

```
[![Health](https://phpackages.com/badges/pinkcrab-memoize-trait/health.svg)](https://phpackages.com/packages/pinkcrab-memoize-trait)
```

PHPackages © 2026

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