PHP code example of rikudou / memoize-bundle

1. Go to this page and download the library: Download rikudou/memoize-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rikudou / memoize-bundle example snippets




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;
    }
}



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;
    }
}



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;
    }
}



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;
    }
}



namespace App\Service;

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

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

    #[Memoize(seconds: -1)]
    public function sub(int $a, int $b): int
    {
        return $a - $b;
    }

    #[NoMemoize]
    public function mul(int $a, int $b): int
    {
        return $a * $b;
    }

    public function someVoidMethod(): void
    {
    }

    public function throwException(): never
    {
        throw new RuntimeException();
    }
}



namespace App\Memoized;

final class Calculator_Proxy_422705a42881a5490e7996fe93245734 implements \App\Service\CalculatorInterface
{
	public function __construct(
		private readonly \App\Service\Calculator $original,
		private readonly \Psr\Cache\CacheItemPoolInterface $cache,
		private readonly \Rikudou\MemoizeBundle\Cache\InMemoryCachePool $internalCache,
		private readonly \Rikudou\MemoizeBundle\Cache\KeySpecifier\CacheKeySpecifier $cacheKeySpecifier,
	) {}

	public function add(int $a, int $b): int {
		$cacheKey = '';
		$cacheKey .= serialize($a);
		$cacheKey .= serialize($b);
		$cacheKey .= $this->cacheKeySpecifier->generate();
		$cacheKey = hash('sha512', $cacheKey);
		$cacheKey = "rikudou_memoize_AppServiceCalculator_add_{$cacheKey}";

		$cacheItem = $this->cache->getItem($cacheKey);
		if ($cacheItem->isHit()) {
			return $cacheItem->get();
		}
		$cacheItem->set($this->original->add($a, $b));
		$cacheItem->expiresAfter(10);
		$this->cache->save($cacheItem);

		return $cacheItem->get();
	}

	public function sub(int $a, int $b): int {
		$cacheKey = '';
		$cacheKey .= serialize($a);
		$cacheKey .= serialize($b);
		$cacheKey .= $this->cacheKeySpecifier->generate();
		$cacheKey = hash('sha512', $cacheKey);
		$cacheKey = "rikudou_memoize_AppServiceCalculator_sub_{$cacheKey}";

		$cacheItem = $this->internalCache->getItem($cacheKey);
		if ($cacheItem->isHit()) {
			return $cacheItem->get();
		}
		$cacheItem->set($this->original->sub($a, $b));
		$cacheItem->expiresAfter(0);
		$this->internalCache->save($cacheItem);

		return $cacheItem->get();
	}

	public function mul(int $a, int $b): int {
		return $this->original->mul($a, $b);
	}

	public function someVoidMethod(): void {
		$cacheKey = '';
		$cacheKey .= $this->cacheKeySpecifier->generate();
		$cacheKey = hash('sha512', $cacheKey);
		$cacheKey = "rikudou_memoize_AppServiceCalculator_someVoidMethod_{$cacheKey}";

		$cacheItem = $this->cache->getItem($cacheKey);
		if ($cacheItem->isHit()) {
			return;
		}
		$cacheItem->set($this->original->someVoidMethod());
		$cacheItem->expiresAfter(10);
		$this->cache->save($cacheItem);

	}

	public function throwException(): never {
		$this->original->throwException();
	}

}