PHP Класс Cache_Lite, cachearium

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$_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

Открытые методы

Метод Описание
__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

Описание методов

__construct() публичный Метод

$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() публичный Метод

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'
Результат boolean true if no problem

_hash() публичный Метод

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'
Результат string control key

_memoryCacheAdd() публичный Метод

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

_read() публичный Метод

Read the cache file and return the content
public _read ( ) : string
Результат string content of the cache file (else : false or a PEAR_Error object)

_setFileName() публичный Метод

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

_setRefreshTime() публичный Метод

Compute & set the refresh time
public _setRefreshTime ( )

_write() публичный Метод

Write the given data in the cache file
public _write ( string $data ) : boolean
$data string data to put in cache
Результат boolean true if ok (a PEAR_Error object else)

_writeAndControl() публичный Метод

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
Результат boolean true if the test is ok (else : false or a PEAR_Error object)

clean() публичный Метод

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'
Результат boolean true if no problem

extendLife() публичный Метод

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

get() публичный Метод

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
Результат string data of the cache (else : false)

getMemoryCachingState() публичный Метод

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() публичный Метод

BE CAREFUL : THIS METHOD IS FOR HACKING ONLY !
public lastModified ( ) : integer
Результат integer last modification time

raiseError() публичный Метод

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

remove() публичный Метод

Remove a cache file
public remove ( string $id, string $group = 'default' ) : boolean
$id string cache id
$group string name of the cache group
Результат boolean true if no problem

save() публичный Метод

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
Результат boolean true if no problem (else : false or a PEAR_Error object)

saveMemoryCachingState() публичный Метод

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() публичный Метод

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

setOption() публичный Метод

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

setToDebug() публичный Метод

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

Описание свойств

$_automaticCleaningFactor публичное свойство

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
Результат integer

$_automaticSerialization публичное свойство

it can be used to save directly datas which aren't strings (but it's slower)
public bool $_automaticSerialization
Результат boolean

$_cacheDir публичное свойство

Directory where to put the cache files (make sure to add a trailing slash)
public string $_cacheDir
Результат string

$_caching публичное свойство

(can be very usefull for the debug of cached scripts)
public bool $_caching
Результат boolean

$_errorHandlingAPIBreak публичное свойство

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
Результат boolean

$_file публичное свойство

File name (with path)
public string $_file
Результат string

$_fileLocking публичное свойство

(can avoid cache corruption under bad circumstances)
public bool $_fileLocking
Результат boolean

$_fileName публичное свойство

File name (without path)
public string $_fileName
Результат string

$_fileNameProtection публичное свойство

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
Результат boolean

$_group публичное свойство

Current cache group
public string $_group
Результат string

$_hashedDirectoryLevel публичное свойство

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
Результат integer

$_hashedDirectoryUmask публичное свойство

Umask for hashed directory structure
public int $_hashedDirectoryUmask
Результат integer

$_id публичное свойство

Current cache id
public string $_id
Результат string

$_lifeTime публичное свойство

If null, the cache is valid forever.
public int $_lifeTime
Результат integer

$_memoryCaching публичное свойство

NB : There is no lifetime for memory caching !
public bool $_memoryCaching
Результат boolean

$_memoryCachingArray публичное свойство

Memory caching array
public array $_memoryCachingArray
Результат array

$_memoryCachingCounter публичное свойство

Memory caching counter
public int $_memoryCachingCounter
Результат integer

$_memoryCachingLimit публичное свойство

Memory caching limit
public int $_memoryCachingLimit
Результат integer

$_onlyMemoryCaching публичное свойство

Enable / Disable "Only Memory Caching" (be carefull, memory caching is "beta quality")
public bool $_onlyMemoryCaching
Результат boolean

$_pearErrorMode публичное свойство

(see PEAR doc)
См. также: setToDebug()
public int $_pearErrorMode
Результат integer

$_readControl публичное свойство

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
Результат boolean

$_readControlType публичное свойство

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
Результат boolean

$_refreshTime публичное свойство

Timestamp of the last valid cache
public int $_refreshTime
Результат integer

$_writeControl публичное свойство

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
Результат boolean