PHPackages                             zenstruck/memoize - 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. [Caching](/categories/caching)
4. /
5. zenstruck/memoize

ActiveLibrary[Caching](/categories/caching)

zenstruck/memoize
=================

Helper trait to efficiently cache expensive methods in memory.

v1.0.0(5mo ago)580MITPHPPHP &gt;=8.1CI passing

Since Apr 26Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/zenstruck/memoize)[ Packagist](https://packagist.org/packages/zenstruck/memoize)[ Docs](https://github.com/zenstruck/memoize)[ GitHub Sponsors](https://github.com/kbond)[ GitHub Sponsors](https://github.com/nikophil)[ RSS](/packages/zenstruck-memoize/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

zenstruck/memoize
=================

[](#zenstruckmemoize)

[![CI](https://github.com/zenstruck/memoize/actions/workflows/ci.yml/badge.svg)](https://github.com/zenstruck/memoize/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/dcd1c5c3848aab33917a0071db0ad00817440085fd529d8bf65c9b05cdf29437/68747470733a2f2f636f6465636f762e696f2f67682f7a656e73747275636b2f6d656d6f697a652f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d5a515059364753787674)](https://codecov.io/gh/zenstruck/memoize)

Helper trait to efficiently cache expensive methods in memory.

Installation
------------

[](#installation)

```
composer require zenstruck/memoize
```

Usage
-----

[](#usage)

Add the memoize trait to an object you wish to cache operations on.

```
use Zenstruck\Memoize;

class MyObject
{
    use Memoize;

    public function method1(): mixed
    {
        // cache key defaults to the method name "method1"
        return $this->memoize(
            fn() => $this->someExpensiveOperation() // called only the first time method1() is called
        );
    }

    public function method2(): mixed
    {
        return $this->memoize(
            fn() => $this->someExpensiveOperation(),
            'my_custom_cache_key' // explicitly set the cache key
        );
    }

    public function method3(string $parameter): mixed
    {
        return $this->memoize(
            fn() => $this->someExpensiveOperation($parameter) // called once per unique parameter
            'my_custom_cache_key'.$parameter, // cache key includes the parameter
        )
    }

    public function refresh(): void
    {
        $this->clearMemoized(); // clear all cached values for this object instance
        $this->clearMemoized('method1'); // clear just the cached value for "method1"
    }
}
```

> **Note**: The cached values are stored in a `WeakMap` keyed by each object's instance. They are automatically cleared as the objects are garbage collected.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance78

Regular maintenance activity

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

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 ~513 days

Total

3

Last Release

91d ago

Major Versions

v0.1.0 → v1.0.02025-12-07

PHP version history (2 changes)v0.1.0PHP &gt;=8.0

v1.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/707369cc916e0ea1aacbf077dcba464f611cef879f024d8944311a54a15224b3?d=identicon)[kbond](/maintainers/kbond)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (10 commits)")

---

Tags

cache

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[psr/simple-cache

Common interfaces for simple caching

8.1k727.3M2.1k](/packages/psr-simple-cache)[psr/cache

Common interface for caching libraries

5.2k686.9M1.3k](/packages/psr-cache)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[beste/in-memory-cache

A PSR-6 In-Memory cache that can be used as a fallback implementation and/or in tests.

2512.2M6](/packages/beste-in-memory-cache)[anahkiasen/flatten

A package for the Illuminate framework that flattens pages to plain HTML

33313.0k](/packages/anahkiasen-flatten)[rtcamp/nginx-helper

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also provides cloudflare edge cache purging with Cache-Tags.

23817.0k1](/packages/rtcamp-nginx-helper)

PHPackages © 2026

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