PHP code example of pinkcrab / memoize-trait

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

    

pinkcrab / memoize-trait example snippets


use PinkCrab\Memoize\Memoizable;

class CategoryRepository {

    /**
     * Gives access to the Memoize cache
     */
    use Memoizable;

    /** 
     * Will call the database on the first call.
     * Creates a cache based on the hash of $id & $parent (concatinated)
     */
    public function getCategory($id, $parent): ?CategoryTerm {
        return $this->memoize(
            $this->generateHash($id, $parent),
            function () use ($id, $parent): ?CategoryTerm {
                return $this->slowDataSource->someExpensiveQuery($id, $parent);
            }
        )
    }
}

public function doExpensiveCall($param1, $param2): Result 
{
    return $this->memoize(
        // Generate Hash
        $this->generateHash($param1, $param2),
        // The fetch/call callable.
        function() use ($param1, $param2) : Result {
            return $this->service
                ->expensiveCall($param1, $param2);
        }
    );
}