-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheService.php
211 lines (185 loc) · 5.53 KB
/
CacheService.php
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
<?php
class CacheService {
const DOMAIN = 'cached';
/**
* Generates a transient key.
*
* @param string $key The unique key for the cache.
*
* @return string The transient key.
*/
private static function _generateTransientKey($key) {
return self::DOMAIN . '_' . md5($key);
}
/**
* Sets an item to the cache.
*
* @param string $key The unique key for the cache.
* @param mixed $value The data to cache.
* @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24).
*
* @return void
*/
private static function set($key, $value, $expiration) {
if (!$key || !is_string($key)) {
throw new \InvalidArgumentException('Invalid cache key');
}
if (!is_numeric($expiration)) {
throw new \InvalidArgumentException('Invalid expiration');
}
$transientKey = self::_generateTransientKey($key);
return set_transient($transientKey, $value, $expiration);
}
/**
* Gets an item from the cache
*
* @param string $key The unique key for the cache.
* @param mixed $default (Optional) If value doesn't exist, return default
*
* @return mixed The item from the cache.
*/
public static function get($key, $default = false) {
if (!$key || !is_string($key)) {
throw new \InvalidArgumentException('Invalid cache key');
}
$transientKey = self::_generateTransientKey($key);
$cached = get_transient($transientKey);
return ($cached !== false ? $cached : $default);
}
/**
* Deletes an item from the cache
*
* @param string $key The unique key for the cache.
*
* @return void
*/
public static function delete($key) {
if (!$key || !is_string($key)) {
throw new \InvalidArgumentException('Invalid cache key');
}
$transientKey = self::_generateTransientKey($key);
delete_transient($transientKey);
}
/**
* Deletes an item from the cache
*
* @param string $key The unique key for the cache.
*
* @return void
*/
public static function forget($key) {
self::delete($key);
}
/**
* Retrieve an item from the cache and then delete it
*
* @param string $key The unique key for the cache.
*
* @return mixed The cached data.
*/
public static function pull($key, $default) {
$cached = self::get($key, $default);
self::delete($key);
return $cached;
}
/**
* Sets an item to the cache.
*
* @param string $key The unique key for the cache.
* @param mixed $value The data to cache.
* @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24).
*
* @return void
*/
public static function put($key, $value, $expiration) {
self::set($key, $value, $expiration);
}
/**
* Adds an item to cache if it doesn't previously exist.
*
* @param string $key The unique key for the cache.
* @param mixed $value The data to cache.
* @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24).
*
* @return bool True if added to cache
*/
public static function add($key, $value, $expiration) {
if (!self::has($key)) {
self::put($key, $value, $expiration);
return true;
}
return false;
}
/**
* Adds an item to cache without expiration.
*
* @param string $key The unique key for the cache.
* @param mixed $value The data to cache.
*
* @return bool True if added to cache
*/
public static function forever($key, $value) {
return self::put($key, $value, 0);
}
/**
* Checks if an item exists in the cache.
*
* @param string $key The unique key for the cache.
*
* @return boolean True if item exists.
*/
public static function has($key) {
$cached = self::get($key);
if ($cached !== false) {
return true;
}
return false;
}
/**
* Remembers a value.
*
* @param string $key The unique key for the cache
* @param callable $cb Callback that runs if the cache has expired.
* @param array $params (optional) Callback parameters
*
* @return mixed Depending on the callback.
*/
public static function rememberForever($key, callable $cb, array $params = array()) {
return self::remember($key, 0, $cb, $params);
}
/**
* Remembers a value for a set amount of time.
*
* @param string $key The unique key for the cache
* @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24).
* @param callable $cb Callback that runs if the cache has expired.
* @param array $params (optional) Callback parameters
*
* @return mixed Depending on the callback.
*/
public static function remember($key, $expiration, callable $cb, array $params = array()) {
$cached = self::get($key);
if ($cached === false) {
$cached = call_user_func_array($cb, $params);
self::set($key, $cached, $expiration);
}
return $cached;
}
/**
* Clears the cache completely
*
* @return void
*/
public static function emptyCache() {
global $wpdb;
$cachedItems = $wpdb->get_results("
SELECT *
FROM {$wpdb->options}
WHERE option_name LIKE '_transient_" . self::DOMAIN . "%'
");
foreach($cachedItems as $item) {
$cachedKey = str_replace('_transient_', '', $item->option_name);
delete_transient($cachedKey);
}
}
}