📅  最后修改于: 2023-12-03 15:06:02.278000             🧑  作者: Mango
wp_cache_get()
is a WordPress function that retrieves a cached value from the cache by its key.
wp_cache_get( string $key, string $group = '', bool $force = false, bool &$found = null );
Parameters
$key
(string) (required): The cache key to retrieve the value for.$group
(string) (optional): The group to which the cache belongs. Default is an empty string (which means use the default cache group).$force
(bool) (optional): Whether to force a cache miss. Default is false.&$found
(bool) (optional): Whether the value was found in the cache. Default is null.Return Value
The cached value on success or false
on failure.
$key = 'my_cached_data';
$group = 'my_cache_group';
// Try to get data from cache
$cached_data = wp_cache_get($key, $group);
// If data is not found, get it from a database or other source and save it to cache
if ($cached_data === false) {
$cached_data = get_data_from_database();
wp_cache_set($key, $cached_data, $group);
}
// Use the cached data
echo $cached_data;
wp_cache_get()
is a wrapper function for the WordPress caching API. It retrieves values from the cache using the backend cache drop-in that is currently set up (e.g. Memcached, Redis, APC, or the file cache).$group
parameter is useful when you want to separate cached data into different groups or categories. For example, you might cache page content for different languages using different groups.$force
parameter is useful when you want to refresh the cache of a specific key. Set it to true
and wp_cache_get()
will return false
, indicating that the cached value was not found. Then, you can get data from a database or other source and save it to cache using wp_cache_set()
.$found
parameter is passed by reference and is used to determine whether the cached value was found or not. If the cached value is found, $found
is set to true
. Otherwise, it is set to false
.