Download the PHP package spiechu/json-disk-cache without Composer
On this page you can find all versions of the php package spiechu/json-disk-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spiechu/json-disk-cache
More information about spiechu/json-disk-cache
Files in spiechu/json-disk-cache
Package json-disk-cache
Short Description Cache class intended to keep serialized data in JSON format files
License MIT
Homepage https://github.com/spiechu/JSON-Disk-Cache
Informations about the package json-disk-cache
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.
Features
Class caches data on disk using JSON format. Uses typical methods set()
, get()
, clear()
.
Caches are grouped in domains. Each domain is contained in one file. Hashtable with pairs: name and (optional) params are stashed in file.
What is diffrent is method getSet
which accepts array with object, method name and optionable params to execute when cache is not present.
Additionally any set cache has its own valid time, which can be set globally or per set()
.
Requirements
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.
Installation
Library needs to be registered for autoload. It uses standard SplClassLoader, for example:
Usage
At creation of JSONDiskCache
object You can set cache directory in first param, otherwise jsoncache
in JSONDiskCache.php directory will be set. Second param is default domain. Then You can set default cache valid time. Example below:
There are a few ways to set cache entry:
Now You can retrieve value from cache, notice that null value is treated as no cache at all:
You can also clear cache, true means cache was found and deleted:
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)
:
To save cache to a file just do nothing. Just before object is being destroyed, its destructor will save cache to cache files. To force file write You can do:
Before cache is written to file, some maintenance is being performed automatically when cache entries numbers are above threshold. You can always do a function call: