Symfony bundle for class/method memoization

Overview

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

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

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:

Class with one memoized method



use Rikudou\MemoizeBundle\Attribute\Memoizable;
use Rikudou\MemoizeBundle\Attribute\Memoize;

interface CalculatorInterface
{
    public function add(int $a, int $b): int;
    public function sub(int $a, int $b): int;
}

#[Memoizable]
final class Calculator implements CalculatorInterface
{
    #[Memoize]
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
    
    public function sub(int $a, int $b): int
    {
        return $a - $b;
    }
}

This class will simply have the add() method memoized while sub() will not be memoized.

Whole class memoized



use Rikudou\MemoizeBundle\Attribute\Memoizable;
use Rikudou\MemoizeBundle\Attribute\Memoize;

#[Memoizable]
#[Memoize]
final class Calculator implements CalculatorInterface
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
    
    public function sub(int $a, int $b): int
    {
        return $a - $b;
    }
}

Here both add() and sub() methods will be memoized.

Whole class memoized without one method



use Rikudou\MemoizeBundle\Attribute\Memoizable;
use Rikudou\MemoizeBundle\Attribute\Memoize;
use Rikudou\MemoizeBundle\Attribute\NoMemoize;

#[Memoizable]
#[Memoize]
final class Calculator implements CalculatorInterface
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
    
    #[NoMemoize]
    public function sub(int $a, int $b): int
    {
        return $a - $b;
    }
}

Here the whole class will be memoized except for sub() method due to the #[NoMemoize] attribute.

Custom memoization validity



use Rikudou\MemoizeBundle\Attribute\Memoizable;
use Rikudou\MemoizeBundle\Attribute\Memoize;

#[Memoizable]
#[Memoize(seconds: 30)]
final class Calculator implements CalculatorInterface
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
    
    public function sub(int $a, int $b): int
    {
        return $a - $b;
    }
}

Here the whole class will be memoized with the cache validity set to 30 seconds.

Configuration

Tip: Create the default configuration file by using this command: php bin/console config:dump rikudou_memoize > config/packages/rikudou_memoize.yaml

There are these configurations:

  • enabled
  • default_memoize_seconds
  • cache_service

enabled

Simple boolean to enable or disable memoization.

default_memoize_seconds

This parameter sets the default lifetime of memoized cache in seconds. Defaults to -1 which means until end of process (end of request in standard php-fpm or apache2 configurations).

Note that when the default value of -1 is used the cache_service configuration is ignored.

cache_service

This parameter controls which cache service is used for memoization. Defaults to cache.app meaning the default cache for your application.

If the default_memoize_seconds is set to -1 this config is ignored and a default in-memory implementation is used (service rikudou.memoize.internal_cache, class InMemoryCachePool).

Default configuration

Generated by php bin/console config:dump rikudou_memoize

# Default configuration for extension with alias: "rikudou_memoize"
rikudou_memoize:

  # Whether memoization is enabled or not.
  enabled:              true

  # The default memoization period if none is specified in attribute. -1 means until end of request.
  default_memoize_seconds: -1

  # The default cache service to use. If default_memoize_seconds is set to -1 this setting is ignored and internal service is used.
  cache_service:        cache.app
You might also like...
Easily decorate your method calls with laravel-decorator package
Easily decorate your method calls with laravel-decorator package

🎄 Laravel Decorator Decorator pattern in laravel apps Made with ❤️ for smart clean coders A try to port "decorator" feature from python language to l

With the help of the Laravel eCommerce CashU Payment Gateway, the admin can integrate the CashU payment method in the Bagisto store.

Introduction Bagisto CashU Payment add-on allow customers to pay for others using CashU payment gateway. Requirements: Bagisto: v1.3.2 Installation wi

A Bayesian average is a method of estimating the mean of a population using outside information, especially a pre-existing belief, which is factored into the calculation
A Bayesian average is a method of estimating the mean of a population using outside information, especially a pre-existing belief, which is factored into the calculation

A Bayesian average is a method of estimating the mean of a population using outside information, especially a pre-existing belief, which is factored into the calculation.

Simple Symfony API-Platform Template which you can use to start to develop with symfony and api-platform

symfony-api-platform-skeleton Simple Template for Symfony API You can fork it and change the git remote to your Repo git remote set-url your-git-remo

Dockerise Symfony Application (Symfony 6 + Clean Architecture+ DDD+ CQRS + Docker + Xdebug + PHPUnit + Doctrine ORM + JWT Auth + Static analysis)

