PHP code example of roolith / cache

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

    

roolith / cache example snippets



define('ROOLITH_CACHE_DIR', __DIR__ . '/cache');

:put('a', 'b', 3600);
echo CacheFactory::get('a');


Roolith\Caching\Cache\CacheFactory;

// Highest priority: explicit dir.
CacheFactory::driver('file', ['dir' => __DIR__ . '/cache']);

// Middle priority: static override.
CacheFactory::$fileDriverCacheDir = __DIR__ . '/cache';

// Lowest priority: ROOLITH_CACHE_DIR constant or temp fallback.


define('ROOLITH_CACHE_DIR', __DIR__ . '/cache');

cache with 1-hour TTL.
CacheFactory::put('a', 'b', 3600);

// Will retrieve cache or false when missing, expired, or corrupt.
CacheFactory::get('a');

// You can select driver and store.
CacheFactory::driver('file')->put('a', 'b', 3600);

// Will return boolean.
CacheFactory::has('foo');

// Will delete cache item, false when the key has no valid entry.
CacheFactory::remove('foo');

// Will delete all `*.rcache` items in the configured dir.
CacheFactory::flush();


Roolith\Caching\Cache\Cache;

$cache = new Cache();
$cache->driver('file', ['dir' => __DIR__ . '/cache']);

// Third argument is seconds and defaults to 3600.
$cache->put('foo', 'bar', 3600);
print_r($cache->get('foo'));


Roolith\Caching\Driver\FileDriver;
use Roolith\Caching\Cache\Pool;

$fileDriver = new FileDriver(['dir' => __DIR__ . '/cache']);
$pool = new Pool($fileDriver);
$item = $pool->getItem('foo');

if (!$item->isHit()) {
    $item->set([1, 2, 3])->expiresAfter(3600);
    $pool->save($item);
}

print_r($item->get());


Roolith\Caching\Cache\SimpleCache;
use Roolith\Caching\Driver\FileDriver;

$fileDriver = new FileDriver(['dir' => __DIR__ . '/cache']);
$simpleCache = new SimpleCache($fileDriver);

$simpleCache->set('foo', 'bar', 3600);
print_r($simpleCache->get('foo'));


use Roolith\Caching\Cache\SimpleCache;
use Roolith\Caching\Driver\FileDriver;

$cache = new SimpleCache(new FileDriver(['dir' => __DIR__ . '/cache']));

$cache->set('null-ttl', 'v', null); // Expires in 5 hours.
$cache->set('seconds', 'v', 3600); // Expires in 1 hour.
$cache->set('interval', 'v', new DateInterval('P1D')); // Expires in 1 full day.
$cache->set('gone', 'v', 0); // Immediately expired.