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

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

rikudou/memoize-bundle
======================

Automatic memoization for your Symfony services

v1.1.0(2y ago)222.5k↑50%2[2 PRs](https://github.com/RikudouSage/SymfonyMemoizeBundle/pulls)MITPHPPHP ^8.1

Since Feb 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/RikudouSage/SymfonyMemoizeBundle)[ Packagist](https://packagist.org/packages/rikudou/memoize-bundle)[ Fund](https://ko-fi.com/dominik_ch)[ Fund](https://liberapay.com/dominik_ch)[ RSS](/packages/rikudou-memoize-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (13)Used By (0)

Symfony service memoization bundle
==================================

[](#symfony-service-memoization-bundle)

This bundle provides memoization for your services - every time you call the same method with the same arguments a cached response will be returned instead of executing the method.

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

[](#installation)

Requires php 8.1+.

`composer require rikudou/memoize-bundle`

Afterwards add this to your `composer.json` autoload PSR-4 section: `"App\\Memoized\\": "memoized/"`

Example on fresh Symfony project:

```
{
  "autoload": {
    "psr-4": {
      "App\\": "src/",
      "App\\Memoized\\": "memoized/" // add this line
    }
  }
}
```

Usage
-----

[](#usage)

You can simply use the provided attributes.

- `#[Memoizable]` - Every class that has memoized methods must have this attribute.
- `#[Memoize]` - An attribute that can be used on classes or methods to mark given class/method as memoized. Has an optional parameter with number of seconds for cache validity. Default to `-1` which means until end of the process (meaning end of request in standard php-fpm and apache2 configurations)
- `#[NoMemoize]` - An attribute that allows you to mark a method as non memoized in case you made the whole class memoized.

Every memoized class needs to implement at least one interface that you use to typehint the given service. A proxy class is created for each service with `#[Memoizable]` attribute. This proxy class decorates your service so that every time you ask for your service the proxy gets injected instead.

For non-memoized methods the proxy class simply passes the arguments through to your service while for memoized methods it also creates a cache key based on the parameters and looks for the result into the cache.

> The proxy classes are generated in a compiler pass, meaning the proxy creation doesn't add overhead once container has been dumped.

### Examples:

[](#examples)

**Class with one memoized method**

```
