5 * CacheResource Implementation based on the Custom API to use
6 * MySQL as the storage resource for Smarty's output caching.
8 * <pre>CREATE TABLE IF NOT EXISTS `output_cache` (
9 * `id` CHAR(40) NOT NULL COMMENT 'sha1 hash',
10 * `name` VARCHAR(250) NOT NULL,
11 * `cache_id` VARCHAR(250) NULL DEFAULT NULL,
12 * `compile_id` VARCHAR(250) NULL DEFAULT NULL,
13 * `modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
14 * `content` LONGTEXT NOT NULL,
18 * INDEX(`compile_id`),
20 * ) ENGINE = InnoDB;</pre>
22 * @package CacheResource-examples
25 class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom
32 protected $fetchTimestamp;
36 public function __construct()
39 $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty");
41 catch (PDOException $e) {
42 throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
44 $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id');
45 $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id');
46 $this->save = $this->db->prepare('REPLACE INTO output_cache (id, name, cache_id, compile_id, content)
47 VALUES (:id, :name, :cache_id, :compile_id, :content)');
51 * fetch cached content and its modification time from data source
53 * @param string $id unique cache content identifier
54 * @param string $name template name
55 * @param string $cache_id cache id
56 * @param string $compile_id compile id
57 * @param string $content cached content
58 * @param integer $mtime cache modification timestamp (epoch)
62 protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
64 $this->fetch->execute(array('id' => $id));
65 $row = $this->fetch->fetch();
66 $this->fetch->closeCursor();
68 $content = $row[ 'content' ];
69 $mtime = strtotime($row[ 'modified' ]);
77 * Fetch cached content's modification timestamp from data source
79 * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content.
81 * @param string $id unique cache content identifier
82 * @param string $name template name
83 * @param string $cache_id cache id
84 * @param string $compile_id compile id
86 * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
88 protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
90 $this->fetchTimestamp->execute(array('id' => $id));
91 $mtime = strtotime($this->fetchTimestamp->fetchColumn());
92 $this->fetchTimestamp->closeCursor();
98 * Save content to cache
100 * @param string $id unique cache content identifier
101 * @param string $name template name
102 * @param string $cache_id cache id
103 * @param string $compile_id compile id
104 * @param integer|null $exp_time seconds till expiration time in seconds or null
105 * @param string $content content to cache
107 * @return boolean success
109 protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
111 $this->save->execute(array('id' => $id, 'name' => $name, 'cache_id' => $cache_id, 'compile_id' => $compile_id,
112 'content' => $content,));
114 return !!$this->save->rowCount();
118 * Delete content from cache
120 * @param string $name template name
121 * @param string $cache_id cache id
122 * @param string $compile_id compile id
123 * @param integer|null $exp_time seconds till expiration or null
125 * @return integer number of deleted caches
127 protected function delete($name, $cache_id, $compile_id, $exp_time)
129 // delete the whole cache
130 if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) {
131 // returning the number of deleted caches would require a second query to count them
132 $query = $this->db->query('TRUNCATE TABLE output_cache');
139 if ($name !== null) {
140 $where[] = 'name = ' . $this->db->quote($name);
142 // equal test compile_id
143 if ($compile_id !== null) {
144 $where[] = 'compile_id = ' . $this->db->quote($compile_id);
146 // range test expiration time
147 if ($exp_time !== null) {
148 $where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)';
150 // equal test cache_id and match sub-groups
151 if ($cache_id !== null) {
152 $where[] = '(cache_id = ' . $this->db->quote($cache_id) . ' OR cache_id LIKE ' .
153 $this->db->quote($cache_id . '|%') . ')';
156 $query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where));
158 return $query->rowCount();