|
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
|
|
|
* @param $cFileName |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function setCFileName($cFileName) |
|
29
|
|
|
{ |
|
30
|
|
|
static::$cFileName = $cFileName; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function getCFileName() |
|
37
|
|
|
{ |
|
38
|
|
|
return static::$cFileName ?: static::getUniqueString(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
* |
|
44
|
|
|
* Return absolute file path which store cache list |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getCFilePath() |
|
47
|
|
|
{ |
|
48
|
|
|
return sys_get_temp_dir() . '/' . static::getCFileName(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Unique string for each server |
|
53
|
|
|
* @param string $salt |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getUniqueString($salt = '') |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$uniqueString = __DIR__; |
|
59
|
|
|
if (isset($_SERVER['SERVER_SIGNATURE'])) { |
|
60
|
|
|
$uniqueString .= $_SERVER['SERVER_SIGNATURE']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return md5($uniqueString . $salt); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $key |
|
68
|
|
|
* @return bool|string |
|
69
|
|
|
* |
|
70
|
|
|
* Return absolute file path of caching object |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function get($key) |
|
73
|
|
|
{ |
|
74
|
|
|
$cacheList = static::getCacheList(); |
|
75
|
|
|
if (isset($cacheList[$key])) { |
|
76
|
|
|
if (($cacheList[$key][0] == 0 || $cacheList[$key][0] >= time())) { |
|
77
|
|
|
return $cacheList[$key][1]; |
|
78
|
|
|
} else { |
|
79
|
|
|
unlink($cacheList[$key][1]); |
|
80
|
|
|
static::remove($key); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $key |
|
89
|
|
|
* @param $filePath |
|
90
|
|
|
* @return bool |
|
91
|
|
|
* |
|
92
|
|
|
* Save $key and $filePath in cacheList |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function set($key, $filePath, $ttl) |
|
95
|
|
|
{ |
|
96
|
|
|
$cacheList = static::getCacheList(); |
|
97
|
|
|
|
|
98
|
|
|
if ($ttl != 0) { |
|
99
|
|
|
$ttl += time(); |
|
100
|
|
|
} |
|
101
|
|
|
$cacheList[$key] = [$ttl, $filePath]; |
|
102
|
|
|
|
|
103
|
|
|
return static::setCacheList($cacheList); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $key |
|
108
|
|
|
* @return bool |
|
109
|
|
|
* |
|
110
|
|
|
* Remove caching object file and remove from cache list |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function remove($key) |
|
113
|
|
|
{ |
|
114
|
|
|
$cacheList = static::getCacheList(); |
|
115
|
|
|
unset($cacheList[$key]); |
|
116
|
|
|
|
|
117
|
|
|
return static::setCacheList($cacheList); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $key |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function has($key) |
|
125
|
|
|
{ |
|
126
|
|
|
$cacheList = static::getCacheList(); |
|
127
|
|
|
return array_key_exists($key, $cacheList); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
* |
|
133
|
|
|
* Return cache list in array |
|
134
|
|
|
*/ |
|
135
|
|
|
protected static function getCacheList() |
|
136
|
|
|
{ |
|
137
|
|
|
if (file_exists(static::getCFilePath())) { |
|
138
|
|
|
return static::decode(file_get_contents(static::getCFilePath())); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return array(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param $cacheList |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
protected static function setCacheList($cacheList) |
|
149
|
|
|
{ |
|
150
|
|
|
return (bool)file_put_contents(static::getCFilePath(), static::encode($cacheList)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param $array |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
protected static function encode($array) |
|
158
|
|
|
{ |
|
159
|
|
|
return json_encode($array); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $string |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
protected static function decode($string) |
|
167
|
|
|
{ |
|
168
|
|
|
return json_decode($string, true); |
|
169
|
|
|
} |
|
170
|
|
|
} |
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: