-
Notifications
You must be signed in to change notification settings - Fork 10
/
wp-cache-remember.php
171 lines (144 loc) · 4.94 KB
/
wp-cache-remember.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
<?php
/**
* Plugin Name: WP Cache Remember
* Plugin URI: https://github.com/stevegrunwell/wp-cache-remember
* Description: Helper for the WordPress object cache and transients.
* Author: Steve Grunwell
* Author URI: https://stevegrunwell.com
* Version: 1.1.2
*
* @package SteveGrunwell\WPCacheRemember
*/
if ( ! function_exists( 'wp_cache_remember' ) ) :
/**
* Retrieve a value from the object cache. If it doesn't exist, run the $callback to generate and
* cache the value.
*
* @param string $key The cache key.
* @param callable $callback The callback used to generate and cache the value.
* @param string $group Optional. The cache group. Default is empty.
* @param int $expire Optional. The number of seconds before the cache entry should expire.
* Default is 0 (as long as possible).
*
* @return mixed The value returned from $callback, pulled from the cache when available.
*/
function wp_cache_remember( $key, $callback, $group = '', $expire = 0 ) {
$found = false;
$cached = wp_cache_get( $key, $group, false, $found );
if ( false !== $found ) {
return $cached;
}
$value = $callback();
if ( ! is_wp_error( $value ) ) {
wp_cache_set( $key, $value, $group, $expire );
}
return $value;
}
endif;
if ( ! function_exists( 'wp_cache_forget' ) ) :
/**
* Retrieve and subsequently delete a value from the object cache.
*
* @param string $key The cache key.
* @param string $group Optional. The cache group. Default is empty.
* @param mixed $default Optional. The default value to return if the given key doesn't
* exist in the object cache. Default is null.
*
* @return mixed The cached value, when available, or $default.
*/
function wp_cache_forget( $key, $group = '', $default = null ) {
$found = false;
$cached = wp_cache_get( $key, $group, false, $found );
if ( false !== $found ) {
wp_cache_delete( $key, $group );
return $cached;
}
return $default;
}
endif;
if ( ! function_exists( 'remember_transient' ) ) :
/**
* Retrieve a value from transients. If it doesn't exist, run the $callback to generate and
* cache the value.
*
* @param string $key The transient key.
* @param callable $callback The callback used to generate and cache the value.
* @param int $expire Optional. The number of seconds before the cache entry should expire.
* Default is 0 (as long as possible).
*
* @return mixed The value returned from $callback, pulled from transients when available.
*/
function remember_transient( $key, $callback, $expire = 0 ) {
$cached = get_transient( $key );
if ( false !== $cached ) {
return $cached;
}
$value = $callback();
if ( ! is_wp_error( $value ) ) {
set_transient( $key, $value, $expire );
}
return $value;
}
endif;
if ( ! function_exists( 'forget_transient' ) ) :
/**
* Retrieve and subsequently delete a value from the transient cache.
*
* @param string $key The transient key.
* @param mixed $default Optional. The default value to return if the given key doesn't
* exist in transients. Default is null.
*
* @return mixed The cached value, when available, or $default.
*/
function forget_transient( $key, $default = null ) {
$cached = get_transient( $key );
if ( false !== $cached ) {
delete_transient( $key );
return $cached;
}
return $default;
}
endif;
if ( ! function_exists( 'remember_site_transient' ) ) :
/**
* Retrieve a value from site transients. If it doesn't exist, run the $callback to generate
* and cache the value.
*
* @param string $key The site transient key.
* @param callable $callback The callback used to generate and cache the value.
* @param int $expire Optional. The number of seconds before the cache entry should expire.
* Default is 0 (as long as possible).
*
* @return mixed The value returned from $callback, pulled from transients when available.
*/
function remember_site_transient( $key, $callback, $expire = 0 ) {
$cached = get_site_transient( $key );
if ( false !== $cached ) {
return $cached;
}
$value = $callback();
if ( ! is_wp_error( $value ) ) {
set_site_transient( $key, $value, $expire );
}
return $value;
}
endif;
if ( ! function_exists( 'forget_site_transient' ) ) :
/**
* Retrieve and subsequently delete a value from the site transient cache.
*
* @param string $key The site transient key.
* @param mixed $default Optional. The default value to return if the given key doesn't
* exist in the site transients. Default is null.
*
* @return mixed The cached value, when available, or $default.
*/
function forget_site_transient( $key, $default = null ) {
$cached = get_site_transient( $key );
if ( false !== $cached ) {
delete_site_transient( $key );
return $cached;
}
return $default;
}
endif;