PHP code example of italystrap / cache

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

    

italystrap / cache example snippets


const MINUTE_IN_SECONDS  = 60; // (seconds)
const HOUR_IN_SECONDS    = 60 * MINUTE_IN_SECONDS;
const DAY_IN_SECONDS     = 24 * HOUR_IN_SECONDS;
const WEEK_IN_SECONDS    = 7 * DAY_IN_SECONDS;
const MONTH_IN_SECONDS   = 30 * DAY_IN_SECONDS;
const YEAR_IN_SECONDS    = 365 * DAY_IN_SECONDS;

use ItalyStrap\Cache\ExpirationInterface;

$expirationTime = ExpirationInterface::MINUTE_IN_SECONDS;
$expirationTime = ExpirationInterface::HOUR_IN_SECONDS;
$expirationTime = ExpirationInterface::DAY_IN_SECONDS;
$expirationTime = ExpirationInterface::WEEK_IN_SECONDS;
$expirationTime = ExpirationInterface::MONTH_IN_SECONDS;
$expirationTime = ExpirationInterface::YEAR_IN_SECONDS;

if (false === ($special_data_to_save = \get_transient('special_data_to_save'))) {
    // It wasn't there, so regenerate the data and save the transient
    $special_data_to_save = ['some-key' => 'come value'];
    \set_transient('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS);
}

use ItalyStrap\Cache\Pool;
use ItalyStrap\Cache\Expiration;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()
$expiration = new Expiration();

$pool = new Pool($driver, $expiration);

// Pass the pool object to other classes that need to save data
// then retrieve the data from the pool
$item = $pool->getItem('special_data_to_save');
if (!$item->isHit()) {
    // It wasn't there, so regenerate the data and save the transient
    $item->set(['some-key' => 'some value']);
    $item->expiresAfter(12 * HOUR_IN_SECONDS);
    $pool->save($item);
}
$special_data_to_save = $item->get();

['some-key' => 'some value'] === $special_data_to_save; // True

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

// Pay attention to `SimpleCacheInterface::get()` method because if there is no value will return `null` and not `false` as the WordPress Transient API does.
if (null === ($special_data_to_save = $cache->get('special_data_to_save'))) {
    // It wasn't there, so regenerate the data and save the transient
    $special_data_to_save = ['some-key' => 'some value'];
    $cache->set('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS);
}

['some-key' => 'some value'] === $special_data_to_save; // True

use ItalyStrap\Cache\Pool;
use ItalyStrap\Cache\Expiration;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()
$expiration = new Expiration();

$pool = new Pool($driver, $expiration);

$item = $pool->getItem('special_data_to_save');
$item->set(['some-key' => 'some value']);
$item->expiresAfter(12 * HOUR_IN_SECONDS);
$pool->save($item);

$pool->deleteItem('special_data_to_save'); // Return bool

// `::getItem()` will return a new item instance, always
$pool->getItem('special_data_to_save')->isHit(); // Return false

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

$cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS);

$cache->delete('special_data_to_save'); // Return bool

$cache->get('special_data_to_save'); // Return null

use ItalyStrap\Cache\Pool;
use ItalyStrap\Cache\Expiration;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()
$expiration = new Expiration();
    
$pool = new Pool($driver, $expiration);

$item = $pool->getItem('special_data_to_save');
$item->set(['some-key' => 'some value']);
$item->expiresAfter(12 * HOUR_IN_SECONDS);
$pool->save($item);

$pool->hasItem('special_data_to_save'); // Return true

// But also this will return false if the item is expired or not exists
$pool->hasItem('expired_or_not_existent_value'); // Return false

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

$cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS);
$cache->has('special_data_to_save'); // Return true

// But also this will return false if the item is expired or not exists
$cache->has('expired_or_not_existent_value'); // Return false

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

$values = [
    'key'       => 'value',
    'key2'      => 'value2',
];

$cache->setMultiple($values, 12 * HOUR_IN_SECONDS); // Return bool

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

$values = [
    'key'       => 'value',
    'key2'      => 'value2',
    'key3'      => false, // This will be replaced with 'some default value' because the method pass a default value
];

$fetched_values = $cache->getMultiple(\array_keys($values), 'some default value'); // Return values

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

$values = [
    'key'       => 'value',
    'key2'      => 'value2',
    'key3'      => false,
];

$cache->deleteMultiple(\array_keys($values)); // Return bool

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);
$cache->set('special_data_to_save',['some-key' => 'come value'], 12 * HOUR_IN_SECONDS);

$values = [
    'key'       => 'value',
    'key2'      => 'value2',
];

$cache->setMultiple($values, 12 * HOUR_IN_SECONDS);

$cache->clear(); // Return bool

$cache->get('special_data_to_save'); // Return null
$cache->get('key'); // Return null
$cache->get('key2'); // Return null

use ItalyStrap\Cache\Expiration;
use ItalyStrap\Cache\SimpleCache;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()

$expiration = new Expiration();
$cache = new SimpleCache($driver, $expiration);

// Get any existing copy of our transient data
if (false === ($special_data_to_save = $cache->get('special_data_to_save'))) {
    // It wasn't there, so regenerate the data and save the transient
     $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS);
}
// Use the data like you would have normally...

//Or

// Get any existing copy of our transient data
if (!$cache->has('special_data_to_save')) {
    // It wasn't there, so regenerate the data and save the transient
     $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS);
}
// Use the data like you would have normally...

use ItalyStrap\Cache\Pool;
use ItalyStrap\Cache\Expiration;
use ItalyStrap\Storage\BinaryCacheDecorator;
use ItalyStrap\Storage\Transient;

$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()
$expiration = new Expiration();
    
$pool = new Pool($driver, $expiration);

$cache = new \ItalyStrap\Cache\SimpleCacheBridge($pool);
// and use the $cache as \Psr\SimpleCache\CacheInterface

use ItalyStrap\Cache\Factory;

$cache = (new Factory())->makePool();
$cache = (new Factory())->makePoolTransient();
$cache = (new Factory())->makeSimpleCache();
$cache = (new Factory())->makeSimpleCacheTransient();
$cache = (new Factory())->makeSimpleCacheBridge();
$cache = (new Factory())->makeSimpleCacheBridgeTransient();