PHP Class Cache_Lite, cachearium

Exibir arquivo Open project: corollarium/cachearium Class Usage Examples

Public Properties

Property Type Description
$_automaticCleaningFactor integer The automatic cleaning process destroy too old (for the given life time) cache files when a new cache file is written. 0 => no automatic cache cleaning 1 => systematic cache cleaning x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
$_automaticSerialization boolean it can be used to save directly datas which aren't strings (but it's slower)
$_cacheDir string Directory where to put the cache files (make sure to add a trailing slash)
$_caching boolean (can be very usefull for the debug of cached scripts)
$_errorHandlingAPIBreak boolean In CACHE_LITE_ERROR_RETURN mode, error handling was not good because for example save() method always returned a boolean (a PEAR_Error object would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without breaking the API, this option (false by default) can change this handling.
$_file string File name (with path)
$_fileLocking boolean (can avoid cache corruption under bad circumstances)
$_fileName string File name (without path)
$_fileNameProtection boolean 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 carefull with special characters...
$_group string Current cache group
$_hashedDirectoryLevel 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_Lite only when you have many thousands of cache file. Only specific benchs can help you to choose the perfect value for you. Maybe, 1 or 2 is a good start.
$_hashedDirectoryUmask integer Umask for hashed directory structure
$_id string Current cache id
$_lifeTime integer If null, the cache is valid forever.
$_memoryCaching boolean NB : There is no lifetime for memory caching !
$_memoryCachingArray array Memory caching array
$_memoryCachingCounter integer Memory caching counter
$_memoryCachingLimit integer Memory caching limit
$_onlyMemoryCaching boolean Enable / Disable "Only Memory Caching" (be carefull, memory caching is "beta quality")
$_pearErrorMode integer (see PEAR doc)
$_readControl boolean If enabled, a control key is embeded in cache file and this key is compared with the one calculated after the reading.
$_readControlType boolean Available values are : 'md5' for a md5 hash control (best but slowest) 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) 'strlen' for a length only test (fastest)
$_refreshTime integer Timestamp of the last valid cache
$_writeControl boolean 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

Public Methods

Method Description
__construct ( array $options = [NULL] ) Constructor
_cleanDir ( string $dir, string $group = false, string $mode = 'ingroup' ) : boolean Recursive function for cleaning cache file in the given directory
_hash ( string $data, string $controlType ) : string Make a control key with the string containing datas
_memoryCacheAdd ( string $data ) Add some date in the memory caching array
_read ( ) : string Read the cache file and return the content
_setFileName ( string $id, string $group ) Make a file name (with path)
_setRefreshTime ( ) Compute & set the refresh time
_unlink ( string $file ) : boolean Remove a file
_write ( string $data ) : boolean Write the given data in the cache file
_writeAndControl ( string $data ) : boolean Write the given data in the cache file and control it just after to avoir corrupted cache entries
clean ( string $group = false, string $mode = 'ingroup' ) : boolean Clean the cache
extendLife ( ) Extend the life of a valid cache file
get ( string $id, string $group = 'default', boolean $doNotTestCacheValidity = false ) : string Test if a cache is available and (if yes) return it
getMemoryCachingState ( string $id, string $group = 'default', boolean $doNotTestCacheValidity = false ) Load the state of the caching memory array from a given cache file cache
lastModified ( ) : integer Return the cache last modification time
raiseError ( string $msg, integer $code ) Trigger error
remove ( string $id, string $group = 'default' ) : boolean Remove a cache file
save ( string $data, string $id = NULL, string $group = 'default' ) : boolean Save some data in a cache file
saveMemoryCachingState ( string $id, string $group = 'default' ) Save the state of the caching memory array into a cache file cache
setLifeTime ( integer $newLifeTime ) Set a new life time
setOption ( $name, $value ) Generic way to set a Cache_Lite option
setToDebug ( ) Set to debug mode

Method Details

__construct() public method

