PHP Class Cachearium\CacheAbstract

显示文件 Open project: corollarium/cachearium Class Usage Examples

Public Properties

Property Type Description
$debugLogFile Controls debug to a file
$debugOnPage boolean Controls debug on html page for all Cache backends.

Protected Properties

Property Type Description
$cache_log array Stores cache log for debugging.
$enabled boolean Is this cache enabled?
$lifetime
$namespace string Initialize it to a unique string. If you are not running multiple instances, ignore.
$should_log boolean Is log enabled? Log can take a lot of RAM, so only turn this on when profiling.
$summary array Array for basic cache profiling. Keys are CacheLogEnum, values are counters.

Public Methods

Method Description
appendCallback ( callable $callback ) : boolean Appends a callback to the current start()/end() cache
clean ( CacheKey $k ) : boolean Cleans cache: all entries with a certain $base and $id in the $key are deleted.
cleanP ( $base, $id ) : boolean Cleans cache: all entries with a certain $base and $id
clear ( ) Clears entire cache. Use sparingly.
clearAll ( ) Clears all cache classes.
cssDebug ( ) Renders CSS for live view debugging of cached data.
delete ( CacheKey $k ) : boolean Deletes an entry from the cache
deleteP ( string $base, string $id, mixed $sub = null )
disable ( ) : CacheAbstract Disable this cache
dumpSummary ( ) Dumps a short HTML summary of the cache hits/misses
enable ( ) : CacheAbstract Enable this cache
end ( $print = true ) Ends the cache start().
factory ( string $backend ) : CacheAbstract Returns a cache
footerDebug ( ) Extensive footer debug code. Shows which parts of the HTML were cached or missed visually. Great!
get ( CacheKey $k ) : mixed Get cached entry.
getData ( CacheKey $k ) : CacheData Same as get, but assumes data was stored with a CacheData object and will treat it accordingly.
getDataP ( string $base, string $id, mixed $sub = null ) Same as getData(), but expanded parameters.
getDefaultLifetime ( )
getLogSummary ( ) : array() Returns basic cache statistics. See $summary.
getMulti ( array $cacheid, callable $callback = null ) : array:mixed Gets data from multiple cache keys at once
getNamespace ( ) : string
getP ( string $base, string $id, mixed $sub = null ) : mixed Same as get(), but expanded parameters.
increment ( integer $value, CacheKey $k, integer $default ) : integer Increment a variable. Backend deals with this, but in general this is atomic.
invalidate ( CacheKey $k ) Invalidates a dependency index. If the index does not exist it is created.
isEnabled ( ) : True
logHigh ( $message ) High level log for testing and debugging
newEnd ( $print = true )
newstart ( CacheKey $k, $lifetime = null, $fail = false )
prefetch ( array $data ) Prefetches data which will be used. This avoids multiple trips to the cache server if they can be avoided.
recursiveEnd ( boolean $print = true ) : string
recursiveStart ( CacheKey $k, integer $lifetime = null, boolean $print = true, boolean $fail = false ) : string
recursiveStartP ( $base, $id, $sub = null, $lifetime = null, $print = true, $fail = false )
report ( ) Generates a report for this backend
resetLogSummary ( )
setDefaultLifetime ( number $lifetime )
setEnabled ( $b ) : CacheAbstract Enable this cache
setLog ( boolean $b ) : CacheAbstract
setNamespace ( string $name )
start ( CacheKey $k, string $lifetime = null, boolean $print = true, boolean $fail = false ) : boolean | string Starts a cache if it doesn't exist, or outputs the data and returns true.
startCallback ( CacheKey $k, callable $c, array $cparams = [], integer $lifetime = null ) start() using a callable. Same as start()/c()/end().
startP ( $base, $id, $sub = null, $lifetime = null, $print = true, $fail = false )
store ( mixed $data, CacheKey $k, integer $lifetime ) : boolean Saves data in cache.
storeData ( CacheData $data, number $lifetime ) : boolean Same as store() but expanded parameters
storeP ( mixed $data, string $base, string $id, string $sub = null, number $lifetime ) : boolean Same as store() but expanded parameters

Protected Methods

