Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 16 | */ |
9f49773e | 17 | class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface { |
0d64c8fa | 18 | |
6714d8d2 SL |
19 | // TODO Consider native implementation. |
20 | use CRM_Utils_Cache_NaiveMultipleTrait; | |
21 | // TODO Native implementation | |
22 | use CRM_Utils_Cache_NaiveHasTrait; | |
0d64c8fa | 23 | |
6a488035 | 24 | const DEFAULT_TIMEOUT = 3600; |
353ffa53 | 25 | const DEFAULT_PREFIX = ''; |
6a488035 TO |
26 | |
27 | /** | |
fe482240 | 28 | * The default timeout to use. |
6a488035 TO |
29 | * |
30 | * @var int | |
31 | */ | |
32 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
33 | ||
34 | /** | |
35 | * The prefix prepended to cache keys. | |
36 | * | |
37 | * If we are using the same memcache instance for multiple CiviCRM | |
38 | * installs, we must have a unique prefix for each install to prevent | |
39 | * the keys from clobbering each other. | |
40 | * | |
41 | * @var string | |
42 | */ | |
43 | protected $_prefix = self::DEFAULT_PREFIX; | |
44 | ||
45 | /** | |
fe482240 | 46 | * Constructor. |
6a488035 | 47 | * |
77855840 TO |
48 | * @param array $config |
49 | * An array of configuration params. | |
6a488035 | 50 | * |
77b97be7 | 51 | * @return \CRM_Utils_Cache_APCcache |
6a488035 | 52 | */ |
00be9182 | 53 | public function __construct(&$config) { |
6a488035 TO |
54 | if (isset($config['timeout'])) { |
55 | $this->_timeout = intval($config['timeout']); | |
56 | } | |
57 | if (isset($config['prefix'])) { | |
58 | $this->_prefix = $config['prefix']; | |
59 | } | |
60 | } | |
61 | ||
5bc392e6 EM |
62 | /** |
63 | * @param $key | |
64 | * @param $value | |
858451a9 | 65 | * @param null|int|\DateInterval $ttl |
5bc392e6 EM |
66 | * |
67 | * @return bool | |
68 | */ | |
858451a9 | 69 | public function set($key, $value, $ttl = NULL) { |
9e1a686f TO |
70 | CRM_Utils_Cache::assertValidKey($key); |
71 | if (is_int($ttl) && $ttl <= 0) { | |
72 | return $this->delete($key); | |
858451a9 | 73 | } |
9e1a686f TO |
74 | |
75 | $ttl = CRM_Utils_Date::convertCacheTtl($ttl, $this->_timeout); | |
76 | $expires = time() + $ttl; | |
77 | if (!apc_store($this->_prefix . $key, ['e' => $expires, 'v' => $value], $ttl)) { | |
6a488035 TO |
78 | return FALSE; |
79 | } | |
80 | return TRUE; | |
81 | } | |
82 | ||
5bc392e6 EM |
83 | /** |
84 | * @param $key | |
2da67cc5 | 85 | * @param mixed $default |
5bc392e6 EM |
86 | * |
87 | * @return mixed | |
88 | */ | |
2da67cc5 | 89 | public function get($key, $default = NULL) { |
9e1a686f TO |
90 | CRM_Utils_Cache::assertValidKey($key); |
91 | $result = apc_fetch($this->_prefix . $key, $success); | |
92 | if ($success && isset($result['e']) && $result['e'] > time()) { | |
93 | return $this->reobjectify($result['v']); | |
2da67cc5 | 94 | } |
9e1a686f | 95 | return $default; |
6a488035 TO |
96 | } |
97 | ||
5bc392e6 EM |
98 | /** |
99 | * @param $key | |
100 | * | |
101 | * @return bool|string[] | |
102 | */ | |
00be9182 | 103 | public function delete($key) { |
9e1a686f TO |
104 | CRM_Utils_Cache::assertValidKey($key); |
105 | apc_delete($this->_prefix . $key); | |
106 | return TRUE; | |
6a488035 TO |
107 | } |
108 | ||
00be9182 | 109 | public function flush() { |
6a488035 TO |
110 | $allinfo = apc_cache_info('user'); |
111 | $keys = $allinfo['cache_list']; | |
6714d8d2 SL |
112 | // Our keys follows this pattern: ([A-Za-z0-9_]+)?CRM_[A-Za-z0-9_]+ |
113 | $prefix = $this->_prefix; | |
114 | // Get prefix length | |
115 | $lp = strlen($prefix); | |
6a488035 TO |
116 | |
117 | foreach ($keys as $key) { | |
118 | $name = $key['info']; | |
6c552737 | 119 | if ($prefix == substr($name, 0, $lp)) { |
a1a2a83d | 120 | // Ours? |
9e1a686f | 121 | apc_delete($name); |
6a488035 TO |
122 | } |
123 | } | |
124e5288 | 124 | return TRUE; |
6a488035 | 125 | } |
96025800 | 126 | |
c31de879 TO |
127 | public function clear() { |
128 | return $this->flush(); | |
129 | } | |
130 | ||
9e1a686f TO |
131 | private function reobjectify($value) { |
132 | return is_object($value) ? unserialize(serialize($value)) : $value; | |
133 | } | |
134 | ||
6a488035 | 135 | } |