1 | <?php |
||
21 | class FileCache extends AbstractCache |
||
22 | { |
||
23 | /** |
||
24 | * @param $cacheDir the default cache dir |
||
25 | * @param $cacheTime the default cache time (5 minutes) |
||
26 | */ |
||
27 | |||
28 | protected $cacheDir = "cache/queryCache/"; |
||
29 | protected $cacheTime = 300; |
||
30 | |||
31 | /** |
||
32 | * @param string $cd a dir to use instead of default |
||
33 | */ |
||
34 | public function __construct($cd = null) |
||
35 | { |
||
36 | global $cacheDir; |
||
37 | if (isset($cacheDir) && $cd === null) { |
||
38 | $cd = $cacheDir; |
||
39 | } |
||
40 | |||
41 | if (!is_null($cd)) $this->cacheDir = $cd; |
||
42 | if (!is_dir($this->cacheDir)) mkdir($this->cacheDir); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Gets the data |
||
47 | * |
||
48 | * @param string $key |
||
49 | * @return array|boolean |
||
50 | */ |
||
51 | public function get($key) |
||
52 | { |
||
53 | if(file_exists($this->cacheDir.sha1($key))) |
||
54 | { |
||
55 | $time = time(); |
||
56 | $data = self::getData($key); |
||
57 | $age = $data["age"]; |
||
58 | $data = json_decode($data["data"], true); |
||
59 | if($age <= $time) |
||
60 | { |
||
61 | @unlink($this->cacheDir.sha1($key)); |
||
|
|||
62 | return false; |
||
63 | } |
||
64 | return $data; |
||
65 | } |
||
66 | else |
||
67 | return false; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Sets data |
||
72 | * |
||
73 | * @param string $key |
||
74 | * @param string|array $value |
||
75 | * @param string $timeout |
||
76 | * |
||
77 | * return bool |
||
78 | */ |
||
79 | public function set($key, $value, $timeout) |
||
83 | |||
84 | /** |
||
85 | * Replaces data |
||
86 | * |
||
87 | * @param string $key |
||
88 | * @param string|array $value |
||
89 | * @param string $timeout |
||
90 | * @return boolean |
||
91 | */ |
||
92 | public function replace($key, $value, $timeout) |
||
93 | { |
||
94 | if(file_exists($this->cacheDir.sha1($key))) |
||
95 | { |
||
96 | @unlink($this->cacheDir.sha1($key)); |
||
97 | if(self::setData($key, $value, $timeout) !== false) |
||
98 | return true; |
||
99 | } |
||
100 | |||
101 | return false; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Deletes a key |
||
106 | * |
||
107 | * @param string $key |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function delete($key) |
||
111 | { |
||
112 | try |
||
113 | { |
||
114 | @unlink($this->cacheDir.sha1($key)); |
||
115 | } |
||
116 | catch (Exception $e) |
||
117 | { |
||
118 | return false; |
||
119 | } |
||
120 | return true; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Increments value |
||
125 | * |
||
126 | * @param string $key |
||
127 | * @param int $step |
||
128 | * @param int $timeout |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function increment($key, $step = 1, $timeout = 0) |
||
132 | { |
||
133 | $data = self::getData($key); |
||
134 | $data = json_decode($data["data"], true); |
||
135 | |||
136 | try |
||
137 | { |
||
138 | @unlink($this->cacheDir.sha1($key)); |
||
139 | return self::setData($key, $data+$step, $timeout); |
||
140 | } |
||
141 | catch (Exception $e) |
||
142 | { |
||
143 | return false; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Decrements value |
||
149 | * |
||
150 | * @param string $key |
||
151 | * @param int $step |
||
152 | * @param int $timeout |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function decrement($key, $step = 1, $timeout = 0) |
||
156 | { |
||
157 | $data = self::getData($key); |
||
158 | $data = json_decode($data["data"], true); |
||
159 | |||
160 | try |
||
161 | { |
||
162 | @unlink($this->cacheDir.sha1($key)); |
||
163 | return self::setData($key, $data-$step, $timeout); |
||
164 | } |
||
165 | catch (Exception $e) |
||
166 | { |
||
167 | return false; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Flushes the cache |
||
173 | */ |
||
174 | public function flush() |
||
175 | { |
||
176 | $dir = opendir($this->cacheDir); |
||
177 | while($file = readdir($dir)) |
||
178 | { |
||
179 | @unlink($this->cacheDir.$file); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Sets data to cache file |
||
185 | * |
||
186 | * @param string $key |
||
187 | * @param string|array $value |
||
188 | * @param int $timeout |
||
189 | * |
||
190 | * return bool |
||
191 | */ |
||
192 | private function setData($key, $value, $timeout = NULL) |
||
212 | |||
213 | /** |
||
214 | * Gets the data from the cache |
||
215 | * |
||
216 | * @param string $key |
||
217 | * @param bool $sha |
||
218 | * @return array |
||
219 | */ |
||
220 | private function getData($key, $sha = true) |
||
233 | |||
234 | /** |
||
235 | * Cleans up old and unused query cache files |
||
236 | */ |
||
237 | public function cleanUp() |
||
254 | } |
||
255 |
If you suppress an error, we recommend checking for the error condition explicitly: