3 * @author Jan Altensen (Stricted)
4 * @copyright 2013-2014 Jan Altensen (Stricted)
5 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 protected $apcu = false;
21 * Constructs a new instance of APC class.
23 public function __construct () {
24 if (extension_loaded("apcu")) {
26 $this->version
= phpversion('apcu');
28 else if (extension_loaded("apc")) {
29 $this->version
= phpversion('apc');
32 throw new Exception('APC/APCu support is not enabled.');
37 * deletes a cache item
42 public function delete ($key) {
43 if ($this->exists($key)) {
45 return apcu_delete($key);
48 return apc_delete($key);
59 public function fetch ($key) {
60 if ($this->exists($key)) {
61 $cacheTime = $this->getCacheTime($key);
62 if ($cacheTime['ttl'] > 0 && (time() - $cacheTime['mtime']) > $cacheTime['ttl']) {
68 return apcu_fetch($key);
71 return apc_fetch($key);
83 * @param integer $ttl <optional>
86 public function store ($key, $var, $ttl = 0) {
87 $this->delete($key); // remove cache entry if allready exists
90 apcu_store($key, $var, $ttl);
93 apc_store($key, $var, $ttl);
98 * Checks if APC/APCu key exists
103 protected function exists ($key) {
104 $cacheItems = array();
105 foreach ($this->cache_info() as $item) {
106 $cacheItems[] = $item['info'];
108 return in_array($key, $cacheItems);
117 protected function getCacheTime ($key) {
118 $cacheItems = array();
119 foreach ($this->cache_info() as $item) {
120 if ($item['info'] == $key) {
122 "ttl" => $item['ttl'],
123 "mtime" => $item['mtime']
132 * @param string $cache_type <optional>
135 public function cache_info ($cache_type = "") {
139 $apcinfo = apcu_cache_info($cache_type);
142 // APC need cache_type = 'user'
143 if ($cache_type == "") $cache_type = "user";
145 $apcinfo = apc_cache_info($cache_type);
148 if (isset($apcinfo['cache_list'])) {
149 $cacheList = $apcinfo['cache_list'];
151 usort($cacheList, array($this, "usort"));
153 foreach ($cacheList as $cache) {
154 // make APCu output compatible with APC
156 if (isset($cache['key'])) {
157 $cache['info'] = $cache['key'];
158 unset($cache['key']);
161 if (!isset($cache['type'])) {
162 $cache['type'] = 'user';
165 if (isset($cache['nhits'])) {
166 $cache['num_hits'] = $cache['nhits'];
167 unset($cache['nhits']);
170 if (isset($cache['ctime'])) {
171 $cache['creation_time'] = $cache['ctime'];
172 unset($cache['ctime']);
175 if (isset($cache['dtime'])) {
176 $cache['deletion_time'] = $cache['dtime'];
177 unset($cache['dtime']);
180 if (isset($cache['atime'])) {
181 $cache['access_time'] = $cache['atime'];
182 unset($cache['atime']);
185 if (isset($cache['modification_time'])) {
186 $cache['mtime'] = $cache['modification_time'];
187 unset($cache['modification_time']);
198 * sort the given data
204 protected function usort ($a, $b) {
205 if (isset($a['key']) && isset($b['key'])) {
206 return $a['key'] > $b['key'];
208 else if (isset($a['info']) && isset($b['info'])) {
209 return $a['info'] > $b['info'];