helick /
cache-helpers
| 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); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 19 | |||||||
| 20 | if (false !== $cached) { |
||||||
| 21 | return $cached; |
||||||
| 22 | } |
||||||
| 23 | |||||||
| 24 | $value = $callback(); |
||||||
| 25 | |||||||
| 26 | if (!is_wp_error($value)) { |
||||||
|
0 ignored issues
–
show
The function
is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 27 | wp_cache_set($key, $value, $group, $expire); |
||||||
|
0 ignored issues
–
show
The function
wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The function
wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 45 | |||||||
| 46 | if (false !== $cached) { |
||||||
| 47 | wp_cache_delete($key, $group); |
||||||
|
0 ignored issues
–
show
The function
wp_cache_delete was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The function
get_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 68 | |||||||
| 69 | if (false !== $cached) { |
||||||
| 70 | return $cached; |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | $value = $callback(); |
||||||
| 74 | |||||||
| 75 | if (!is_wp_error($value)) { |
||||||
|
0 ignored issues
–
show
The function
is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 76 | set_transient($key, $value, $expire); |
||||||
|
0 ignored issues
–
show
The function
set_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The function
get_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 93 | |||||||
| 94 | if (false !== $cached) { |
||||||
| 95 | delete_transient($key); |
||||||
|
0 ignored issues
–
show
The function
delete_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The function
get_site_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 116 | |||||||
| 117 | if (false !== $cached) { |
||||||
| 118 | return $cached; |
||||||
| 119 | } |
||||||
| 120 | |||||||
| 121 | $value = $callback(); |
||||||
| 122 | |||||||
| 123 | if (!is_wp_error($value)) { |
||||||
|
0 ignored issues
–
show
The function
is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 124 | set_site_transient($key, $value, $expire); |
||||||
|
0 ignored issues
–
show
The function
set_site_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The function
get_site_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 141 | |||||||
| 142 | if (false !== $cached) { |
||||||
| 143 | delete_site_transient($key); |
||||||
|
0 ignored issues
–
show
The function
delete_site_transient was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 144 | |||||||
| 145 | return $cached; |
||||||
| 146 | } |
||||||
| 147 | |||||||
| 148 | return $default; |
||||||
| 149 | } |
||||||
| 150 |