1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helick\CacheHelpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Get a value from the object cache, if one doesn't exist, run the given callback to generate |
7
|
|
|
* and cache the value. |
8
|
|
|
* |
9
|
|
|
* @param string $key |
10
|
|
|
* @param callable $callback |
11
|
|
|
* @param string $group |
12
|
|
|
* @param int $expire |
13
|
|
|
* |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
function cache_remember(string $key, callable $callback, string $group = '', int $expire = 0) |
17
|
|
|
{ |
18
|
|
|
$cached = wp_cache_get($key, $group); |
|
|
|
|
19
|
|
|
|
20
|
|
|
if (false !== $cached) { |
21
|
|
|
return $cached; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$value = $callback(); |
25
|
|
|
|
26
|
|
|
if (!is_wp_error($value)) { |
|
|
|
|
27
|
|
|
wp_cache_set($key, $value, $group, $expire); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $value; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get and subsequently delete a value from the object cache. |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
* @param string $group |
38
|
|
|
* @param mixed $default |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
function cache_forget(string $key, string $group = '', $default = null) |
43
|
|
|
{ |
44
|
|
|
$cached = wp_cache_get($key, $group); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (false !== $cached) { |
47
|
|
|
wp_cache_delete($key, $group); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return $cached; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get a value from the transients, if one doesn't exist, run the given callback to generate |
57
|
|
|
* and cache the value. |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param callable $callback |
61
|
|
|
* @param int $expire |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
function transient_remember(string $key, callable $callback, int $expire = 0) |
66
|
|
|
{ |
67
|
|
|
$cached = get_transient($key); |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (false !== $cached) { |
70
|
|
|
return $cached; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$value = $callback(); |
74
|
|
|
|
75
|
|
|
if (!is_wp_error($value)) { |
|
|
|
|
76
|
|
|
set_transient($key, $value, $expire); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get and subsequently delete a value from the transients. |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* @param mixed $default |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
function transient_forget(string $key, $default = null) |
91
|
|
|
{ |
92
|
|
|
$cached = get_transient($key); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if (false !== $cached) { |
95
|
|
|
delete_transient($key); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $cached; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $default; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get a value from the transients, if one doesn't exist, run the given callback to generate |
105
|
|
|
* and cache the value. |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @param callable $callback |
109
|
|
|
* @param int $expire |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
function site_transient_remember(string $key, callable $callback, int $expire = 0) |
114
|
|
|
{ |
115
|
|
|
$cached = get_site_transient($key); |
|
|
|
|
116
|
|
|
|
117
|
|
|
if (false !== $cached) { |
118
|
|
|
return $cached; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$value = $callback(); |
122
|
|
|
|
123
|
|
|
if (!is_wp_error($value)) { |
|
|
|
|
124
|
|
|
set_site_transient($key, $value, $expire); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get and subsequently delete a value from the site transients. |
132
|
|
|
* |
133
|
|
|
* @param string $key |
134
|
|
|
* @param mixed $default |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
function site_transient_forget(string $key, $default = null) |
139
|
|
|
{ |
140
|
|
|
$cached = get_site_transient($key); |
|
|
|
|
141
|
|
|
|
142
|
|
|
if (false !== $cached) { |
143
|
|
|
delete_site_transient($key); |
|
|
|
|
144
|
|
|
|
145
|
|
|
return $cached; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $default; |
149
|
|
|
} |
150
|
|
|
|