add some changes
[Snippets.git] / APC.class.php
CommitLineData
93a50078
S
1<?php
2/**
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>
6 */
7class APC {
8 /**
9 * php extension
10 * @var string
11 */
12 protected $apcu = false;
13
14 /**
15 * APC(u) version
16 * @var integer
17 */
18 public $version = 0;
19
20 /**
701c06d4 21 * Constructs a new instance of APC class.
93a50078
S
22 */
23 public function __construct () {
24 if (extension_loaded("apcu")) {
25 $this->apcu = true;
26 $this->version = phpversion('apcu');
5f6347bf
S
27 }
28 else if (extension_loaded("apc")) {
93a50078 29 $this->version = phpversion('apc');
5f6347bf
S
30 }
31 else {
93a50078
S
32 throw new Exception('APC/APCu support is not enabled.');
33 }
34 }
35
36 /**
37 * deletes a cache item
38 *
39 * @param string $key
40 * @return boolean
41 */
42 public function delete ($key) {
43 if ($this->exists($key)) {
44 if ($this->apcu) {
45 return apcu_delete($key);
46 }
47 else {
48 return apc_delete($key);
49 }
50 }
51 }
52
53 /**
54 * fetch a cache item
55 *
56 * @param string $key
57 * @return string
58 */
59 public function fetch ($key) {
60 if ($this->exists($key)) {
61 $cacheTime = $this->getCacheTime($key);
be1fac5b 62 if ($cacheTime['ttl'] > 0 && (time() - $cacheTime['mtime']) > $cacheTime['ttl']) {
93a50078
S
63 $this->delete($key);
64 return null;
65 }
66
67 if ($this->apcu) {
68 return apcu_fetch($key);
69 }
70 else {
71 return apc_fetch($key);
72 }
73 }
74
75 return null;
76 }
77
78 /**
79 * store a cache item
80 *
81 * @param string $key
82 * @param string $var
83 * @param integer $ttl <optional>
84 * @return boolean
85 */
86 public function store ($key, $var, $ttl = 0) {
87 $this->delete($key); // remove cache entry if allready exists
88
89 if ($this->apcu) {
90 apcu_store($key, $var, $ttl);
91 }
92 else {
93 apc_store($key, $var, $ttl);
94 }
95 }
96
97 /**
98 * Checks if APC/APCu key exists
99 *
100 * @param string $key
101 * @return boolean
102 */
103 protected function exists ($key) {
104 $cacheItems = array();
105 foreach ($this->cache_info() as $item) {
106 $cacheItems[] = $item['info'];
107 }
108 return in_array($key, $cacheItems);
109 }
110
111 /**
112 * get cache lifetime
113 *
114 * @param string $key
115 * @return array
116 */
117 protected function getCacheTime ($key) {
118 $cacheItems = array();
119 foreach ($this->cache_info() as $item) {
120 if ($item['info'] == $key) {
121 return array(
122 "ttl" => $item['ttl'],
123 "mtime" => $item['mtime']
124 );
125 }
126 }
127 }
128
129 /**
130 * get cache items
131 *
132 * @param string $cache_type <optional>
133 * @return array
134 */
135 public function cache_info ($cache_type = "") {
136 $info = array();
137
138 if ($this->apcu) {
139 $apcinfo = apcu_cache_info($cache_type);
140 }
141 else {
142 // APC need cache_type = 'user'
143 if ($cache_type == "") $cache_type = "user";
144
145 $apcinfo = apc_cache_info($cache_type);
146 }
147
148 if (isset($apcinfo['cache_list'])) {
149 $cacheList = $apcinfo['cache_list'];
150
151 usort($cacheList, array($this, "usort"));
152
153 foreach ($cacheList as $cache) {
154 // make APCu output compatible with APC
155
156 if (isset($cache['key'])) {
157 $cache['info'] = $cache['key'];
158 unset($cache['key']);
159 }
160
161 if (!isset($cache['type'])) {
162 $cache['type'] = 'user';
163 }
164
165 if (isset($cache['nhits'])) {
166 $cache['num_hits'] = $cache['nhits'];
167 unset($cache['nhits']);
168 }
169
170 if (isset($cache['ctime'])) {
171 $cache['creation_time'] = $cache['ctime'];
172 unset($cache['ctime']);
173 }
174
175 if (isset($cache['dtime'])) {
176 $cache['deletion_time'] = $cache['dtime'];
177 unset($cache['dtime']);
178 }
179
180 if (isset($cache['atime'])) {
181 $cache['access_time'] = $cache['atime'];
182 unset($cache['atime']);
183 }
184
a37c89e6
S
185 if (isset($cache['modification_time'])) {
186 $cache['mtime'] = $cache['modification_time'];
187 unset($cache['modification_time']);
188 }
189
93a50078
S
190 $info[] = $cache;
191 }
192 }
193
194 return $info;
195 }
196
197 /**
198 * sort the given data
199 *
200 * @param array $a
201 * @param array $b
202 * @return array
203 */
204 protected function usort ($a, $b) {
205 if (isset($a['key']) && isset($b['key'])) {
206 return $a['key'] > $b['key'];
207 }
208 else if (isset($a['info']) && isset($b['info'])) {
209 return $a['info'] > $b['info'];
210 }
211 }
212}