PHPackages                             stellarwp/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. stellarwp/memoize

ActiveLibrary[Caching](/categories/caching)

stellarwp/memoize
=================

A flexible memoization library for memory caching.

1.2.2(1y ago)032.8k—8.1%[1 issues](https://github.com/stellarwp/memoize/issues)GPL-2.0PHPCI passing

Since Dec 18Pushed 10mo ago4 watchersCompare

[ Source](https://github.com/stellarwp/memoize)[ Packagist](https://packagist.org/packages/stellarwp/memoize)[ RSS](/packages/stellarwp-memoize/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (10)Versions (7)Used By (0)

Stellar Memoize
===============

[](#stellar-memoize)

[![Tests](https://github.com/stellarwp/memoize/workflows/Tests/badge.svg)](https://github.com/stellarwp/memoize/actions?query=branch%3Amain) [![PHPCS](https://github.com/stellarwp/memoize/actions/workflows/phpcs.yml/badge.svg)](https://github.com/stellarwp/memoize/actions/workflows/phpcs.yml) [![PHPStan](https://github.com/stellarwp/memoize/actions/workflows/phpstan.yml/badge.svg)](https://github.com/stellarwp/memoize/actions/workflows/phpstan.yml) [![Compatibility](https://github.com/stellarwp/memoize/actions/workflows/compatibility.yml/badge.svg)](https://github.com/stellarwp/memoize/actions/workflows/compatibility.yml)

A flexible memoization library for memory caching.

Table of Contents
-----------------

[](#table-of-contents)

- [Memoization](#memoization)
- [Installation](#installation)
- [Notes on examples](#notes-on-examples)
- [Usage](#usage)
    - [Simple example](#simple-example)
    - [Setting a nested structure](#setting-a-nested-structure)
    - [Purging a nested structure](#purging-a-nested-structure)
    - [Caching with closures](#caching-with-closures)
    - [Using with a dependency injection container](#using-with-a-dependency-injection-container)
- [Drivers](#drivers)
    - [MemoryDriver](#memorydriver)
- [Traits](#traits)

Memoization
-----------

[](#memoization)

[Memoization](https://en.wikipedia.org/wiki/Memoization) is the process of caching the results of expensive function calls so that they can be reused when the same inputs occur again.

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

[](#installation)

It's recommended that you install Memoize as a project dependency via [Composer](https://getcomposer.org/):

```
composer require stellarwp/memoize
```

> We *actually* recommend that this library gets included in your project using [Strauss](https://github.com/BrianHenryIE/strauss).
>
> Luckily, adding Strauss to your `composer.json` is only slightly more complicated than adding a typical dependency, so checkout our [strauss docs](https://github.com/stellarwp/global-docs/blob/main/docs/strauss-setup.md).

Notes on examples
-----------------

[](#notes-on-examples)

All namespaces within the examples will be using the `StellarWP\Memoize`, however, if you are using Strauss, you will need to prefix these namespaces with your project's namespace.

Usage
-----

[](#usage)

### Simple example

[](#simple-example)

```
use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo', 'bar');

if ($memoizer->has('foo')) {
    echo $memoizer->get('foo'); // Outputs: bar
}

// Unsets foo from the memoization cache.
$memoizer->forget('foo');
```

### Setting a nested structure

[](#setting-a-nested-structure)

Memoize allows you to use dot notation to set, get, and forget values from a nested structure. This allows you to easily add/fetch values and then purge individual items or whole collections of items.

```
use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo.bar.bork', 'baz');

// This results in the following cache:
// [
//     'foo' => [
//         'bar' => [
//             'bork' => 'baz',
//         ],
//     ],
// ]

// You can fetch the value like so:
$value = $memoizer->get('foo.bar.bork');
echo $value; // Outputs: baz

// You can fetch anywhere up the chain:
$value = $memoizer->get('foo.bar');
echo $value; // Outputs: [ 'bork' => 'baz' ]

$value = $memoizer->get('foo');
echo $value; // Outputs: [ 'bar' => [ 'bork' => 'baz' ] ]

$value = $memoizer->get();
echo $value; // Outputs: [ 'foo' => [ 'bar' => [ 'bork' => 'baz' ] ] ]
```

#### Purging a nested structure

[](#purging-a-nested-structure)

```
use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo.bar.bork', 'baz');
$memoizer->forget('foo.bar.bork');

// This results in the following cache:
// [
//     'foo' => [
//         'bar' => [],
//     ],
// ]

$memoizer->forget('foo.bar');

// This results in the following cache:
// [
//     'foo' => [],
// ]

$memoizer->forget('foo');

// This results in the following cache:
// []

$memoizer->forget();

// This results in the following cache:
// []
```

### Caching with closures

[](#caching-with-closures)

Memoize also supports using closures as values that get resolved before setting in the cache.

```
use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo', static function () {
    return 'bar';
});

echo $memoizer->get('foo'); // Outputs: bar
```

### Using with a dependency injection container

[](#using-with-a-dependency-injection-container)

#### Project class

[](#project-class)

```
