Smarty Cache Handler Base for Key/Value Storage Implementations

This class implements the functionality required to use simple key/value stores for hierarchical cache groups. key/value stores like memcache or APC do not support wildcards in keys, therefore a cache group cannot be cleared like "a|*" - which is no problem to filesystem and RDBMS implementations.

This implementation is based on the concept of invalidation. While one specific cache can be identified and cleared, any range of caches cannot be identified. For this reason each level of the cache group hierarchy can have its own value in the store. These values are nothing but microtimes, telling us when a particular cache group was cleared for the last time. These keys are evaluated for every cache read to determine if the cache has been invalidated since it was created and should hence be treated as inexistent.

Although deep hierarchies are possible, they are not recommended. Try to keep your cache groups as shallow as possible. Anything up 3-5 parents should be ok. So »a|b|c« is a good depth where »a|b|c|d|e|f|g|h|i|j|k« isn't. Try to join correlating cache groups: if your cache groups look somewhat like »a|b|$page|$items|$whatever« consider using »a|b|c|$page-$items-$whatever« instead.

package Smarty
subpackage Cacher
author Rodney Rehm

 Methods

Lock cache for this template

acquireLock(\Smarty $smarty, \Smarty_Template_Cached $cached) 

Parameters

$smarty

\Smarty

Smarty object

$cached

\Smarty_Template_Cached

cached object

Empty cache for a specific template

clear(\Smarty $smarty, string $resource_name, string $cache_id, string $compile_id, integer $exp_time) : integer

uses to generate the CacheID
uses to mark CacheIDs parent chain as outdated
uses to remove CacheID from cache

Parameters

$smarty

\Smarty

Smarty object

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

$exp_time

integer

expiration time [being ignored]

Returns

integernumber of cache files deleted [always -1]

Empty cache

clearAll(\Smarty $smarty, integer $exp_time) : integer

uses to clear the whole store
uses to mark everything outdated if purge() is inapplicable

Parameters

$smarty

\Smarty

Smarty object

$exp_time

integer

expiration time [being ignored]

Returns

integernumber of cache files deleted [always -1]

Return cached content

getCachedContent(\Smarty_Internal_Template $_template) 
Inherited
inherited_from \Smarty_CacheResource::getCachedContent()

Parameters

$_template

\Smarty_Internal_Template

template object

Check is cache is locked for this template

hasLock(\Smarty $smarty, \Smarty_Template_Cached $cached) : \booelan

Parameters

$smarty

\Smarty

Smarty object

$cached

\Smarty_Template_Cached

cached object

Returns

\booelantrue or false if cache is locked

Invalid Loaded Cache Files

invalidLoadedCache(\Smarty $smarty) 
InheritedStatic
inherited_from \Smarty_CacheResource::invalidLoadedCache()

Parameters

$smarty

\Smarty

Smarty object

Load Cache Resource Handler

load(\Smarty $smarty, string $type) : \Smarty_CacheResource
InheritedStatic
inherited_from \Smarty_CacheResource::load()

Parameters

$smarty

\Smarty

Smarty object

$type

string

name of the cache resource

Returns

\Smarty_CacheResourceCache Resource Handler

locked()

locked(\Smarty $smarty, \Smarty_Template_Cached $cached) 
Inherited
inherited_from \Smarty_CacheResource::locked()

Parameters

$smarty

$cached

populate Cached Object with meta data from Resource

populate(\Smarty_Template_Cached $cached, \Smarty_Internal_Template $_template) : void

Parameters

$cached

\Smarty_Template_Cached

cached object

$_template

\Smarty_Internal_Template

template object

populate Cached Object with timestamp and exists from Resource

populateTimestamp(\Smarty_Template_Cached $cached) : void

Parameters

$cached

\Smarty_Template_Cached

cached object

Read the cached template and process the header

process(\Smarty_Internal_Template $_template, \Smarty_Template_Cached $cached) : \booelan

Parameters

$_template

\Smarty_Internal_Template

template object

$cached

\Smarty_Template_Cached

cached object

Returns

\booelantrue or false if the cached content does not exist

Unlock cache for this template

releaseLock(\Smarty $smarty, \Smarty_Template_Cached $cached) 

Parameters

$smarty

\Smarty

Smarty object

$cached

\Smarty_Template_Cached

cached object

Write the rendered template output to cache

writeCachedContent(\Smarty_Internal_Template $_template, string $content) : boolean

Parameters

$_template

\Smarty_Internal_Template

template object

$content

string

content to cache

Returns

booleansuccess

Add current microtime to the beginning of $cache_content

addMetaTimestamp(string $content) 

Parameters

$content

string

&$content the content to be cached

Remove values from cache

delete(array $keys) : boolean

Parameters

$keys

array

list of keys to delete

Returns

booleantrue on success, false on failure

Fetch and prepare a cache object.

fetch(string $cid, string $resource_name, string $cache_id, string $compile_id, string $content, integer $timestamp, string $resource_uid) : boolean

Parameters

$cid

string

CacheID to fetch

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

$content

string

cached content

$timestamp

integer

&$timestamp cached timestamp (epoch)

$resource_uid

string

resource's uid

Returns

booleansuccess

Determine the latest timestamp known to the invalidation chain

getLatestInvalidationTimestamp(string $cid, string $resource_name, string $cache_id, string $compile_id, string $resource_uid) : float

Parameters

$cid

string

CacheID to determine latest invalidation timestamp of

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

$resource_uid

string

source's filepath

Returns

floatthe microtime the CacheID was invalidated

Extract the timestamp the $content was cached

getMetaTimestamp(string $content) : float

Parameters

$content

string

&$content the cached content

Returns

floatthe microtime the content was cached

Get template's unique ID

getTemplateUid(\Smarty $smarty, string $resource_name, string $cache_id, string $compile_id) : string

Parameters

$smarty

\Smarty

Smarty object

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

Returns

stringfilepath of cache file

Invalidate CacheID

invalidate(string $cid, string $resource_name, string $cache_id, string $compile_id, string $resource_uid) : void

Parameters

$cid

string

CacheID

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

$resource_uid

string

source's uid

Translate a CacheID into the list of applicable InvalidationKeys.

listInvalidationKeys(string $cid, string $resource_name, string $cache_id, string $compile_id, string $resource_uid) : array

Splits "some|chain|into|an|array" into array( '#clearAll#', 'some', 'some|chain', 'some|chain|into', ... )

uses to prepend to each InvalidationKey

Parameters

$cid

string

CacheID to translate

$resource_name

string

template name

$cache_id

string

cache id

$compile_id

string

compile id

$resource_uid

string

source's filepath

Returns

arraylist of InvalidationKeys

Remove *all* values from cache

purge() : boolean

Returns

booleantrue on success, false on failure

Read values for a set of keys from cache

read(array $keys) : array

Parameters

$keys

array

list of keys to fetch

Returns

arraylist of values with the given keys used as indexes

Sanitize CacheID components

sanitize(string $string) : string

Parameters

$string

string

CacheID component to sanitize

Returns

stringsanitized CacheID component

Save values for a set of keys to cache

write(array $keys, int $expire) : boolean

Parameters

$keys

array

list of values to save

$expire

int

expiration time

Returns

booleantrue on success, false on failure

 Properties

 

$resources : array
Inherited
inherited_from \Smarty_CacheResource::$$resources
 

$contents : array
 

$sysplugins : array
Inherited
inherited_from \Smarty_CacheResource::$$sysplugins
 

$timestamps : array