PHP code example of bbc / ipr-cache

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

    

bbc / ipr-cache example snippets


$cacheAdapter = new Doctrine\Common\Cache\ArrayCache();
$cache = new BBC\iPlayerRadio\Cache\Cache($cacheAdapter);

if (($item = $cache->fetch($cacheKey) === false) {
    $data = '{somekey: somevalue}';
    $cache->save($cacheKey, $data, $lifetime);
}



use BBC\iPlayerRadio\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;

$cacheAdapter = new ArrayCache();
$cache = new Cache($cacheAdapter);

$cacheItem = $cache->get('cache_key');

// $cacheItem is an instance of BBC\iPlayerRadio\Cache\CacheItem and will be an object whether the item
// is present in cache or not. You can now call functions on this object to ascertain its state:

// Check if the item is expired: true if it isn't in the cache, false if it is
var_dump($cacheItem->isExpired());

// Retrieve the data you stored in the cache from the item:
$cacheData = $cacheItem->getData();

use BBC\iPlayerRadio\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;

$cache = new Cache(new ArrayCache());

// Attempt to read from the cache:
$item = $cache->get('hello_world');
if ($item->isExpired()) {
    // We don't have an item in the cache, let's rebuild!
    $data = 'This could be the result of an expensive call...';

    // Now we update the item we fetched from the cache with the data
    // and give it a new expiry time (in seconds).
    $item->setData($data);
    $item->setLifetime(60);

    // And re-store in the cache:
    $cache->save($item);
}

// Now we can make use of that data:
echo $item->getData();


$item = $cache->get('hello_world');
$item->setData('I am data!');
$item->setFuzz(0.1); // 10% fuzz
$cache->save($item);

use BBC\iPlayerRadio\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;

$cache = new Cache(new ArrayCache());

// Attempt to read from the cache:
$item = $cache->get('hello_world');
if ($item->isExpired()) {
    $data = 'This could be the result of an expensive call...';

    $item->setData($data);
    $item->setLifetime(60);

    // This is the only difference from Fuzzy caching, we set the fuzz to 0:
    $item->setFuzz(0);

    $cache->save($item);
}

use BBC\iPlayerRadio\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;

$cache = new Cache(new ArrayCache());

$item = $cache->get('hello_world');
if ($item->isStale() || $item->isExpired()) {
    $data = someExpensiveOperation();

    if ($data) {
        // We got a good response, let's cache that:
        $item->setData($data);
        $item->setBestBefore(60); // start re-fetching after 1 minute
        $item->setLifetime(300); // flush from cache at 5 minutes

        $cache->save($item);
    }
}

// At this point, we have to re-examine the cache item to see if the data has been updated. The $item could actually
// be in any state at this point:
//
// - The item was in cache and valid, no refetch happened, you're good to go
// - The item was stale, we re-fetched successfully, you're good to go
// - The item was stale, re-fetch failed, go with the stale data
// - The item was expired, re-fetch failed, you need to do something
//
// Luckily, these four complex states can be handled simply by asking if the item is expired or not:

if ($item->isExpired()) {
    // We have no data to work with:
    gracefullyDegrade();
} else {
    // We have data from somewhere; use it!
    echo $item->getData();
}

// You can either pass the prefix into the constructor:
$cache = new Cache($adapter, 'myprefix_');

// Or set it explicitly:
$cache->setPrefix('mycache_');

$cache = new Cache($adapter, 'myprefix_');
$item = $cache->get('todays_weather'); // actually reads: 'myprefix_todays_weather'

$item->setData('Cloudy');
$cache->save($item);

$cache->delete('yesterdays_weather'); // actually removes 'myprefix_yesterdays_weather'

if ($cache->hasKey('todays_weather')) {
    $item = $cache->get('todays_weather');
    echo 'Today it is: '.$item->getData();
}

function doSomethingWithCache(BBC\iPlayerRadio\Cache\CacheInterface $cache) {

};

$mockedCache = new Cache(new ArrayCache());