1 <?php |
|
2 |
|
3 /** |
|
4 * APC CacheResource |
|
5 * CacheResource Implementation based on the KeyValueStore API to use |
|
6 * memcache as the storage resource for Smarty's output caching. |
|
7 * * |
|
8 * |
|
9 * @package CacheResource-examples |
|
10 * @author Uwe Tews |
|
11 */ |
|
12 class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore { |
|
13 public function __construct() { |
|
14 // test if APC is present |
|
15 if (!function_exists('apc_cache_info')) { |
|
16 throw new Exception('APC Template Caching Error: APC is not installed'); |
|
17 } |
|
18 } |
|
19 |
|
20 /** |
|
21 * Read values for a set of keys from cache |
|
22 * |
|
23 * @param array $keys list of keys to fetch |
|
24 * |
|
25 * @return array list of values with the given keys used as indexes |
|
26 * @return boolean true on success, false on failure |
|
27 */ |
|
28 protected function read(array $keys) { |
|
29 $_res = array(); |
|
30 $res = apc_fetch($keys); |
|
31 foreach ($res as $k => $v) { |
|
32 $_res[$k] = $v; |
|
33 } |
|
34 |
|
35 return $_res; |
|
36 } |
|
37 |
|
38 /** |
|
39 * Save values for a set of keys to cache |
|
40 * |
|
41 * @param array $keys list of values to save |
|
42 * @param int $expire expiration time |
|
43 * |
|
44 * @return boolean true on success, false on failure |
|
45 */ |
|
46 protected function write(array $keys, $expire = null) { |
|
47 foreach ($keys as $k => $v) { |
|
48 apc_store($k, $v, $expire); |
|
49 } |
|
50 |
|
51 return true; |
|
52 } |
|
53 |
|
54 /** |
|
55 * Remove values from cache |
|
56 * |
|
57 * @param array $keys list of keys to delete |
|
58 * |
|
59 * @return boolean true on success, false on failure |
|
60 */ |
|
61 protected function delete(array $keys) { |
|
62 foreach ($keys as $k) { |
|
63 apc_delete($k); |
|
64 } |
|
65 |
|
66 return true; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Remove *all* values from cache |
|
71 * |
|
72 * @return boolean true on success, false on failure |
|
73 */ |
|
74 protected function purge() { |
|
75 return apc_clear_cache('user'); |
|
76 } |
|
77 } |
|