Home Projects PHP file cache
"Any sufficiently advanced technology is indistinguishable from magic. "
PHP file cache.

Download the PHP filecache class
ver. 1.0 beta (2006-05-28)
Filecache is a general-purpose PHP class which uses files ("data containers") to store data.

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
  • PHP filecache is fast as it uses native PHP functions such as serialize, pack, fread.
  • Uses files (referred to as "data containers") to store data. Each container has header where meta-data (flags, expire time etc.) is stored for fast access.
  • Server-restart independent. Your data isn't lost after server restart. Data containers simply preserve it, ready for access.
  • Provides two basic functions: set() and get() to write to and read from the cache.
  • Uses portable advisory file locking to avoid race condition errors and data corruption (local filesystem only).
  • Supports data serialization and compression (gzcompress).
  • Secures data containers so they may be placed within the DOCUMENT_ROOT structure without the risk of exposing the cache content.
  • Supports "flat" and "subdirs" container storage modes allowing container files to be distributed among subdirs.
  • Supports filename hash generation.
  • As simple as:
    include("filecache.class.php");
    $c = new fileCache();
    $mydata = "my data to be saved";
    $c->set("key",$mydata,60); //save for 60 seconds 
    $mydata = $c->get("key"); //get my data back 
Installation
  1. Download and unpack the PHP filecache class. filecache.class.php is the file you need.
  2. Create a directory where cache content will be stored. Make sure PHP scripts have proper permissions to create new files there.
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 seconds 
Example:
$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);
$nameSpace unique namespace for keys (FALSE = use global namespace).
$rootDir FALSE (default) = use default directory from FILECACHE_ROOTDIR or autodetect.
path = use arbitrary base directory to write/read files.
$useSubDirs FALSE (default) = flat mode, everything is stored in the rootDir. No subdirs are created.
TRUE = create additional subdirs inside the rootDir to separate namespaces and files
$useHash TRUE (default) = use hash function to create filenames based on keys.
FALSE = filenames are based directly on keys.

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.


© 2003-2012 by , powered by SICOMA CMS