Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Cache class intended to keep serialized data in JSON format files.
Tag: 0.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

JSON Disk Cache

Cache class intended to keep serialized data in JSON format files.

There is one cache directory per all domains. hashtable.cache keeps cache hashes of all domains. domain.cache keeps hashes with serialized data.


Class caches data on disk using JSON format. Uses typical methods set(), get(), clear(). What is diffrent is method getSet which accepts array with object, method name and optionable params to execute when cache is not present.


You need at least PHP 5.4 since library uses shorthand array creation [] and (new Object)->method() construction. If You want to run unit tests, PHPUnit 3.6 is needed.


Library needs to be registered for autoload. It uses standard SplClassLoader, for example:

require_once 'SplClassLoader.php';
$classLoader = new SplClassLoader('Spiechu\JSONDiskCache' , 'src');


At creation of JSONDiskCache object You can set cache directory in first param, otherwise jsoncache in JSONDiskCache.php directory will be set, for example:

$JSONDiskCache = new JSONDiskCache(__DIR__ . DIRECTORY_SEPARATOR . 'mycachedir', 'my_domain');
$JSONDiskCache->set('valueName', 'value to cache');

Now You can retrieve value from cache, notice that null value is treated as no cache at all:

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

You can also clear cache, true means cache was found and deleted:

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

Suppose You have $db object that retrieves data from database with $db->fetchData(1) method. When valid cache for pair array('dataName', 1) is found then cache is used, otherwise $db->fetchData(1):

$value = JSONDiskCache->getSet(['dataName', 1], [$db, 'fetchData', 1]);
Something went wrong with that request. Please try again.