$options is an assoc. Available options are : $options = array( 'cacheDir' => directory where to put the cache files (string), 'caching' => enable / disable caching (boolean), 'lifeTime' => cache lifetime in seconds (int), 'fileLocking' => enable / disable fileLocking (boolean), 'writeControl' => enable / disable write control (boolean), 'readControl' => enable / disable read control (boolean), 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string), 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int), 'memoryCaching' => enable / disable memory caching (boolean), 'onlyMemoryCaching' => enable / disable only memory caching (boolean), 'memoryCachingLimit' => max nbr of records to store into memory caching (int), 'fileNameProtection' => enable / disable automatic file name protection (boolean), 'automaticSerialization' => enable / disable automatic serialization (boolean), 'automaticCleaningFactor' => distable / tune automatic cleaning process (int), 'hashedDirectoryLevel' => level of the hashed directory system (int), 'hashedDirectoryUmask' => umask for hashed directory structure (int), 'errorHandlingAPIBreak' => API break for better error handling ? (boolean) );
public __construct ( array $options = [NULL] )
$options array options

_cleanDir() public method

Recursive function for cleaning cache file in the given directory
public _cleanDir ( string $dir, string $group = false, string $mode = 'ingroup' ) : boolean
$dir string directory complete path (with a trailing slash)
$group string name of the cache group
$mode string flush cache mode : 'old', 'ingroup', 'notingroup', 'callback_myFunction'
return boolean true if no problem

_hash() public method

Make a control key with the string containing datas
public _hash ( string $data, string $controlType ) : string
$data string data
$controlType string type of control 'md5', 'crc32' or 'strlen'
return string control key

_memoryCacheAdd() public method

Add some date in the memory caching array
public _memoryCacheAdd ( string $data )
$data string data to cache

_read() public method

Read the cache file and return the content
public _read ( ) : string
return string content of the cache file (else : false or a PEAR_Error object)

_setFileName() public method

Make a file name (with path)
public _setFileName ( string $id, string $group )
$id string cache id
$group string name of the group

_setRefreshTime() public method

Compute & set the refresh time
public _setRefreshTime ( )

_write() public method

Write the given data in the cache file
public _write ( string $data ) : boolean
$data string data to put in cache
return boolean true if ok (a PEAR_Error object else)

_writeAndControl() public method

Write the given data in the cache file and control it just after to avoir corrupted cache entries
public _writeAndControl ( string $data ) : boolean
$data string data to put in cache
return boolean true if the test is ok (else : false or a PEAR_Error object)

clean() public method

if no group is specified all cache files will be destroyed else only cache files of the specified group will be destroyed
public clean ( string $group = false, string $mode = 'ingroup' ) : boolean
$group string name of the cache group
$mode string flush cache mode : 'old', 'ingroup', 'notingroup', 'callback_myFunction'
return boolean true if no problem

extendLife() public method

see http://pear.php.net/bugs/bug.php?id=6681
public extendLife ( )

get() public method

Test if a cache is available and (if yes) return it
public get ( string $id, string $group = 'default', boolean $doNotTestCacheValidity = false ) : string
$id string cache id
$group string name of the cache group
$doNotTestCacheValidity boolean if set to true, the cache validity won't be tested
return string data of the cache (else : false)

getMemoryCachingState() public method

Load the state of the caching memory array from a given cache file cache
public getMemoryCachingState ( string $id, string $group = 'default', boolean $doNotTestCacheValidity = false )
$id string cache id
$group string name of the cache group
$doNotTestCacheValidity boolean if set to true, the cache validity won't be tested

lastModified() public method

BE CAREFUL : THIS METHOD IS FOR HACKING ONLY !
public lastModified ( ) : integer
return integer last modification time

raiseError() public method

Trigger error
public raiseError ( string $msg, integer $code )
$msg string error message
$code integer error code

remove() public method

Remove a cache file
public remove ( string $id, string $group = 'default' ) : boolean
$id string cache id
$group string name of the cache group
return boolean true if no problem

save() public method

