2012-07-04 01:23:08 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQL CacheResource
|
|
|
|
* CacheResource Implementation based on the Custom API to use
|
|
|
|
* MySQL as the storage resource for Smarty's output caching.
|
|
|
|
* Table definition:
|
|
|
|
* <pre>CREATE TABLE IF NOT EXISTS `output_cache` (
|
|
|
|
* `id` CHAR(40) NOT NULL COMMENT 'sha1 hash',
|
|
|
|
* `name` VARCHAR(250) NOT NULL,
|
|
|
|
* `cache_id` VARCHAR(250) NULL DEFAULT NULL,
|
|
|
|
* `compile_id` VARCHAR(250) NULL DEFAULT NULL,
|
|
|
|
* `modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
* `content` LONGTEXT NOT NULL,
|
|
|
|
* PRIMARY KEY (`id`),
|
|
|
|
* INDEX(`name`),
|
|
|
|
* INDEX(`cache_id`),
|
|
|
|
* INDEX(`compile_id`),
|
|
|
|
* INDEX(`modified`)
|
|
|
|
* ) ENGINE = InnoDB;</pre>
|
|
|
|
*
|
|
|
|
* @package CacheResource-examples
|
2014-09-07 07:38:28 -04:00
|
|
|
* @author Rodney Rehm
|
2012-07-04 01:23:08 -04:00
|
|
|
*/
|
2014-09-07 07:38:28 -04:00
|
|
|
class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom
|
|
|
|
{
|
2012-07-04 01:23:08 -04:00
|
|
|
// PDO instance
|
|
|
|
protected $db;
|
|
|
|
protected $fetch;
|
|
|
|
protected $fetchTimestamp;
|
|
|
|
protected $save;
|
2014-09-07 07:38:28 -04:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2012-07-04 01:23:08 -04:00
|
|
|
try {
|
2014-09-07 07:38:28 -04:00
|
|
|
$this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty");
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
2012-07-04 01:23:08 -04:00
|
|
|
throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
$this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id');
|
|
|
|
$this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id');
|
|
|
|
$this->save = $this->db->prepare('REPLACE INTO output_cache (id, name, cache_id, compile_id, content)
|
|
|
|
VALUES (:id, :name, :cache_id, :compile_id, :content)');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fetch cached content and its modification time from data source
|
|
|
|
*
|
2014-09-07 07:38:28 -04:00
|
|
|
* @param string $id unique cache content identifier
|
|
|
|
* @param string $name template name
|
|
|
|
* @param string $cache_id cache id
|
|
|
|
* @param string $compile_id compile id
|
|
|
|
* @param string $content cached content
|
|
|
|
* @param integer $mtime cache modification timestamp (epoch)
|
|
|
|
*
|
2012-07-04 01:23:08 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
|
|
|
|
{
|
|
|
|
$this->fetch->execute(array('id' => $id));
|
|
|
|
$row = $this->fetch->fetch();
|
2014-09-07 07:38:28 -04:00
|
|
|
$this->fetch->closeCursor();
|
2012-07-04 01:23:08 -04:00
|
|
|
if ($row) {
|
|
|
|
$content = $row['content'];
|
|
|
|
$mtime = strtotime($row['modified']);
|
|
|
|
} else {
|
|
|
|
$content = null;
|
|
|
|
$mtime = null;
|
|
|
|
}
|
|
|
|
}
|
2014-09-07 07:38:28 -04:00
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
/**
|
|
|
|
* Fetch cached content's modification timestamp from data source
|
|
|
|
*
|
|
|
|
* @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content.
|
2014-09-07 07:38:28 -04:00
|
|
|
*
|
|
|
|
* @param string $id unique cache content identifier
|
|
|
|
* @param string $name template name
|
|
|
|
* @param string $cache_id cache id
|
|
|
|
* @param string $compile_id compile id
|
|
|
|
*
|
2012-07-04 01:23:08 -04:00
|
|
|
* @return integer|boolean timestamp (epoch) the template was modified, or false if not found
|
|
|
|
*/
|
|
|
|
protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
|
|
|
|
{
|
|
|
|
$this->fetchTimestamp->execute(array('id' => $id));
|
|
|
|
$mtime = strtotime($this->fetchTimestamp->fetchColumn());
|
|
|
|
$this->fetchTimestamp->closeCursor();
|
2014-09-07 07:38:28 -04:00
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
return $mtime;
|
|
|
|
}
|
2014-09-07 07:38:28 -04:00
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
/**
|
|
|
|
* Save content to cache
|
|
|
|
*
|
2014-09-07 07:38:28 -04:00
|
|
|
* @param string $id unique cache content identifier
|
|
|
|
* @param string $name template name
|
|
|
|
* @param string $cache_id cache id
|
|
|
|
* @param string $compile_id compile id
|
|
|
|
* @param integer|null $exp_time seconds till expiration time in seconds or null
|
|
|
|
* @param string $content content to cache
|
|
|
|
*
|
|
|
|
* @return boolean success
|
2012-07-04 01:23:08 -04:00
|
|
|
*/
|
|
|
|
protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
|
|
|
|
{
|
|
|
|
$this->save->execute(array(
|
2014-09-07 07:38:28 -04:00
|
|
|
'id' => $id,
|
|
|
|
'name' => $name,
|
|
|
|
'cache_id' => $cache_id,
|
|
|
|
'compile_id' => $compile_id,
|
|
|
|
'content' => $content,
|
|
|
|
));
|
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
return !!$this->save->rowCount();
|
|
|
|
}
|
2014-09-07 07:38:28 -04:00
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
/**
|
|
|
|
* Delete content from cache
|
|
|
|
*
|
2014-09-07 07:38:28 -04:00
|
|
|
* @param string $name template name
|
|
|
|
* @param string $cache_id cache id
|
|
|
|
* @param string $compile_id compile id
|
|
|
|
* @param integer|null $exp_time seconds till expiration or null
|
|
|
|
*
|
|
|
|
* @return integer number of deleted caches
|
2012-07-04 01:23:08 -04:00
|
|
|
*/
|
|
|
|
protected function delete($name, $cache_id, $compile_id, $exp_time)
|
|
|
|
{
|
|
|
|
// delete the whole cache
|
|
|
|
if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) {
|
|
|
|
// returning the number of deleted caches would require a second query to count them
|
|
|
|
$query = $this->db->query('TRUNCATE TABLE output_cache');
|
2014-09-07 07:38:28 -04:00
|
|
|
|
|
|
|
return - 1;
|
2012-07-04 01:23:08 -04:00
|
|
|
}
|
|
|
|
// build the filter
|
|
|
|
$where = array();
|
|
|
|
// equal test name
|
|
|
|
if ($name !== null) {
|
|
|
|
$where[] = 'name = ' . $this->db->quote($name);
|
|
|
|
}
|
|
|
|
// equal test compile_id
|
|
|
|
if ($compile_id !== null) {
|
|
|
|
$where[] = 'compile_id = ' . $this->db->quote($compile_id);
|
|
|
|
}
|
|
|
|
// range test expiration time
|
|
|
|
if ($exp_time !== null) {
|
|
|
|
$where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)';
|
|
|
|
}
|
|
|
|
// equal test cache_id and match sub-groups
|
|
|
|
if ($cache_id !== null) {
|
2014-09-07 07:38:28 -04:00
|
|
|
$where[] = '(cache_id = ' . $this->db->quote($cache_id)
|
|
|
|
. ' OR cache_id LIKE ' . $this->db->quote($cache_id . '|%') . ')';
|
2012-07-04 01:23:08 -04:00
|
|
|
}
|
|
|
|
// run delete query
|
|
|
|
$query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where));
|
2014-09-07 07:38:28 -04:00
|
|
|
|
2012-07-04 01:23:08 -04:00
|
|
|
return $query->rowCount();
|
|
|
|
}
|
|
|
|
}
|