1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: |
|
8: |
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: |
|
16: | abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
|
17: | {
|
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: |
|
30: | abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
|
31: |
|
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: |
|
44: | protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
|
45: | {
|
46: | return false;
|
47: | }
|
48: |
|
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: |
|
61: | abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
|
62: |
|
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | |
72: |
|
73: | abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
|
74: |
|
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: |
|
83: | public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
|
84: | {
|
85: | $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
|
86: | $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
|
87: | $path = $cached->source->uid . $_cache_id . $_compile_id;
|
88: | $cached->filepath = sha1($path);
|
89: | if ($_template->smarty->cache_locking) {
|
90: | $cached->lock_id = sha1('lock.' . $path);
|
91: | }
|
92: | $this->populateTimestamp($cached);
|
93: | }
|
94: |
|
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: |
|
102: | public function populateTimestamp(Smarty_Template_Cached $cached)
|
103: | {
|
104: | $mtime =
|
105: | $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
|
106: | if ($mtime !== null) {
|
107: | $cached->timestamp = $mtime;
|
108: | $cached->exists = !!$cached->timestamp;
|
109: | return;
|
110: | }
|
111: | $timestamp = null;
|
112: | $this->fetch(
|
113: | $cached->filepath,
|
114: | $cached->source->name,
|
115: | $cached->cache_id,
|
116: | $cached->compile_id,
|
117: | $cached->content,
|
118: | $timestamp
|
119: | );
|
120: | $cached->timestamp = isset($timestamp) ? $timestamp : false;
|
121: | $cached->exists = !!$cached->timestamp;
|
122: | }
|
123: |
|
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: |
|
133: | public function process(
|
134: | Smarty_Internal_Template $_smarty_tpl,
|
135: | Smarty_Template_Cached $cached = null,
|
136: | $update = false
|
137: | ) {
|
138: | if (!$cached) {
|
139: | $cached = $_smarty_tpl->cached;
|
140: | }
|
141: | $content = $cached->content ? $cached->content : null;
|
142: | $timestamp = $cached->timestamp ? $cached->timestamp : null;
|
143: | if ($content === null || !$timestamp) {
|
144: | $this->fetch(
|
145: | $_smarty_tpl->cached->filepath,
|
146: | $_smarty_tpl->source->name,
|
147: | $_smarty_tpl->cache_id,
|
148: | $_smarty_tpl->compile_id,
|
149: | $content,
|
150: | $timestamp
|
151: | );
|
152: | }
|
153: | if (isset($content)) {
|
154: | eval('?>' . $content);
|
155: | $cached->content = null;
|
156: | return true;
|
157: | }
|
158: | return false;
|
159: | }
|
160: |
|
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: |
|
169: | public function writeCachedContent(Smarty_Internal_Template $_template, $content)
|
170: | {
|
171: | return $this->save(
|
172: | $_template->cached->filepath,
|
173: | $_template->source->name,
|
174: | $_template->cache_id,
|
175: | $_template->compile_id,
|
176: | $_template->cache_lifetime,
|
177: | $content
|
178: | );
|
179: | }
|
180: |
|
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: |
|
188: | public function readCachedContent(Smarty_Internal_Template $_template)
|
189: | {
|
190: | $content = $_template->cached->content ? $_template->cached->content : null;
|
191: | $timestamp = null;
|
192: | if ($content === null) {
|
193: | $timestamp = null;
|
194: | $this->fetch(
|
195: | $_template->cached->filepath,
|
196: | $_template->source->name,
|
197: | $_template->cache_id,
|
198: | $_template->compile_id,
|
199: | $content,
|
200: | $timestamp
|
201: | );
|
202: | }
|
203: | if (isset($content)) {
|
204: | return $content;
|
205: | }
|
206: | return false;
|
207: | }
|
208: |
|
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: |
|
217: | public function clearAll(Smarty $smarty, $exp_time = null)
|
218: | {
|
219: | return $this->delete(null, null, null, $exp_time);
|
220: | }
|
221: |
|
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: |
|
234: | public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
|
235: | {
|
236: | $cache_name = null;
|
237: | if (isset($resource_name)) {
|
238: | $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
|
239: | if ($source->exists) {
|
240: | $cache_name = $source->name;
|
241: | } else {
|
242: | return 0;
|
243: | }
|
244: | }
|
245: | return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
|
246: | }
|
247: |
|
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: |
|
256: | public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
257: | {
|
258: | $id = $cached->lock_id;
|
259: | $name = $cached->source->name . '.lock';
|
260: | $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
|
261: | if ($mtime === null) {
|
262: | $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
|
263: | }
|
264: | return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
|
265: | }
|
266: |
|
267: | |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | |
274: |
|
275: | public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
276: | {
|
277: | $cached->is_locked = true;
|
278: | $id = $cached->lock_id;
|
279: | $name = $cached->source->name . '.lock';
|
280: | $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
|
281: | }
|
282: |
|
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: |
|
291: | public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
292: | {
|
293: | $cached->is_locked = false;
|
294: | $name = $cached->source->name . '.lock';
|
295: | $this->delete($name, $cached->cache_id, $cached->compile_id, null);
|
296: | }
|
297: | }
|
298: | |