-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheresource.apc.php
107 lines (99 loc) · 3.14 KB
/
cacheresource.apc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Smarty Plugin CacheResource APC
*
* Implements APC resource for the HTML cache
*
* @package Smarty
* @subpackage Cacher
* @author Monte Ohrt
* @author Joe Balancio
*/
/**
* This class does contain all necessary methods for the HTML cache with APC
* @todo Add prefix to stored items. Use APCIterator to remove stored items.
*/
class Smarty_CacheResource_Apc
{
function __construct($smarty)
{
$this->smarty = $smarty;
// test if APC is present
if(!function_exists('apc_store'))
throw new Exception('APC Template Caching Error: APC is not installed');
}
/**
* Returns the filepath of the cached template output
*
* @param object $_template current template
* @return string the cache filepath
*/
public function getCachedFilepath($_template)
{
return md5($_template->getTemplateResource().$_template->cache_id.$template->compile_id);
}
/**
* Returns the timpestamp of the cached template output
*
* @param object $_template current template
* @return integer |booelan the template timestamp or false if the file does not exist
*/
public function getCachedTimestamp($_template)
{
$cacheContent = apc_fetch($this->getCachedFilepath($_template));
return $cacheContent !== false ? $cacheContent[1] : false;
}
/**
* Returns the cached template output
*
* @param object $_template current template
* @return string |booelan the template content or false if the file does not exist
*/
public function getCachedContents($_template, $no_render = false)
{
if (!$no_render) {
ob_start();
}
$_cache_content = apc_fetch($this->getCachedFilepath($_template));
$_smarty_tpl = $_template; // used in the cached template code
eval("?>" . $_cache_content[0]);
if ($no_render) {
return null;
} else {
return ob_get_clean();
}
}
/**
* Writes the rendered template output to cache file
*
* @param object $_template current template
* @return boolean status
*/
public function writeCachedContent($_template, $content)
{
return apc_store($this->getCachedFilepath($_template), array($content, time(), $_template->cache_lifetime), $_template->cache_lifetime);
}
/**
* Empty cache folder
*
* @param integer $exp_time expiration time
* @return integer number of cache files deleted
*/
public function clearAll($exp_time = null)
{
return apc_clear_cache('user');
}
/**
* Empty cache for a specific template
*
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @return integer number of cache files deleted
*/
public function clear($resource_name, $cache_id, $compile_id, $exp_time)
{
return apc_delete(md5($resource_name.$cache_id.$compile_id));
}
}