|
|
|
PHP file cache.
It serves simple yet very important purpose: fast data caching. Filecache comes in handy when no memcached or any other fast cache system is available. Filecache at a glance
Installation
The basics
Initialization
include("filecache.class.php"); $cache = new fileCache(); Saving data to cache
Example:
$myvar = array(1,2,3,4,'foo'=>'bar'); $cache->set("key",$myvar,30); //saved data will be valid for 30 secondsExample: $cache->set("key",$myvar,0); //saved data will be valid FOREVER (ttl=0)Example: $cache->set("key",$myvar,30,9); //data will be compressed (compression level 9) and saved for 30 seconds Reading data from cache
$myvar = $cache->get("key"); var_dump($myvar);Returns NULL when data is expired or there's no data in the cache. Advanced features
Constructor switches, levers and knobs
$c = new fileCache($nameSpace=false,$rootDir=false,$useSubDirs=false,$useHash=true);
Example: $cache = new fileCache('testspace','/tmp/cacheroot',true,true);Example: $cache = new fileCache('testspace',false,false); Getting cache info
$cacheInfo = $cache->getInfo("key"); print_r($cacheInfo);Returns associative array with container header fields: Array ( [create_time] => ... //creation timestamp [expire_time] => ... //expiration timestamp [flags] => ... //container flags [isexpired] => ... //is container past its expiration date ? ) Getting cache modification time
$modtime = $cache->getModTime("key"); print_r($modtime);Returns data container modification timestamp (time() format) or FALSE on error or no data. Compression
$myvar = array(1,2,3,4,'myindex'=>'myvalue'); $cache->set("key",$myvar,300,9); //save it for 300 seconds, compression level 9 Cookbook
Database cache
... this section is to be created soon ...
Refreshing cache only when source data is modified
... this section is to be created soon ...
License and Terms of Use
You are granted permission to freely use the code for noncommercial purposes only.
If you like to use the code for commercial purposes to get the permission first. The code is provided "as is" without warranty of any kind. |