-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCache.php
More file actions
215 lines (192 loc) · 5.08 KB
/
Cache.php
File metadata and controls
215 lines (192 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Copyright 2020 MDS Technologies (Pty) Ltd and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0).
* It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0
*
* @author MDS Collivery <integration@collivery.co.za>
* @copyright 2020 MDS Technologies (Pty) Ltd
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
namespace Mds;
class Cache
{
/**
* @type string
*/
private $cache_dir;
/**
* @type
*/
private $cache;
/**
* @param string $cache_dir
*/
public function __construct($cache_dir = 'cache/mds_collivery/')
{
if ($cache_dir === null) {
$cache_dir = 'cache/mds_collivery/';
}
$this->cache_dir = $cache_dir;
}
/**
* Creates the cache directory
*
* @param $dir_array
*/
protected function createDir($dir_array)
{
if (!is_array($dir_array)) {
$dir_array = explode('/', $this->cache_dir);
}
array_pop($dir_array);
$dir = implode('/', $dir_array);
if ($dir!='' && ! is_dir($dir)) {
$this->createDir($dir_array);
mkdir($dir);
}
}
/**
* Loads a specific cache file else creates the cache directory
*
* @param $name
* @return mixed
*/
protected function load($name)
{
if (! isset($this->cache[ $name ])) {
if (file_exists($this->cache_dir . $name) && $content = file_get_contents($this->cache_dir . $name)) {
$this->cache[ $name ] = json_decode($content, true);
return $this->cache[ $name ];
} else {
$this->createDir($this->cache_dir);
}
} else {
return $this->cache[ $name ];
}
}
/**
* Determines if a specific cache file exists and is valid
*
* @param $name
* @return bool
*/
public function has($name)
{
$cache = $this->load($name);
if (is_array($cache)) {
if ($cache['valid'] === 0 || ($cache['valid'] - 30) > time()) {
return (!empty($cache['value'])) ? true : false;
}
}
return false;
}
/**
* Gets a specific cache files contents
*
* @param $name
* @return null
*/
public function get($name)
{
$cache = $this->load($name);
if (is_array($cache)) {
if ($cache['valid'] === 0 || ($cache['valid'] - 30) > time()) {
return (!empty($cache['value'])) ? $cache['value'] : null;
}
}
return null;
}
/**
* Creates a specific cache file
*
* @param $name
* @param $value
* @param int $time
* @return bool
*/
public function put($name, $value, $time = 1440)
{
$cache = array( 'value' => $value, 'valid' => time() + ($time*60) );
if (file_put_contents($this->cache_dir . $name, json_encode($cache))) {
$this->cache[ $name ] = $cache;
return true;
} else {
return false;
}
}
/**
* Forgets a specific cache file
*
* @param $name
* @return bool
*/
public function forget($name)
{
$cache = array( 'value' => '', 'valid' => 0 );
if (file_put_contents($this->cache_dir . $name, json_encode($cache))) {
$this->cache[ $name ] = $cache;
return true;
} else {
return false;
}
}
/**
* Clears all cache or only files matching a group name
*
* @param null|string|array $group
*/
public function clear($group = null)
{
$map = $this->directoryMap();
if (is_array($group)) {
foreach ($group as $row) {
$this->forgetEachFile($map, $row);
}
} else {
$this->forgetEachFile($map, $group);
}
}
/**
* Forgets each file in the directory map or only files matching a group name
*
* @param array $map
* @param null $group
*/
private function forgetEachFile(array $map, $group = null)
{
foreach ($map as $row) {
if ($group) {
if (preg_match('|' . preg_quote($group) . '|', $row)) {
$this->forget($row);
}
} else {
$this->forget($row);
}
}
}
/**
* Maps all files in the cache directory
*
* @return array|bool
*/
private function directoryMap()
{
if ($fp = @opendir($this->cache_dir)) {
$file_data = array();
while (false !== ($file = readdir($fp))) {
// Remove '.', '..', and hidden files
if (!trim($file, '.') or ($file[0] == '.')) {
continue;
}
$file_data[] = $file;
}
closedir($fp);
return $file_data;
}
return false;
}
}