1. Go to this page and download the library: Download harmonyio/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/ */
harmonyio / cache example snippets
declare(strict_types=1);
namespace Foo;
use Amp\Dns;
use Amp\Dns\Record;
use Amp\Redis\Client;
use HarmonyIO\Cache\Item;
use HarmonyIO\Cache\Key;
use HarmonyIO\Cache\Provider\Redis;
use HarmonyIO\Cache\Ttl;
use function Amp\Dns\query;
use function Amp\wait;
want to cache (in this cache a DNS lookup)
$result = wait(query('www.example.org', Record::A));
// create the cacheable item to be stored in the cache for 1 hour
$itemToCache = new Item($key, json_encode($result), new Ttl(Ttl::ONE_HOUR));
// store the cached item
$cache->store($itemToCache);
// retrieve the item
var_dump($cache->get($key));
declare(strict_types=1);
namespace HarmonyIO\Cache;
use Amp\Promise;
interface Cache
{
/**
* @return Promise<null|string>
*/
public function get(Key $key): Promise;
/**
* @return Promise<bool>
*/
public function exists(Key $key): Promise;
/**
* @return Promise<null>
*/
public function delete(Key $key): Promise;
/**
* @return Promise<bool>
*/
public function store(Item $item): Promise;
}
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Item;
use HarmonyIO\Cache\Key;
use HarmonyIO\Cache\Ttl;
$key = new Key('HttpRequest', 'https://httpbin.org/get', md5('https://httpbin.org/get'));
$itemToCache = new Item($key, 'Result from the http request', new Ttl(60));
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Ttl;
$ttl = Ttl::fromDateTime((new \DateTimeImmutable())->add(new \DateInterval('P1D')));
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Ttl;
$ttl = new Ttl(10);
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Ttl;
$ttl = new Ttl(Ttl::ONE_HOUR);
$hash = md5('SELECT * FROM users WHERE name = ?' . json_encode(['Bobby Tables']));
declare(strict_types=1);
namespace Foo;
use Amp\Redis\Client;
use HarmonyIO\Cache\Provider\Redis;
$cache = new Redis(new Client('tcp://127.0.0.1:6379'));
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Provider\InMemory;
$cache = new InMemory();
declare(strict_types=1);
namespace HarmonyIO\Cache;
interface CacheableRequest
{
public function getCachingKey(): Key;
public function getTtl(): Ttl;
}