maartengdev/cache-drivers

Cache all data using different drivers

1.2 2016-10-22 13:21 UTC

This package is not auto-updated.

Last update: 2020-11-27 22:35:11 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version License

Cache Drivers

An easy to use php caching library.

Usage

Basic Usage
$dir = $_SERVER['DOCUMENT_ROOT'] .'/cache/';
$expireTime = 30;

$driver = new LocalDriver($dir);
$cache = new Cache($driver, $expireTime);

$key = 'HelloWorld';

// Check if cache entry exists
$cacheHasKey = $cache->has($key);

// Create new cache entry
$cache->store($key, 'Hello World Cache Drivers');

// Get cache entry
$cacheEntry = $cache->get($key);
// result: "Hello World Cache Drivers"
Check for cache entry with closure
$dir = $_SERVER['DOCUMENT_ROOT'] .'/cache/';
$drive = new LocalDriver($dir);
$cache = new Cache($drive,30);

function myFunction(Cache $cache){
    $key = 'HelloWorld';

    $cacheEntry = $cache->has($key, function ($cache) use ($key) {
        return $cache->get($key);
    });

    if ($cacheEntry) {
        return $cacheEntry;
    }

    $cache->store($key, 'Hello World!');

    return $cache->get($key);
}
myFunction($cache);

// result: "Hello World!"

Licence

MIT