source: pro-violet-viettel/sourcecode/application/third_party/Smarty/sysplugins/smarty_cacheresource_custom.php

Last change on this file was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 8.0 KB
Line 
1<?php
2/**
3 * Smarty Internal Plugin
4 *
5 * @package Smarty
6 * @subpackage Cacher
7 */
8
9/**
10 * Cache Handler API
11 *
12 * @package Smarty
13 * @subpackage Cacher
14 * @author Rodney Rehm
15 */
16abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource {
17
18    /**
19     * fetch cached content and its modification time from data source
20     *
21     * @param string $id         unique cache content identifier
22     * @param string $name       template name
23     * @param string $cache_id   cache id
24     * @param string $compile_id compile id
25     * @param string $content    cached content
26     * @param integer $mtime cache modification timestamp (epoch)
27     * @return void
28     */
29    protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
30
31    /**
32     * Fetch cached content's modification timestamp from data source
33     *
34     * {@internal implementing this method is optional.
35     *  Only implement it if modification times can be accessed faster than loading the complete cached content.}}
36     *
37     * @param string $id         unique cache content identifier
38     * @param string $name       template name
39     * @param string $cache_id   cache id
40     * @param string $compile_id compile id
41     * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
42     */
43    protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
44    {
45        return null;
46    }
47
48    /**
49     * Save content to cache
50     *
51     * @param string       $id         unique cache content identifier
52     * @param string       $name       template name
53     * @param string       $cache_id   cache id
54     * @param string       $compile_id compile id
55     * @param integer|null $exp_time   seconds till expiration or null
56     * @param string $content content to cache
57     * @return boolean success
58     */
59    protected abstract function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
60
61    /**
62     * Delete content from cache
63     *
64     * @param string       $name       template name
65     * @param string       $cache_id   cache id
66     * @param string       $compile_id compile id
67     * @param integer|null $exp_time   seconds till expiration time in seconds or null
68     * @return integer number of deleted caches
69     */
70    protected abstract function delete($name, $cache_id, $compile_id, $exp_time);
71
72    /**
73     * populate Cached Object with meta data from Resource
74     *
75     * @param Smarty_Template_Cached   $cached    cached object
76     * @param Smarty_Internal_Template $_template template object
77     * @return void
78     */
79    public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
80    {
81        $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
82        $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
83
84        $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
85        $this->populateTimestamp($cached);
86    }
87
88    /**
89     * populate Cached Object with timestamp and exists from Resource
90     *
91     * @param Smarty_Template_Cached $source cached object
92     * @return void
93     */
94    public function populateTimestamp(Smarty_Template_Cached $cached)
95    {
96        $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
97        if ($mtime !== null) {
98            $cached->timestamp = $mtime;
99            $cached->exists = !!$cached->timestamp;
100            return;
101        }
102        $timestamp = null;
103        $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
104        $cached->timestamp = isset($timestamp) ? $timestamp : false;
105        $cached->exists = !!$cached->timestamp;
106    }
107
108    /**
109     * Read the cached template and process the header
110     *
111     * @param Smarty_Internal_Template $_template template object
112     * @param Smarty_Template_Cached $cached cached object
113     * @return booelan true or false if the cached content does not exist
114     */
115    public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
116    {
117        if (!$cached) {
118            $cached = $_template->cached;
119        }
120        $content = $cached->content ? $cached->content : null;
121        $timestamp = $cached->timestamp ? $cached->timestamp : null;
122        if ($content === null || !$timestamp) {
123            $this->fetch(
124                $_template->cached->filepath,
125                $_template->source->name,
126                $_template->cache_id,
127                $_template->compile_id,
128                $content,
129                $timestamp
130            );
131        }
132        if (isset($content)) {
133            $_smarty_tpl = $_template;
134            eval("?>" . $content);
135            return true;
136        }
137        return false;
138    }
139
140    /**
141     * Write the rendered template output to cache
142     *
143     * @param Smarty_Internal_Template $_template template object
144     * @param string                   $content   content to cache
145     * @return boolean success
146     */
147    public function writeCachedContent(Smarty_Internal_Template $_template, $content)
148    {
149        return $this->save(
150            $_template->cached->filepath,
151            $_template->source->name,
152            $_template->cache_id,
153            $_template->compile_id,
154            $_template->properties['cache_lifetime'],
155            $content
156        );
157    }
158
159    /**
160     * Empty cache
161     *
162     * @param Smarty  $smarty   Smarty object
163     * @param integer $exp_time expiration time (number of seconds, not timestamp)
164     * @return integer number of cache files deleted
165     */
166    public function clearAll(Smarty $smarty, $exp_time=null)
167    {
168        $this->cache = array();
169        return $this->delete(null, null, null, $exp_time);
170    }
171
172    /**
173     * Empty cache for a specific template
174     *
175     * @param Smarty  $smarty        Smarty object
176     * @param string  $resource_name template name
177     * @param string  $cache_id      cache id
178     * @param string  $compile_id    compile id
179     * @param integer $exp_time      expiration time (number of seconds, not timestamp)
180     * @return integer number of cache files deleted
181     */
182    public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
183    {
184        $this->cache = array();
185        return $this->delete($resource_name, $cache_id, $compile_id, $exp_time);
186    }
187   
188    /**
189     * Check is cache is locked for this template
190     *
191     * @param Smarty $smarty Smarty object
192     * @param Smarty_Template_Cached $cached cached object
193     * @return booelan true or false if cache is locked
194     */
195    public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
196    {
197        $id = $cached->filepath;
198        $name = $cached->source->name . '.lock';
199       
200        $mtime = $this->fetchTimestamp($id, $name, null, null);
201        if ($mtime === null) {
202            $this->fetch($id, $name, null, null, $content, $mtime);
203        }
204       
205        return $mtime && time() - $mtime < $smarty->locking_timeout;
206    }
207
208    /**
209     * Lock cache for this template
210     *
211     * @param Smarty $smarty Smarty object
212     * @param Smarty_Template_Cached $cached cached object
213     */
214    public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
215    {
216        $cached->is_locked = true;
217       
218        $id = $cached->filepath;
219        $name = $cached->source->name . '.lock';
220        $this->save($id, $name, null, null, $smarty->locking_timeout, '');
221    }
222
223    /**
224     * Unlock cache for this template
225     *
226     * @param Smarty $smarty Smarty object
227     * @param Smarty_Template_Cached $cached cached object
228     */
229    public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
230    {
231        $cached->is_locked = false;
232       
233        $id = $cached->filepath;
234        $name = $cached->source->name . '.lock';
235        $this->delete($name, null, null, null);
236    }
237}
238?>
Note: See TracBrowser for help on using the repository browser.