PHP code example of alielmajdaoui / laravel-memoize

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

    

alielmajdaoui / laravel-memoize example snippets




namespace App\Controllers;

use Aliem\Memoize\Facades\Memoize;

class MyAwesomeController extends Controller {
    public function index() {
        
        // Check if the item `iam_heavy` is memoized and return it,
        // otherwise, run the \Closure and memoize the result.
        $value = Memoize::remember('iam_heavy', function() {
            $result = 0;

            for ($i = 0; $i < 1000000; $i++) {
                $result += $i * rand(1, 10);
            }

            return $result;
        });

        return response(sprintf("My memoized value: %s", $value));
    }
}

Memoize::remember(string $key, \Closure $callback);



use Aliem\Memoize\Facades\Memoize;

$comment = Memoize::remember('comment:1', function() {
    return Comment::find(1);
});

Memoize::get(string $key);



use Aliem\Memoize\Facades\Memoize;

$comment = Memoize::get('comment:1');



use Aliem\Memoize\Facades\Memoize;

$result = Comment::find(1);
$comment = Memoize::put('comment:1', $result);



use Aliem\Memoize\Facades\Memoize;

if (Memoize::has('comment:1')) {
    echo 'the item `comment:1` is memoized';
}



use Aliem\Memoize\Facades\Memoize;

Memoize::forget('comment:1');



use Aliem\Memoize\Facades\Memoize;

Memoize::flush();