Cache_File

Posted by Federico in Components

Description

The Cache component is a little cache system. It’s optimized for high traffic websites so it is really fast and safe, it uses file locking and/or anti-corruption tests.

This component was originally developed by Fabien Marty.

General usage

Every module of Cache follows the same philosophy. Parameters, others than default ones, are set using the setOptions() method. A cache file is identified by a cache ID (and eventually a group). For obvious flexibility reasons, the logic of ID’s choice is left to the developer.

Example: The data is built then recovered in an unique variable

// Set a id for this cache
$id = "123";

if ($data = $this->cache()->get($id)) {
        echo "Data found: " . $data;
} else {
        echo "No valid cache found. Saving data";
        $this->cache()->save($data);
}

Example: Setting options

// This options are set by default
$options = array(
    "cacheDir" => $this->cache()->getCacheDir(),
    "lifeTime" => intval(24*60*60),
);

// Set options
$this->cache()->setOptions($options);

Available options

You can use the setOptions() method to pass an associative array as an argument and set any of the following options:

cacheDir: /cache
string

Directory where to put the cache file (with a trailing slash at the end).

caching: TRUE
boolean

Enable / disable caching.

lifeTime: 24*60*60
integer

Cache lifetime in seconds (you can use a null value for an eternal cache lifetime).

fileLocking: TRUE
boolean

Enable / disable file locking. Can avoid cache corruption under bad circumstances.

writeControl: TRUE
boolean

Enable / disable write control. Enable write control will lightly slow the cache writing but not the cache reading. Write control can detect some corrupt cache files but maybe it’s not a perfect control.

readControl: TRUE
boolean

Enable / disable read control. If enabled, a control key is embeded in cache file and this key is compared with the one calculated after the reading.

readControlType: crc32
string

Type of read control (only if read control is enabled). Must be ‘md5′ (for a md5 hash control (best but slowest)), ‘crc32′ (for a crc32 hash control (lightly less safe but faster)) or ’strlen’ (for a length only test (fastest)).

fileNameProtection: TRUE
boolean

File Name protection (if set to true, you can use any cache id or group name, if set to false, it can be faster but cache ids and group names will be used directly in cache file names so be careful with special characters).

automaticSerialization: FALSE
boolean

Enable / disable automatic serialization (it can be used to save directly data’s which aren’t strings but it’s slower).

memoryCaching: FALSE
boolean

Enable / disable “Memory Caching” (NB : there is no lifetime for memory caching, only the end of the script).

onlyMemoryCaching: FALSEboolean

Enable / disable “Only Memory Caching” (if enabled, files are not used any more).

memoryCachingLimit: 1000
integer

Max number of records to store into memory caching.

automaticCleaningFactor: 0
integer

Disable / Tune the automatic cleaning process. The automatic cleaning process destroy too old (for the given life time) cache files when a new cache file is written. 0 means “no automatic cache cleaning”, 1 means “systematic cache cleaning” (slow), x>1 means “automatic cleaning randomly 1 times on x cache writes”. A value between 20 and 200 is maybe a good start.

hashedDirectoryLevel: 0
integer

Set the hashed directory structure level. 0 means “no hashed directory structure”, 1 means “one level of directory”, 2 means “two levels”… This option can speed up Cache only when you have many thousands of cache file.


Do you have something to say? Say it below.