PHP code example of bentools / redis-psr6-ttl-aware-adapter

1. Go to this page and download the library: Download bentools/redis-psr6-ttl-aware-adapter 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/ */

    

bentools / redis-psr6-ttl-aware-adapter example snippets


$redis = new Redis();
$redis->connect('127.0.0.1');
$cache = new \BenTools\Cache\Adapter\RedisTTLAwareAdapter($redis);

$key = 'my.example';

# Assume my.example does not exist
var_dump($cache->hasItem($key)); // false
$item = $cache->getItem($key);
$item->set(1);
$item->expiresAfter(5); // expires in 5 seconds
$cache->save($item);
sleep(2);

# After 2 seconds, my.example == 1
var_dump($cache->hasItem($key)); // true
$item = $cache->getItem($key);
$value = $item->get(); // 1
var_dump($value); // 1

$item->set($value + 1);
$cache->save($item);
sleep(2);


# After 4 seconds, my.example == 2
var_dump($cache->hasItem($key)); // true
$item = $cache->getItem($key);
$value = $item->get();
var_dump($value); // 2

$item->set($value + 1);
$cache->save($item);
sleep(2);

# After 6 seconds, my.example does not exist anymore
var_dump($cache->hasItem($key)); // false
$item = $cache->getItem($key);
$value = $item->get();
var_dump($value); // null