PHP code example of react / cache

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

    

react / cache example snippets


$cache
    ->get('foo')
    ->then('var_dump');

$cache->set('foo', 'bar', 60);

$cache->delete('foo');

$cache->getMultiple(['name', 'age'])->then(function (array $values): void {
    $name = $values['name'] ?? 'User';
    $age = $values['age'] ?? 'n/a';

    echo $name . ' is ' . $age . PHP_EOL;
});

$cache->setMultiple(['foo' => 1, 'bar' => 2], 60);

$cache->deleteMultiple(['foo', 'bar, 'baz']);

$cache->clear();

$cache
    ->has('foo')
    ->then('var_dump');

$cache = new ArrayCache();

$cache->set('foo', 'bar');

$cache = new ArrayCache(2);

$cache->set('foo', '1');
$cache->set('bar', '2');
$cache->set('baz', '3');

$cache
    ->get('foo')
    ->then(function ($result) {
        if ($result === null) {
            return getFooFromDb();
        }
        
        return $result;
    })
    ->then('var_dump');

$cache
    ->get('foo')
    ->then(function ($result) {
        if ($result === null) {
            return $this->getAndCacheFooFromDb();
        }
        
        return $result;
    })
    ->then('var_dump');

public function getAndCacheFooFromDb()
{
    return $this->db
        ->get('foo')
        ->then([$this, 'cacheFooFromDb']);
}

public function cacheFooFromDb($foo)
{
    $this->cache->set('foo', $foo);

    return $foo;
}