Smarty_CacheResource_Pdo
PDO Cache Handler Allows you to store Smarty Cache files into your db.
Example table :
CREATE TABLE smarty_cache
(
id
char(40) NOT NULL COMMENT 'sha1 hash',
name
varchar(250) NOT NULL,
cache_id
varchar(250) DEFAULT NULL,
compile_id
varchar(250) DEFAULT NULL,
modified
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
expire
timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
content
mediumblob NOT NULL,
PRIMARY KEY (id
),
KEY name
(name
),
KEY cache_id
(cache_id
),
KEY compile_id
(compile_id
),
KEY modified
(modified
),
KEY expire
(expire
)
) ENGINE=InnoDB
Example usage :
$cnx = new PDO("mysql:host=localhost;dbname=mydb", "username", "password");
$smarty->setCachingType('pdo');
$smarty->loadPlugin('Smarty_CacheResource_Pdo');
$smarty->registerCacheResource('pdo', new Smarty_CacheResource_Pdo($cnx, 'smarty_cache'));
- Full name:
\Smarty_CacheResource_Pdo
- Parent class:
\Smarty_CacheResource_Custom
Properties
fetchStatements
insertStatement
deleteStatement
truncateStatement
fetchColumns
fetchTimestampColumns
pdo
table
database
Methods
__construct
Constructor
Parameters:
Parameter | Type | Description |
---|---|---|
$pdo |
\PDO | PDO : active connection |
$table |
string | : table (or view) name |
$database |
string | : optional - if table is located in another db |
Throws:
fillStatementsWithTableName
Fills the table name into the statements.
Return Value:
Current Instance
getFetchStatement
Gets the fetch statement, depending on what you specify
protected getFetchStatement(string $columns, string $id, string|null $cache_id = null, string|null $compile_id = null): \PDOStatement
Parameters:
Parameter | Type | Description |
---|---|---|
$columns |
string | : the column(s) name(s) you want to retrieve from the database |
$id |
string | unique cache content identifier |
$cache_id |
string|null | cache id |
$compile_id |
string|null | compile id |
fetch
fetch cached content and its modification time from data source
protected fetch(string $id, string $name, string|null $cache_id, string|null $compile_id, string& $content, int& $mtime): void
Parameters:
Parameter | Type | Description |
---|---|---|
$id |
string | unique cache content identifier |
$name |
string | template name |
$cache_id |
string|null | cache id |
$compile_id |
string|null | compile id |
$content |
string | cached content |
$mtime |
int | cache modification timestamp (epoch) |
save
Save content to cache
protected save(string $id, string $name, string|null $cache_id, string|null $compile_id, int|null $exp_time, string $content): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$id |
string | unique cache content identifier |
$name |
string | template name |
$cache_id |
string|null | cache id |
$compile_id |
string|null | compile id |
$exp_time |
int|null | seconds till expiration time in seconds or null |
$content |
string | content to cache |
Return Value:
success
inputContent
Encodes the content before saving to database
Parameters:
Parameter | Type | Description |
---|---|---|
$content |
string |
Return Value:
$content
outputContent
Decodes the content before saving to database
Parameters:
Parameter | Type | Description |
---|---|---|
$content |
string |
Return Value:
$content
delete
Delete content from cache
protected delete(string|null $name = null, string|null $cache_id = null, string|null $compile_id = null, int|null|-1 $exp_time = null): int
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string|null | template name |
$cache_id |
string|null | cache id |
$compile_id |
string|null | compile id |
$exp_time |
int|null|-1 | seconds till expiration or null |
Return Value:
number of deleted caches
getTableName
Gets the formatted table name
Inherited methods
populate
populate Cached Object with meta data from Resource
Parameters:
Parameter | Type | Description |
---|---|---|
$cached |
\Smarty_Template_Cached | cached object |
$_template |
\Smarty_Internal_Template | template object |
populateTimestamp
populate Cached Object with timestamp and exists from Resource
Parameters:
Parameter | Type | Description |
---|---|---|
$cached |
\Smarty_Template_Cached |
process
Read the cached template and process the header
public process(\Smarty_Internal_Template $_smarty_tpl, \Smarty_Template_Cached $cached = null, bool $update = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$_smarty_tpl |
\Smarty_Internal_Template | do not change variable name, is used by compiled template |
$cached |
\Smarty_Template_Cached | cached object |
$update |
bool | flag if called because cache update |
Return Value:
true or false if the cached content does not exist
writeCachedContent
Write the rendered template output to cache
Parameters:
Parameter | Type | Description |
---|---|---|
$_template |
\Smarty_Internal_Template | template object |
$content |
string | content to cache |
Return Value:
success
readCachedContent
Read cached template from cache
Parameters:
Parameter | Type | Description |
---|---|---|
$_template |
\Smarty_Internal_Template | template object |
Return Value:
content
getCachedContent
Return cached content
Parameters:
Parameter | Type | Description |
---|---|---|
$_template |
\Smarty_Internal_Template | template object |
clearAll
Empty cache
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$exp_time |
int | expiration time (number of seconds, not timestamp) |
Return Value:
number of cache files deleted
clear
Empty cache for a specific template
public clear(\Smarty $smarty, string $resource_name, string $cache_id, string $compile_id, int $exp_time): int
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$resource_name |
string | template name |
$cache_id |
string | cache id |
$compile_id |
string | compile id |
$exp_time |
int | expiration time (number of seconds, not timestamp) |
Return Value:
number of cache files deleted
Throws:
locked
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | |
$cached |
\Smarty_Template_Cached |
hasLock
Check is cache is locked for this template
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$cached |
\Smarty_Template_Cached | cached object |
Return Value:
true or false if cache is locked
acquireLock
Lock cache for this template
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$cached |
\Smarty_Template_Cached | cached object |
releaseLock
Unlock cache for this template
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$cached |
\Smarty_Template_Cached | cached object |
load
Load Cache Resource Handler
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$smarty |
\Smarty | Smarty object |
$type |
string | name of the cache resource |
Return Value:
Cache Resource Handler
Throws:
fetch
fetch cached content and its modification time from data source
protected fetch(string $id, string $name, string $cache_id, string $compile_id, string& $content, int& $mtime): void
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$id |
string | unique cache content identifier |
$name |
string | template name |
$cache_id |
string | cache id |
$compile_id |
string | compile id |
$content |
string | cached content |
$mtime |
int | cache modification timestamp (epoch) |
fetchTimestamp
Fetch cached content's modification timestamp from data source {@internal implementing this method is optional.
Only implement it if modification times can be accessed faster than loading the complete cached content.}}
Parameters:
Parameter | Type | Description |
---|---|---|
$id |
string | unique cache content identifier |
$name |
string | template name |
$cache_id |
string | cache id |
$compile_id |
string | compile id |
Return Value:
timestamp (epoch) the template was modified, or false if not found
save
Save content to cache
protected save(string $id, string $name, string $cache_id, string $compile_id, int|null $exp_time, string $content): bool
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$id |
string | unique cache content identifier |
$name |
string | template name |
$cache_id |
string | cache id |
$compile_id |
string | compile id |
$exp_time |
int|null | seconds till expiration or null |
$content |
string | content to cache |
Return Value:
success
delete
Delete content from cache
protected delete(string|null $name, string|null $cache_id, string|null $compile_id, int|null $exp_time): int
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string|null | template name |
$cache_id |
string|null | cache id |
$compile_id |
string|null | compile id |
$exp_time |
int|null | seconds till expiration time in seconds or null |
Return Value:
number of deleted caches
Automatically generated on 2025-03-18