PHP code example of maartengdev / cache-drivers

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

    

maartengdev / cache-drivers example snippets


$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"

$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!"