| 148 |
lars |
1 |
Custom Cache Implementation {#caching.custom}
|
|
|
2 |
===========================
|
|
|
3 |
|
|
|
4 |
As an alternative to using the default file-based caching mechanism, you
|
|
|
5 |
can specify a custom cache implementation that will be used to read,
|
|
|
6 |
write and clear cached files.
|
|
|
7 |
|
|
|
8 |
> **Note**
|
|
|
9 |
>
|
|
|
10 |
> In Smarty2 this used to be a callback function called
|
|
|
11 |
> `$cache_handler_func`. Smarty3 replaced this callback by the
|
|
|
12 |
> `Smarty_CacheResource` module.
|
|
|
13 |
|
|
|
14 |
With a custom cache implementation you\'re likely trying to achieve at
|
|
|
15 |
least one of the following goals: replace the slow filesystem by a
|
|
|
16 |
faster storage engine, centralize the cache to be accessible to multiple
|
|
|
17 |
servers.
|
|
|
18 |
|
|
|
19 |
Smarty allows CacheResource implementations to use one of the APIs
|
|
|
20 |
`Smarty_CacheResource_Custom` or `Smarty_CacheResource_KeyValueStore`.
|
|
|
21 |
`Smarty_CacheResource_Custom` is a simple API directing all read, write,
|
|
|
22 |
clear calls to your implementation. This API allows you to store
|
|
|
23 |
wherever and however you deem fit. The
|
|
|
24 |
`Smarty_CacheResource_KeyValueStore` API allows you to turn any \"dumb\"
|
|
|
25 |
KeyValue-Store (like APC, Memcache, ...) into a full-featured
|
|
|
26 |
CacheResource implementation. That is, everything around deep
|
|
|
27 |
cache-groups like \"a\|b\|c\" is being handled for you in way that
|
|
|
28 |
allows clearing the cache-group \"a\" and all nested groups are cleared
|
|
|
29 |
as well - even though KeyValue-Stores don\'t allow this kind of
|
|
|
30 |
hierarchy by nature.
|
|
|
31 |
|
|
|
32 |
Custom CacheResources may be put in a file `cacheresource.foobarxyz.php`
|
|
|
33 |
within your [`$plugins_dir`](#variable.plugins.dir), or registered on
|
|
|
34 |
runtime with [`registerCacheResource()`](#api.register.cacheresource).
|
|
|
35 |
In either case you need to set [`$caching_type`](#variable.caching.type)
|
|
|
36 |
to invoke your custom CacheResource implementation.
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
<?php
|
|
|
40 |
|
|
|
41 |
require_once 'libs/Smarty.class.php';
|
|
|
42 |
$smarty = new Smarty();
|
|
|
43 |
$smarty->caching_type = 'mysql';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* MySQL CacheResource
|
|
|
47 |
*
|
|
|
48 |
* CacheResource Implementation based on the Custom API to use
|
|
|
49 |
* MySQL as the storage resource for Smarty's output caching.
|
|
|
50 |
*
|
|
|
51 |
* Table definition:
|
|
|
52 |
* <pre>CREATE TABLE IF NOT EXISTS `output_cache` (
|
|
|
53 |
* `id` CHAR(40) NOT NULL COMMENT 'sha1 hash',
|
|
|
54 |
* `name` VARCHAR(250) NOT NULL,
|
|
|
55 |
* `cache_id` VARCHAR(250) NULL DEFAULT NULL,
|
|
|
56 |
* `compile_id` VARCHAR(250) NULL DEFAULT NULL,
|
|
|
57 |
* `modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
58 |
* `content` LONGTEXT NOT NULL,
|
|
|
59 |
* PRIMARY KEY (`id`),
|
|
|
60 |
* INDEX(`name`),
|
|
|
61 |
* INDEX(`cache_id`),
|
|
|
62 |
* INDEX(`compile_id`),
|
|
|
63 |
* INDEX(`modified`)
|
|
|
64 |
* ) ENGINE = InnoDB;</pre>
|
|
|
65 |
*
|
|
|
66 |
* @package CacheResource-examples
|
|
|
67 |
* @author Rodney Rehm
|
|
|
68 |
*/
|
|
|
69 |
class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom {
|
|
|
70 |
// PDO instance
|
|
|
71 |
protected $db;
|
|
|
72 |
protected $fetch;
|
|
|
73 |
protected $fetchTimestamp;
|
|
|
74 |
protected $save;
|
|
|
75 |
|
|
|
76 |
public function __construct() {
|
|
|
77 |
try {
|
|
|
78 |
$this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
|
|
|
79 |
} catch (PDOException $e) {
|
|
|
80 |
throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
|
|
|
81 |
}
|
|
|
82 |
$this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id');
|
|
|
83 |
$this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id');
|
|
|
84 |
$this->save = $this->db->prepare('REPLACE INTO output_cache (id, name, cache_id, compile_id, content)
|
|
|
85 |
VALUES (:id, :name, :cache_id, :compile_id, :content)');
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* fetch cached content and its modification time from data source
|
|
|
90 |
*
|
|
|
91 |
* @param string $id unique cache content identifier
|
|
|
92 |
* @param string $name template name
|
|
|
93 |
* @param string $cache_id cache id
|
|
|
94 |
* @param string $compile_id compile id
|
|
|
95 |
* @param string $content cached content
|
|
|
96 |
* @param integer $mtime cache modification timestamp (epoch)
|
|
|
97 |
* @return void
|
|
|
98 |
*/
|
|
|
99 |
protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
|
|
|
100 |
{
|
|
|
101 |
$this->fetch->execute(array('id' => $id));
|
|
|
102 |
$row = $this->fetch->fetch();
|
|
|
103 |
$this->fetch->closeCursor();
|
|
|
104 |
if ($row) {
|
|
|
105 |
$content = $row['content'];
|
|
|
106 |
$mtime = strtotime($row['modified']);
|
|
|
107 |
} else {
|
|
|
108 |
$content = null;
|
|
|
109 |
$mtime = null;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Fetch cached content's modification timestamp from data source
|
|
|
115 |
*
|
|
|
116 |
* @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content.
|
|
|
117 |
* @param string $id unique cache content identifier
|
|
|
118 |
* @param string $name template name
|
|
|
119 |
* @param string $cache_id cache id
|
|
|
120 |
* @param string $compile_id compile id
|
|
|
121 |
* @return integer|boolean timestamp (epoch) the template was modified, or false if not found
|
|
|
122 |
*/
|
|
|
123 |
protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
|
|
|
124 |
{
|
|
|
125 |
$this->fetchTimestamp->execute(array('id' => $id));
|
|
|
126 |
$mtime = strtotime($this->fetchTimestamp->fetchColumn());
|
|
|
127 |
$this->fetchTimestamp->closeCursor();
|
|
|
128 |
return $mtime;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Save content to cache
|
|
|
133 |
*
|
|
|
134 |
* @param string $id unique cache content identifier
|
|
|
135 |
* @param string $name template name
|
|
|
136 |
* @param string $cache_id cache id
|
|
|
137 |
* @param string $compile_id compile id
|
|
|
138 |
* @param integer|null $exp_time seconds till expiration time in seconds or null
|
|
|
139 |
* @param string $content content to cache
|
|
|
140 |
* @return boolean success
|
|
|
141 |
*/
|
|
|
142 |
protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
|
|
|
143 |
{
|
|
|
144 |
$this->save->execute(array(
|
|
|
145 |
'id' => $id,
|
|
|
146 |
'name' => $name,
|
|
|
147 |
'cache_id' => $cache_id,
|
|
|
148 |
'compile_id' => $compile_id,
|
|
|
149 |
'content' => $content,
|
|
|
150 |
));
|
|
|
151 |
return !!$this->save->rowCount();
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Delete content from cache
|
|
|
156 |
*
|
|
|
157 |
* @param string $name template name
|
|
|
158 |
* @param string $cache_id cache id
|
|
|
159 |
* @param string $compile_id compile id
|
|
|
160 |
* @param integer|null $exp_time seconds till expiration or null
|
|
|
161 |
* @return integer number of deleted caches
|
|
|
162 |
*/
|
|
|
163 |
protected function delete($name, $cache_id, $compile_id, $exp_time)
|
|
|
164 |
{
|
|
|
165 |
// delete the whole cache
|
|
|
166 |
if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) {
|
|
|
167 |
// returning the number of deleted caches would require a second query to count them
|
|
|
168 |
$query = $this->db->query('TRUNCATE TABLE output_cache');
|
|
|
169 |
return -1;
|
|
|
170 |
}
|
|
|
171 |
// build the filter
|
|
|
172 |
$where = array();
|
|
|
173 |
// equal test name
|
|
|
174 |
if ($name !== null) {
|
|
|
175 |
$where[] = 'name = ' . $this->db->quote($name);
|
|
|
176 |
}
|
|
|
177 |
// equal test compile_id
|
|
|
178 |
if ($compile_id !== null) {
|
|
|
179 |
$where[] = 'compile_id = ' . $this->db->quote($compile_id);
|
|
|
180 |
}
|
|
|
181 |
// range test expiration time
|
|
|
182 |
if ($exp_time !== null) {
|
|
|
183 |
$where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)';
|
|
|
184 |
}
|
|
|
185 |
// equal test cache_id and match sub-groups
|
|
|
186 |
if ($cache_id !== null) {
|
|
|
187 |
$where[] = '(cache_id = '. $this->db->quote($cache_id)
|
|
|
188 |
. ' OR cache_id LIKE '. $this->db->quote($cache_id .'|%') .')';
|
|
|
189 |
}
|
|
|
190 |
// run delete query
|
|
|
191 |
$query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where));
|
|
|
192 |
return $query->rowCount();
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
<?php
|
|
|
200 |
|
|
|
201 |
require_once 'libs/Smarty.class.php';
|
|
|
202 |
$smarty = new Smarty();
|
|
|
203 |
$smarty->caching_type = 'memcache';
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Memcache CacheResource
|
|
|
207 |
*
|
|
|
208 |
* CacheResource Implementation based on the KeyValueStore API to use
|
|
|
209 |
* memcache as the storage resource for Smarty's output caching.
|
|
|
210 |
*
|
|
|
211 |
* Note that memcache has a limitation of 256 characters per cache-key.
|
|
|
212 |
* To avoid complications all cache-keys are translated to a sha1 hash.
|
|
|
213 |
*
|
|
|
214 |
* @package CacheResource-examples
|
|
|
215 |
* @author Rodney Rehm
|
|
|
216 |
*/
|
|
|
217 |
class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore {
|
|
|
218 |
/**
|
|
|
219 |
* memcache instance
|
|
|
220 |
* @var Memcache
|
|
|
221 |
*/
|
|
|
222 |
protected $memcache = null;
|
|
|
223 |
|
|
|
224 |
public function __construct()
|
|
|
225 |
{
|
|
|
226 |
$this->memcache = new Memcache();
|
|
|
227 |
$this->memcache->addServer( '127.0.0.1', 11211 );
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* Read values for a set of keys from cache
|
|
|
232 |
*
|
|
|
233 |
* @param array $keys list of keys to fetch
|
|
|
234 |
* @return array list of values with the given keys used as indexes
|
|
|
235 |
* @return boolean true on success, false on failure
|
|
|
236 |
*/
|
|
|
237 |
protected function read(array $keys)
|
|
|
238 |
{
|
|
|
239 |
$_keys = $lookup = array();
|
|
|
240 |
foreach ($keys as $k) {
|
|
|
241 |
$_k = sha1($k);
|
|
|
242 |
$_keys[] = $_k;
|
|
|
243 |
$lookup[$_k] = $k;
|
|
|
244 |
}
|
|
|
245 |
$_res = array();
|
|
|
246 |
$res = $this->memcache->get($_keys);
|
|
|
247 |
foreach ($res as $k => $v) {
|
|
|
248 |
$_res[$lookup[$k]] = $v;
|
|
|
249 |
}
|
|
|
250 |
return $_res;
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Save values for a set of keys to cache
|
|
|
255 |
*
|
|
|
256 |
* @param array $keys list of values to save
|
|
|
257 |
* @param int $expire expiration time
|
|
|
258 |
* @return boolean true on success, false on failure
|
|
|
259 |
*/
|
|
|
260 |
protected function write(array $keys, $expire=null)
|
|
|
261 |
{
|
|
|
262 |
foreach ($keys as $k => $v) {
|
|
|
263 |
$k = sha1($k);
|
|
|
264 |
$this->memcache->set($k, $v, 0, $expire);
|
|
|
265 |
}
|
|
|
266 |
return true;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* Remove values from cache
|
|
|
271 |
*
|
|
|
272 |
* @param array $keys list of keys to delete
|
|
|
273 |
* @return boolean true on success, false on failure
|
|
|
274 |
*/
|
|
|
275 |
protected function delete(array $keys)
|
|
|
276 |
{
|
|
|
277 |
foreach ($keys as $k) {
|
|
|
278 |
$k = sha1($k);
|
|
|
279 |
$this->memcache->delete($k);
|
|
|
280 |
}
|
|
|
281 |
return true;
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Remove *all* values from cache
|
|
|
286 |
*
|
|
|
287 |
* @return boolean true on success, false on failure
|
|
|
288 |
*/
|
|
|
289 |
protected function purge()
|
|
|
290 |
{
|
|
|
291 |
return $this->memcache->flush();
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|