PHP code example of drift / react-key-value

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

    

drift / react-key-value example snippets


use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value');

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$ttl = 0.1; // Means 0.1 second (100 milliseconds)
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value', $ttl);

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value');
$value = $cache->get('my_key');

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$ttl = 2; // Means 2 seconds
$cache->set('my_key', 'Any value');

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 3 second
$value = $cache->get('my_key', true); // Not Found

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->delete('my_key');

return $this
    ->dbConnection
    ->find('token', '123');

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;
use Drift\Cache\KeyValueCacheMiddleware;

$loop = Factory::create();
$ttl = 600; // 10 minutes
$cache = new LocalKeyValueCache($loop);
$middleware = new KeyValueCacheMiddleware($cache);

return $middleware->getOrAsk('token_123', function() {
    return $this
        ->dbConnection
        ->find('token', '123');
}, $ttl, true);