Query cache
Increase database communication performance using in-memory cache storageObject caching is a memory-based, short-term caching solution for MySQL requests. By temporarily storing database query results in memory, object caching significantly reduces the need for repeated queries to the MySQL database. This cache accelerates the retrieval of frequently accessed data, improves server performance, and reduces database load.
Query cache management
The Query cache in the SiteBox infrastructure relies on a Redis server, seamlessly integrating with WordPress that uses a MySQL database. Thanks to the core WordPress cache implementation, various themes and plugins can take advantage of Query Cache without the need for additional integration. Everything operates through the Cache API, simplifying the caching process and making it accessible to a wide range of WordPress components without extra effort.
Example of using cache
/**
* Get user avatar URL and cache it.
*/
function sitebox_get_user_avatar_url(int $userId): ?string
{
$cacheKey = \sprintf('%s|%s', __FUNCTION__, $userId);
$cacheValue = \wp_cache_get($cacheKey, 'sitebox-functions', found: $cacheFound);
if ($cacheFound ?? false) {
return (string) $cacheValue;
}
$avatar = \get_avatar_url($userId);
\wp_cache_set($cacheKey, $avatar, 'sitebox-functions', 3600);
return $avatar || null;
}