PHP Class Prado\I18N\core\TCache_Lite

TCache_Lite is a fast, light and safe cache system. It's optimized for file containers. It is fast and safe (because it uses file locking and/or anti-corruption tests). There are some examples in the 'docs/examples' file Technical choices are described in the 'docs/technical' file A tutorial is available in english at this url : http://www.pearfr.org/index.php/en/article/cache_lite (big thanks to Pierre-Alain Joye for the translation) The same tutorial is also available in french at this url : http://www.pearfr.org/index.php/fr/article/cache_lite Memory Caching is from an original idea of Mike BENOIT
Datei anzeigen Open project: pradosoft/prado Class Usage Examples

Protected Properties

Property Type Description
$_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)
$_file string File name (with path)
$_fileLocking boolean (can avoid cache corruption under bad circumstances)
$_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
$_id string Current cache id
$_lifeTime integer Cache lifetime (in seconds)
$_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")
$_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
_hash ( string $data, string $controlType ) : string Make a control key with the string containing datas
_memoryCacheAdd ( $id, $data )
_read ( ) : string Read the cache file and return the content
_setFileName ( string $id, string $group ) Make a file name (with path)
_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 avoid corrupted cache entries
clean ( string $group = false ) : boolean Clean the cache
get ( string $id, string $group = 'default', boolean $doNotTestCacheValidity = false ) : string Test if a cache is available and (if yes) return it
getCacheFile ( )
getMemoryCachingState ( $id, $group = 'default', $doNotTestCacheValidity = false )
lastModified ( ) : integer Return the cache last modification time
raiseError ( string $msg, integer $code ) Trigger a PEAR 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 ( $id, $group = 'default' )
setLifeTime ( integer $newLifeTime ) Set a new life time

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', 'memoryCaching' => enable / disable memory caching (boolean), 'onlyMemoryCaching' => enable / disable only memory caching (boolean), 'memoryCachingLimit' => max nbr of records in memory caching (int), 'fileNameProtection' => enable / disable file name protection (boolean), 'automaticSerialization' => enable / disable serialization (boolean) );
public __construct ( array $options = [null] )
$options array options

_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

public _memoryCacheAdd ( $id, $data )

_read() public method

Read the cache file and return the content
public _read ( ) : string
return string content of the cache file

_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

_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

_writeAndControl() public method

Write the given data in the cache file and control it just after to avoid corrupted cache entries
public _writeAndControl ( string $data ) : boolean
$data string data to put in cache
return boolean true if the test is ok

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 ) : boolean
$group string name of the cache group
return boolean true if no problem

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 (or false if no cache available)

getCacheFile() public method

public getCacheFile ( )

getMemoryCachingState() public method

public getMemoryCachingState ( $id, $group = 'default', $doNotTestCacheValidity = false )

lastModified() public method

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

raiseError() public method

To improve performances, the PEAR.php file is included dynamically. The file is so included only when an error is triggered. So, in most cases, the file isn't included and perfs are much better.
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

saveMemoryCachingState() public method

public saveMemoryCachingState ( $id, $group = 'default' )

setLifeTime() public method

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

Property Details

$_automaticSerialization protected_oe property

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

$_cacheDir protected_oe property

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

$_caching protected_oe property

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

$_file protected_oe property

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

$_fileLocking protected_oe property

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

$_fileNameProtection protected_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...
protected bool $_fileNameProtection
return boolean

$_group protected_oe property

Current cache group
protected string $_group
return string

$_id protected_oe property

Current cache id
protected string $_id
return string

$_lifeTime protected_oe property

Cache lifetime (in seconds)
protected int $_lifeTime
return integer

$_memoryCaching protected_oe property

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

$_memoryCachingArray protected_oe property

Memory caching array
protected array $_memoryCachingArray
return array

$_memoryCachingCounter protected_oe property

Memory caching counter
protected int $_memoryCachingCounter
return integer

$_memoryCachingLimit protected_oe property

Memory caching limit
protected int $_memoryCachingLimit
return integer

$_onlyMemoryCaching protected_oe property

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

$_readControl protected_oe property

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

$_readControlType protected_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)
protected bool $_readControlType
return boolean

$_refreshTime protected_oe property

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

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