1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jaredchu |
5
|
|
|
* Date: 04/08/2017 |
6
|
|
|
* Time: 11:36 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace JC\Cache; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CacheManager |
13
|
|
|
* @package JC |
14
|
|
|
* |
15
|
|
|
* Manage the list of caching file and object |
16
|
|
|
* Save in $cFilename |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $cFileName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $cacheDirectory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $cacheDirectory |
32
|
|
|
*/ |
33
|
|
|
public static function setCacheDirectory($cacheDirectory) { |
34
|
|
|
self::$cacheDirectory = $cacheDirectory; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $cFileName |
39
|
|
|
*/ |
40
|
|
|
public static function setCFileName($cFileName) |
41
|
|
|
{ |
42
|
|
|
static::$cFileName = $cFileName; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public static function getCFileName() |
49
|
|
|
{ |
50
|
|
|
return static::$cFileName; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
* |
56
|
|
|
* Return absolute file path which store cache list |
57
|
|
|
*/ |
58
|
|
|
public static function getCFilePath() |
59
|
|
|
{ |
60
|
|
|
$cacheDirectory = empty(self::$cacheDirectory) ? sys_get_temp_dir() : self::$cacheDirectory; |
61
|
|
|
return $cacheDirectory . '/' . static::getCFileName(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Unique string for each server |
66
|
|
|
* @param string $salt |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public static function getUniqueString($salt = '') |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$uniqueString = isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER['SERVER_SIGNATURE'] . __DIR__ : __DIR__; |
72
|
|
|
return md5($uniqueString . $salt); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $key |
77
|
|
|
* @return bool|string |
78
|
|
|
* |
79
|
|
|
* Return absolute file path of caching object |
80
|
|
|
*/ |
81
|
|
|
public static function get($key) |
82
|
|
|
{ |
83
|
|
|
$cacheList = static::getCacheList(); |
84
|
|
|
if (isset($cacheList[$key])) { |
85
|
|
|
if (($cacheList[$key][0] == 0 || $cacheList[$key][0] >= time())) { |
86
|
|
|
return $cacheList[$key][1]; |
87
|
|
|
} else { |
88
|
|
|
unlink($cacheList[$key][1]); |
89
|
|
|
static::remove($key); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $key |
98
|
|
|
* @param $filePath |
99
|
|
|
* @return bool |
100
|
|
|
* |
101
|
|
|
* Save $key and $filePath in cacheList |
102
|
|
|
*/ |
103
|
|
|
public static function set($key, $filePath, $ttl) |
104
|
|
|
{ |
105
|
|
|
$cacheList = static::getCacheList(); |
106
|
|
|
|
107
|
|
|
if ($ttl != 0) { |
108
|
|
|
$ttl += time(); |
109
|
|
|
} |
110
|
|
|
$cacheList[$key] = [$ttl, $filePath]; |
111
|
|
|
|
112
|
|
|
return static::setCacheList($cacheList); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $key |
117
|
|
|
* @return bool |
118
|
|
|
* |
119
|
|
|
* Remove caching object file and remove from cache list |
120
|
|
|
*/ |
121
|
|
|
public static function remove($key) |
122
|
|
|
{ |
123
|
|
|
$cacheList = static::getCacheList(); |
124
|
|
|
unset($cacheList[$key]); |
125
|
|
|
|
126
|
|
|
return static::setCacheList($cacheList); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $key |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public static function has($key) |
134
|
|
|
{ |
135
|
|
|
$cacheList = static::getCacheList(); |
136
|
|
|
if (is_array($cacheList)) { |
137
|
|
|
return array_key_exists($key, $cacheList); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
* |
146
|
|
|
* Return cache list in array |
147
|
|
|
*/ |
148
|
|
|
protected static function getCacheList() |
149
|
|
|
{ |
150
|
|
|
if (file_exists(static::getCFilePath())) { |
151
|
|
|
return static::decode(file_get_contents(static::getCFilePath())); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return array(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $cacheList |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
protected static function setCacheList($cacheList) |
162
|
|
|
{ |
163
|
|
|
return (bool)file_put_contents(static::getCFilePath(), static::encode($cacheList)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $array |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected static function encode($array) |
171
|
|
|
{ |
172
|
|
|
return json_encode($array); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $string |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
protected static function decode($string) |
180
|
|
|
{ |
181
|
|
|
return json_decode($string, true); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: