PHP code example of zenstruck / memoize

1. Go to this page and download the library: Download zenstruck/memoize 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/ */

    

zenstruck / memoize example snippets


use Zenstruck\Memoize;

class MyObject
{
    use Memoize;

    public function method1(): mixed
    {
        // cache key defaults to the method name "method1"
        return $this->memoize(
            fn() => $this->someExpensiveOperation() // called only the first time method1() is called
        );
    }

    public function method2(): mixed
    {
        return $this->memoize(
            fn() => $this->someExpensiveOperation(),
            'my_custom_cache_key' // explicitly set the cache key
        );
    }

    public function method3(string $parameter): mixed
    {
        return $this->memoize(
            fn() => $this->someExpensiveOperation($parameter) // called once per unique parameter
            'my_custom_cache_key'.$parameter, // cache key