Save some data in a cache file
public save ( string $data, string $id = NULL, string $group = 'default' ) : boolean
$data string data to put in cache (can be another type than strings if automaticSerialization is on)
$id string cache id
$group string name of the cache group
return boolean true if no problem (else : false or a PEAR_Error object)

saveMemoryCachingState() public method

Save the state of the caching memory array into a cache file cache
public saveMemoryCachingState ( string $id, string $group = 'default' )
$id string cache id
$group string name of the cache group

setLifeTime() public method

Set a new life time
public setLifeTime ( integer $newLifeTime )
$newLifeTime integer new life time (in seconds)

setOption() public method

see Cache_Lite constructor for available options
public setOption ( $name, $value )

setToDebug() public method

When an error is found, the script will stop and the message will be displayed (in debug mode only).
public setToDebug ( )

Property Details

$_automaticCleaningFactor public_oe property

The automatic cleaning process destroy too old (for the given life time) cache files when a new cache file is written. 0 => no automatic cache cleaning 1 => systematic cache cleaning x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
public int $_automaticCleaningFactor
return integer

$_automaticSerialization public_oe property

it can be used to save directly datas which aren't strings (but it's slower)
public bool $_automaticSerialization
return boolean

$_cacheDir public_oe property

Directory where to put the cache files (make sure to add a trailing slash)
public string $_cacheDir
return string

$_caching public_oe property

(can be very usefull for the debug of cached scripts)
public bool $_caching
return boolean

$_errorHandlingAPIBreak public_oe property

In CACHE_LITE_ERROR_RETURN mode, error handling was not good because for example save() method always returned a boolean (a PEAR_Error object would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without breaking the API, this option (false by default) can change this handling.
public bool $_errorHandlingAPIBreak
return boolean

$_file public_oe property

File name (with path)
public string $_file
return string

$_fileLocking public_oe property

(can avoid cache corruption under bad circumstances)
public bool $_fileLocking
return boolean

$_fileName public_oe property

File name (without path)
public string $_fileName
return string

$_fileNameProtection public_oe property

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 carefull with special characters...
public bool $_fileNameProtection
return boolean

$_group public_oe property

Current cache group
public string $_group
return string

$_hashedDirectoryLevel public_oe property

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_Lite only when you have many thousands of cache file. Only specific benchs can help you to choose the perfect value for you. Maybe, 1 or 2 is a good start.
public int $_hashedDirectoryLevel
return integer

$_hashedDirectoryUmask public_oe property

Umask for hashed directory structure
public int $_hashedDirectoryUmask
return integer

$_id public_oe property

Current cache id
public string $_id
return string

$_lifeTime public_oe property

If null, the cache is valid forever.
public int $_lifeTime
return integer

$_memoryCaching public_oe property

NB : There is no lifetime for memory caching !
public bool $_memoryCaching
return boolean

$_memoryCachingArray public_oe property

Memory caching array
public array $_memoryCachingArray
return array

$_memoryCachingCounter public_oe property

Memory caching counter
public int $_memoryCachingCounter
return integer

$_memoryCachingLimit public_oe property

Memory caching limit
public int $_memoryCachingLimit
return integer

$_onlyMemoryCaching public_oe property

Enable / Disable "Only Memory Caching" (be carefull, memory caching is "beta quality")
public bool $_onlyMemoryCaching
return boolean

$_pearErrorMode public_oe property

(see PEAR doc)
See also: setToDebug()
public int $_pearErrorMode
return integer

$_readControl public_oe property

If enabled, a control key is embeded in cache file and this key is compared with the one calculated after the reading.
public bool $_readControl
return boolean

$_readControlType public_oe property

Available values are : 'md5' for a md5 hash control (best but slowest) 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) 'strlen' for a length only test (fastest)
public bool $_readControlType
return boolean

$_refreshTime public_oe property

Timestamp of the last valid cache
public int $_refreshTime
return integer

$_writeControl public_oe property

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
public bool $_writeControl
return boolean