Method Description
hashKey ( CacheKey $k ) Returns a key given parameters. This is up to storage and different values may be returned for the same parameters, as storages are likely to use key-based cache expiration.
keyFromDeps ( CacheKey $k, $deps )
log ( string $status, CacheKey $k, integer $lifetime ) Logs cache accesses for debugging
printProbeEnd ( string $key, CacheData $cachedata ) Prints HTML for cache debug probes -> closes tag
printProbeStart ( string $key, CacheData $cachedata, string $type ) Prints HTML for cache debug probes -> opens tag

Private Methods

Method Description
extraSub ( unknown &$sub ) Get extra sub

Method Details

appendCallback() public method

Callbacks are always called at runtime, their result is never cached at this level. You may cache it in the callback, of course.
public appendCallback ( callable $callback ) : boolean
$callback callable
return boolean

clean() public method

Cleans cache: all entries with a certain $base and $id in the $key are deleted.
public clean ( CacheKey $k ) : boolean
$k CacheKey
return boolean true if no problem

cleanP() abstract public method

Cleans cache: all entries with a certain $base and $id
abstract public cleanP ( $base, $id ) : boolean
return boolean true if no problem

clear() abstract public method

Clears entire cache. Use sparingly.
abstract public clear ( )

clearAll() public static method

Clears all cache classes.
public static clearAll ( )

cssDebug() public static method

Renders CSS for live view debugging of cached data.
public static cssDebug ( )

delete() abstract public method

Deletes an entry from the cache
abstract public delete ( CacheKey $k ) : boolean
$k CacheKey
return boolean

deleteP() public method

See also: delete()
public deleteP ( string $base, string $id, mixed $sub = null )
$base string
$id string
$sub mixed

disable() public method

Disable this cache
public disable ( ) : CacheAbstract
return CacheAbstract

dumpSummary() public static method

Dumps a short HTML summary of the cache hits/misses
public static dumpSummary ( )

enable() public method

Enable this cache
public enable ( ) : CacheAbstract
return CacheAbstract this

end() public method

Ends the cache start().
See also: recursiveEnd()
public end ( $print = true )

factory() public static method

Returns a cache
public static factory ( string $backend ) : CacheAbstract
$backend string
return CacheAbstract

footerDebug() public static method

Extensive footer debug code. Shows which parts of the HTML were cached or missed visually. Great!
public static footerDebug ( )

get() abstract public method

Get cached entry.
abstract public get ( CacheKey $k ) : mixed
$k CacheKey
return mixed

getData() public method

Same as get, but assumes data was stored with a CacheData object and will treat it accordingly.
public getData ( CacheKey $k ) : CacheData
$k CacheKey
return CacheData

getDataP() public method

Same as getData(), but expanded parameters.
See also: getData()
public getDataP ( string $base, string $id, mixed $sub = null )
$base string
$id string
$sub mixed

getDefaultLifetime() public method

public getDefaultLifetime ( )

getLogSummary() public static method

Returns basic cache statistics. See $summary.
public static getLogSummary ( ) : array()
return array()

getMulti() public method

Backends may override this to provide an efficient implementation over multiple calls to get().
public getMulti ( array $cacheid, callable $callback = null ) : array:mixed
$cacheid array List of cache keys
$callback callable if present will be called for any \NotCachedExceptions. Callback should have this signature: (CacheAbstract $c, CacheKey $k)
return array:mixed

getNamespace() public method

public getNamespace ( ) : string
return string

getP() public method

Same as get(), but expanded parameters.
See also: getK
public getP ( string $base, string $id, mixed $sub = null ) : mixed
$base string
$id string
$sub mixed
return mixed

hashKey() abstract protected method

Returns a key given parameters. This is up to storage and different values may be returned for the same parameters, as storages are likely to use key-based cache expiration.
abstract protected hashKey ( CacheKey $k )
$k CacheKey

increment() abstract public method

Backend must only guarantee that the increment is made, but the final value may not be current + $value due to concurrent accesses.
abstract public increment ( integer $value, CacheKey $k, integer $default ) : integer
$value integer
$k CacheKey
$default integer If key is not in cache, this value is returned.
return integer

invalidate() public method

Invalidates a dependency index. If the index does not exist it is created.
public invalidate ( CacheKey $k )
$k CacheKey

isEnabled() public method

public isEnabled ( ) : True
return True if cache is enabled, working and storing/retrieving data.

keyFromDeps() protected method

protected keyFromDeps ( CacheKey $k, $deps )
$k CacheKey

log() protected method

Logs cache accesses for debugging
protected log ( string $status, CacheKey $k, integer $lifetime )
$status string CacheLogEnum constant
$k CacheKey The message to print.
$lifetime integer

