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

Abandoned → [valksor/php-functions-memoize](/?search=valksor%2Fphp-functions-memoize)Library[Caching](/categories/caching)

vairogs/functions-memoize
=========================

A PHP library providing memoization functionality to cache the results of expensive function calls and return the cached result when the same inputs occur again

00PHP

Since May 24Pushed 11mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Vairogs Functions: Memoize
==========================

[](#vairogs-functions-memoize)

A PHP library providing memoization functionality to cache the results of expensive function calls and return the cached result when the same inputs occur again.

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

[](#installation)

Install the package via Composer:

```
composer require vairogs/functions-memoize
```

Requirements
------------

[](#requirements)

PHP 8.4 or higher

Usage
-----

[](#usage)

The package provides two classes for memoization:

1. `Memoize` - The base class that provides a caching mechanism to store and retrieve values based on a context and keys.
2. `MemoizeCache` - A conditional class that extends either `RequestCache` (if available in a Symfony environment) or falls back to `Memoize`. This provides seamless integration with Symfony's dependency injection when used as part of the Vairogs bundle.

### Basic Usage

[](#basic-usage)

```
use Vairogs\Functions\Memoize\Memoize;
use YourNamespace\YourEnum;

// Create a new Memoize instance
$memoize = new Memoize();

// Cache the result of an expensive function call
$result = $memoize->memoize(
    YourEnum::SOME_VALUE,  // Context (must be a BackedEnum)
    'your-key',            // Key
    function() {           // Callback function whose result will be cached
        // Expensive operation here
        return $expensiveResult;
    }
);

// Later, retrieve the cached value
$cachedResult = $memoize->value(
    YourEnum::SOME_VALUE,  // Same context
    'your-key',            // Same key
    null                   // Default value if not found
);
```

### Using MemoizeCache

[](#using-memoizecache)

```
use Vairogs\Functions\Memoize\MemoizeCache;
use YourNamespace\YourEnum;

// Create a new MemoizeCache instance
$memoizeCache = new MemoizeCache();

// Use it the same way as Memoize
$result = $memoizeCache->memoize(
    YourEnum::SOME_VALUE,  // Context (must be a BackedEnum)
    'your-key',            // Key
    function() {           // Callback function whose result will be cached
        // Expensive operation here
        return $expensiveResult;
    }
);
```

In a Symfony environment with the Vairogs bundle installed, you can inject MemoizeCache as a service:

```
use Vairogs\Functions\Memoize\MemoizeCache;

class YourService
{
    public function __construct(
        private readonly MemoizeCache $memoizeCache,
    ) {
    }

    public function someMethod()
    {
        // Use $this->memoizeCache
    }
}
```

### Advanced Usage with Nested Keys

[](#advanced-usage-with-nested-keys)

You can use subkeys for more complex caching scenarios (this works with both `Memoize` and `MemoizeCache`):

```
// Cache with nested keys
$result = $memoize->memoize(
    YourEnum::SOME_VALUE,  // Context
    'parent-key',          // Main key
    function() {           // Callback
        return $expensiveResult;
    },
    false,                 // Don't refresh the cache
    'child-key',           // Subkey
    'grandchild-key'       // Another level of subkey
);

// Retrieve nested value
$cachedResult = $memoize->value(
    YourEnum::SOME_VALUE,  // Same context
    'parent-key',          // Same main key
    null,                  // Default value
    'child-key',           // Same subkey path
    'grandchild-key'       // Same subkey path
);
```

### Refreshing the Cache

[](#refreshing-the-cache)

You can force a refresh of the cached value (this works with both `Memoize` and `MemoizeCache`):

```
$result = $memoize->memoize(
    YourEnum::SOME_VALUE,
    'your-key',
    function() {
        return $newExpensiveResult;
    },
    true  // Set refresh to true to force recalculation
);
```

Features
--------

[](#features)

For a complete list of all functions available in this package, see [Features](docs/features.md).

License
-------

[](#license)

This package is licensed under the [BSD-3-Clause License](LICENSE).

About Vairogs
-------------

[](#about-vairogs)

This package is part of the [vairogs/vairogs](https://github.com/vairogs/vairogs) project - a comprehensive PHP library and Symfony bundle that provides a collection of utilities, components, and integrations for Symfony applications.

The main project includes:

- Various utility functions and components
- Doctrine ORM tools and extensions
- API Platform integrations
- Symfony bundle for easy configuration
- And much more

If you find this Memoize component useful, you might want to check out the full Vairogs project for additional tools and utilities that can enhance your Symfony application development.

To install the complete package:

```
composer require vairogs/vairogs
```

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![k0d3r1s](https://avatars.githubusercontent.com/u/38725938?v=4)](https://github.com/k0d3r1s "k0d3r1s (3 commits)")

### Embed Badge

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

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

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)

PHPackages © 2026

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