PHP code example of spiechu / json-disk-cache

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

    

spiechu / json-disk-cache example snippets



sLoader = new SplClassLoader('Spiechu\JSONDiskCache' , 'src');
$classLoader->register();


$JSONDiskCache = new JSONDiskCache(__DIR__ . DIRECTORY_SEPARATOR . 'mycachedir', 'my_domain');

// set global cache expiration time in seconds
$JSONDiskCache->setValidTime(10);


$JSONDiskCache->set('valueName', 'value to cache');

// additionally You can set value expiration time in seconds
// global cache expiration is suppressed
$JSONDiskCache->set('valueName', 'value to cache for 5 seconds', 5);

// the same cache name can be distinguished by used params
// in this case You can pass an array to set()
$JSONDiskCache->set(['valueName', 'myParam'], 'value to cache for 5 seconds', 5);


$cachedValue = $JSONDiskCache->get('valueName');

// when array has been passed to set(), the same construction is needed in get()
$cachedValue = $JSONDiskCache->get(['valueName', 'myParam']);


$isCleared = $JSONDiskCache->clear('valueName');

// and again with param
$isCleared = $JSONDiskCache->clear(['valueName', 'myParam']);


$value = JSONDiskCache->getSet(['dataName', 1], [$db, 'fetchData'], [1]);



// this will work, but You lost object too
unset($JSONDiskCache);

// save only hashes
$JSONDiskCache->saveHashTableToFile();

// save cache to file
$JSONDiskCache->saveCacheToFile();



// iterates over all domains and tries to eliminate old cache entries
$JSONDiskCache->cleanUpCache();

// clean only certain domain
$JSONDiskCache->removeOldCacheEntries('domain to clean');