logHigh() public static method

High level log for testing and debugging
public static logHigh ( $message )

newEnd() public method

public newEnd ( $print = true )

newstart() public method

public newstart ( CacheKey $k, $lifetime = null, $fail = false )
$k CacheKey

prefetch() abstract public method

Backend may ignore this call and implement a noop.
abstract public prefetch ( array $data )
$data array array(0 => CacheKey, ...)

printProbeEnd() protected method

Prints HTML for cache debug probes -> closes tag
protected printProbeEnd ( string $key, CacheData $cachedata )
$key string
$cachedata CacheData

printProbeStart() protected method

Prints HTML for cache debug probes -> opens tag
protected printProbeStart ( string $key, CacheData $cachedata, string $type )
$key string
$cachedata CacheData
$type string

recursiveEnd() public method

public recursiveEnd ( boolean $print = true ) : string
$print boolean
return string The string. If $print == true the string is printed as well.

recursiveStart() public method

public recursiveStart ( CacheKey $k, integer $lifetime = null, boolean $print = true, boolean $fail = false ) : string
$k CacheKey
$lifetime integer if null uses the class default
$print boolean
$fail boolean if true throws a NotCachedException if not cached.
return string The cached item as a string or false if not cached.

recursiveStartP() public method

See also: recursiveStart()
public recursiveStartP ( $base, $id, $sub = null, $lifetime = null, $print = true, $fail = false )

report() abstract public method

Generates a report for this backend
abstract public report ( )

resetLogSummary() public static method

public static resetLogSummary ( )

setDefaultLifetime() public method

public setDefaultLifetime ( number $lifetime )
$lifetime number 0 for infinite

setEnabled() final public method

Enable this cache
final public setEnabled ( $b ) : CacheAbstract
return CacheAbstract this

setLog() public method

public setLog ( boolean $b ) : CacheAbstract
$b boolean
return CacheAbstract

setNamespace() public method

public setNamespace ( string $name )
$name string An optional namespace.

start() public method

Calls extraSub().
public start ( CacheKey $k, string $lifetime = null, boolean $print = true, boolean $fail = false ) : boolean | string
$k CacheKey
$lifetime string The lifetime, in seconds
$print boolean if True echoes the data
$fail boolean if false throws an exception if something happens, such as not cached
return boolean | string True if cached

startCallback() public method

start() using a callable. Same as start()/c()/end().
public startCallback ( CacheKey $k, callable $c, array $cparams = [], integer $lifetime = null )
$k CacheKey
$c callable A callable. Whatever it prints will be cached.
$cparams array parameters for the callback, optional
$lifetime integer

startP() public method

See also: start()
public startP ( $base, $id, $sub = null, $lifetime = null, $print = true, $fail = false )

store() abstract public method

Saves data in cache.
abstract public store ( mixed $data, CacheKey $k, integer $lifetime ) : boolean
$data mixed Data to save in cache
$k CacheKey
$lifetime integer The lifetime in sceonds, although it is up to the implementation whether it is respected or not.
return boolean true if no problem

storeData() public method

Same as store() but expanded parameters
See also: store()
public storeData ( CacheData $data, number $lifetime ) : boolean
$data CacheData
$lifetime number
return boolean true if no problem

storeP() public method

Same as store() but expanded parameters
See also: store()
public storeP ( mixed $data, string $base, string $id, string $sub = null, number $lifetime ) : boolean
$data mixed
$base string
$id string
$sub string
$lifetime number
return boolean true if no problem

Property Details

$cache_log protected_oe property

Stores cache log for debugging.
protected array $cache_log
return array

$debugLogFile public_oe static_oe property

Controls debug to a file
public static $debugLogFile

$debugOnPage public_oe static_oe property

Controls debug on html page for all Cache backends.
public static bool $debugOnPage
return boolean

$enabled protected_oe property

Is this cache enabled?
protected bool $enabled
return boolean

$lifetime protected_oe property

protected $lifetime

$namespace protected_oe property

Initialize it to a unique string. If you are not running multiple instances, ignore.
protected string $namespace
return string

$should_log protected_oe property

Is log enabled? Log can take a lot of RAM, so only turn this on when profiling.
protected bool $should_log
return boolean

$summary protected_oe static_oe property

Array for basic cache profiling. Keys are CacheLogEnum, values are counters.
protected static array $summary
return array