PHP code example of popphp / pop-cache

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

    

popphp / pop-cache example snippets


use Pop\Cache\Cache;
use Pop\Cache\Adapter\File;

// Passing the file adapter the location on disk and the TTL
$cache = new Cache(new File('/path/to/my/cache/dir', 300));

$cache->saveItem('foo', $data);

$data = $cache->getItem('foo');

$cache['foo'] = $data;      // same as $cache->saveItem('foo', $data)
$data = $cache['foo'];      // same as $cache->getItem('foo')
isset($cache['foo']);       // same as $cache->hasItem('foo')
unset($cache['foo']);       // same as $cache->deleteItem('foo')

$cache->foo = $data;        // property syntax works the same way
$data = $cache->foo;
isset($cache->foo);
unset($cache->foo);

$cache->saveItem('feature_enabled', false);

$cache->getItem('feature_enabled', null); // false — the real cached value
$cache->getItem('feature_disabled_key', null); // null — a genuine miss

use Pop\Cache\Cache;
use Pop\Cache\Adapter\File;
use Pop\Cache\Clock\MutableClock;

$clock = new MutableClock();
$cache = new Cache(new File('/path/to/my/cache/dir', 0, $clock));

$cache->saveItem('foo', 'bar', 10); // TTL of 10 seconds
$clock->advance(11);

$cache->getItem('foo'); // false — expired, instantly and exactly, no sleep() needed

$clock->setTime(2000); // Set clock to a fixed Unix timestamp

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Redis;
use Psr\SimpleCache\CacheInterface;

function useAnyPsr16Cache(CacheInterface $cache): void
{
    $cache->set('foo', 'bar', 300);
    $cache->get('foo'); // 'bar'
}

useAnyPsr16Cache(new Cache(new Redis(300)));

use Pop\Cache\Adapter\Redis;
use Pop\Cache\Psr6\CacheItemPool;

$pool = new CacheItemPool(new Redis(300));

$item = $pool->getItem('foo');
if (!$item->isHit()) {
    $item->set('bar');
    $item->expiresAfter(300); // seconds, or pass a \DateInterval, or null to use the adapter's default TTL
    $pool->save($item);
}

$item->get(); // 'bar'

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Redis;
use Pop\Cache\Psr6\CacheItemPool;

$adapter = new Redis(300);
$cache   = new Cache($adapter);      // pre-existing API + PSR-16
$pool    = new CacheItemPool($adapter); // PSR-6

$value = $cache->remember('expensive-report', function () {
    return generateExpensiveReport(); // only runs on a cache miss
}, 300); // TTL of 300 seconds, optional — omit to use the adapter's global TTL

$value = $cache->remember('expensive-report', function () {
    return generateExpensiveReport();
}, 300, 1.0); // TTL 300s, beta 1.0 enables early recomputation

$views = $cache->incrementItem('page-views-123');           // creates at 0, then +1 -> 1
$views = $cache->incrementItem('page-views-123', 5);        // +5 -> 6
$attempts = $cache->decrementItem('login-attempts-user42', 1, 5); // creates at 5, then -1 -> 4

// Peek at a counter's current value without mutating it: amount 0.
$current = $cache->incrementItem('page-views-123', 0);

$cache->saveTaggedItem('product-1', $productData, ['products', 'category-electronics']);
$cache->saveTaggedItem('product-2', $otherProductData, ['products', 'category-books']);

// Something changed about electronics products in general:
$cache->invalidateTag('category-electronics'); // deletes product-1, leaves product-2 alone

// Invalidate several tags in one call:
$cache->invalidateTags(['products', 'category-books']);

if ($cache->hasItem('foo')) {
    // ...
}

$cache->saveItems([
    'foo' => 'bar',
    'baz' => 'qux',
]);

$cache->deleteItem('foo');

$cache->deleteItems(['foo', 'bar']);

$cache->clear();

$cache->destroy();

Cache::getAvailableAdapters(); // ['apc' => bool, 'file' => true, 'memcached' => bool, 'memory' => true, 'null' => true, 'redis' => bool, 'session' => bool, 'sqlite' => bool]
Cache::isAvailable('redis');   // bool

$cache->adapter(); // the underlying Adapter\AdapterInterface instance
$cache->getTtl();  // the adapter's configured global TTL

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Apc;

$cache = new Cache(new Apc(300));

$cache = new Cache(new Apc(300, 'my-app'));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Memcached;

$cache = new Cache(new Memcached(300, 'localhost', 11211));

$cache = new Cache(new Memcached(300, 'localhost', 11211, 1, 'my-app'));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Redis;

$cache = new Cache(new Redis(300, 'localhost', 6379));

$cache = new Cache(new Redis(300, 'localhost', 6379, 'my-app'));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\File;

$cache = new Cache(new File('/path/to/my/cache/dir', 300));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Database;
use Pop\Db\Db;

$cache = new Cache(
    new Database(Db::sqliteConnect(['database' => __DIR__ . '/tmp/cache.sqlite']), 300)
);

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Session;

$cache = new Cache(new Session(300));

$cache = new Cache(new Session(300, 'my-app'));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\Memory;

$cache = new Cache(new Memory(300));

use Pop\Cache\Cache;
use Pop\Cache\Adapter\NullAdapter;

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