Symfony Dockerise Symfony Application Install Docker Install Docker Compose Docker PHP & Nginx Create Symfony Application Debugging Install Xdebug Con

Pimcore Bundle that enbable pimcore to expose webook for communication to third parties
Pimcore Bundle that enbable pimcore to expose webook for communication to third parties

WebHookBundle Plugin This extention provide an easy way to send dataobjects to an external sites via json, whenever a pimcore event occurs on a specif

Bundle providing Honeypot field for the Form Builder in Ibexa DXP Experience/Commerce (3.X)

IbexaHoneypot Bundle providing Honeypot field for the Form Builder in Ibexa DXP Experience/Commerce (3.X) What is Honey pot? A honey pot trap involves

PHP bundle which makes array traversing / inserting dead easy.

XTraverse.php This bundle makes it dead easy to traverse through nested arrays/objects in PHP. Installation Via Composer composer require phiil/xtrave

Pageon Doctrine Data Grid Bundle

Pageon Doctrine Data Grid Bundle A bundle that wraps around the knp paginator bundle and doctrine to generate a data grid from your entity Documentati

Releases(v1.0.8)
  • v1.0.8(Mar 21, 2022)

    What's Changed

    • Handle null values for dumping by @RikudouSage in https://github.com/RikudouSage/SymfonyMemoizeBundle/pull/2

    Full Changelog: https://github.com/RikudouSage/SymfonyMemoizeBundle/compare/v1.0.7...v1.0.8

    Source code(tar.gz)
    Source code(zip)
  • v1.0.7(Feb 18, 2022)

  • v1.0.6(Feb 18, 2022)

Owner
Dominik Chrástecký
Dominik Chrástecký
A Symfony Feature Flag Bundle which easily allows you to configure and use your favorite feature flag provider.

Metro Markets FF Metro Markets FF is a Feature Flag Symfony Bundle. It easily allows you to configure and use your favorite feature flag provider. Ins

METRO Markets 14 May 23, 2022
A Symfony bundle built to schedule/consume repetitive tasks

Daily runs Code style Infection PHPUnit Rector Security Static analysis A Symfony bundle built to schedule/consume repetitive tasks Main features Exte

Guillaume Loulier 98 Jan 4, 2023
Tabler.io bundle for Symfony - a backend/admin theme for easy integration

Tabler Bundle for Symfony This repository contains a Symfony bundle, integrating the fantastic Tabler.io HTML Template into your Symfony project. It s

Kevin Papst 22 Jan 2, 2023
A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.

select2entity-bundle Introduction This is a Symfony bundle which enables the popular Select2 component to be used as a drop-in replacement for a stand

Ross Keatinge 214 Nov 21, 2022
Symfony Bundle to create HTML tables with bootstrap-table for Doctrine Entities.

HelloBootstrapTableBundle This Bundle provides simple bootstrap-table configuration for your Doctrine Entities. Used bootstrap-table version 1.18.3. I

Sebastian B 7 Nov 3, 2022
Laravel style FormRequests for Symfony; inspired by adamsafr/form-request-bundle

Somnambulist Form Request Bundle An implementation of form requests from Laravel for Symfony based on the original work by Adam Sapraliev. Requirement

Somnambulist Tech 1 Dec 14, 2021
RSQueue Bundle for Symfony

RSQueueBundle for Symfony Simple queuing system based on Redis Table of contents Installing/Configuring Tags Installing Redis Installing PHPRedis Inst

RSQueue 11 Oct 8, 2018
🕧 Provides an scheduler bundle for symfony framework.

?? PHP Scheduler Bundle Provides the integration of the PHP Scheduler library into Symfony Framework. Installation Run composer require flexic/schedul

FlexicSystems 3 Nov 15, 2022
Exploiting and fixing security vulnerabilities of an old version of E-Class. Project implemented as part of the class YS13 Cyber-Security.

Open eClass 2.3 Development of XSS, CSRF, SQLi, RFI attacks/defences of an older,vulnerable version of eclass. Project implemented as part of the clas

Aristi_Papastavrou 11 Apr 23, 2022
Adds a header to every response to try and twart Google's usage of your site in it's FLoC tracking method.

Laravel No FLoC This package will add the Permissions-Policy: interest-cohort=() to try and twart Google's usage of your site in it's FLoC tracking me

Jean-Philippe Murray 11 Jul 14, 2022