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: {
14: /**
15: * Smarty_CacheResource_Apc constructor.
16: *
17: * @throws \Exception
18: */
19: public function __construct()
20: {
21: // test if APC is present
22: if (!function_exists('apc_cache_info')) {
23: throw new Exception('APC Template Caching Error: APC is not installed');
24: }
25: }
26:
27: /**
28: * Read values for a set of keys from cache
29: *
30: * @param array $keys list of keys to fetch
31: *
32: * @return array list of values with the given keys used as indexes
33: * @return boolean true on success, false on failure
34: */
35: protected function read(array $keys)
36: {
37: $_res = array();
38: $res = apc_fetch($keys);
39: foreach ($res as $k => $v) {
40: $_res[ $k ] = $v;
41: }
42: return $_res;
43: }
44:
45: /**
46: * Save values for a set of keys to cache
47: *
48: * @param array $keys list of values to save
49: * @param int $expire expiration time
50: *
51: * @return boolean true on success, false on failure
52: */
53: protected function write(array $keys, $expire = null)
54: {
55: foreach ($keys as $k => $v) {
56: apc_store($k, $v, $expire);
57: }
58: return true;
59: }
60:
61: /**
62: * Remove values from cache
63: *
64: * @param array $keys list of keys to delete
65: *
66: * @return boolean true on success, false on failure
67: */
68: protected function delete(array $keys)
69: {
70: foreach ($keys as $k) {
71: apc_delete($k);
72: }
73: return true;
74: }
75:
76: /**
77: * Remove *all* values from cache
78: *
79: * @return boolean true on success, false on failure
80: */
81: protected function purge()
82: {
83: return apc_clear_cache('user');
84: }
85: }
86: