Does wp_cache still load WordPress and use MySQL?

I have a JSON API endpoint that uses wp_cache_set/wp_cache_get to store the result. This endpoint is hit hundreds of thousands of times in a day.

However this often takes down my server as it seems the cache is still accessing MySQL and/or loading WordPress.

Read More

Is this true? And if so what would be a better caching solution to make this as light as possible? (eg. memcached)

Here’s code in case that’s helpful:

define('WP_USE_THEMES', false);
require_once('../../../wp-blog-header.php');
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

if(!$image_url) $image_url = $_GET['url'];

if(!$image_url) return false;

$cacheTitle = md5($image_url) . '1';

$result = wp_cache_get( $cacheTitle );
$notCached = $result ? false : true;

if ($notCached){

    /** Insert code here to get the data I need and store it in $result **/

    wp_cache_set( $cacheTitle, $result );

}

return json_encode($result);

Related posts

Leave a Reply

2 comments

  1. You’re using caching routines within WP that are designed to save you repeating db queries and other expensive operations. So you’re saving repeating your md5 but you’re still running WP for every page load.

    So, yes, to avoid the hit of running WP each time you do need some kind of page cache. Personally I’ve been using the w3totalcache plugin which has worked well. My next step will likely be a move to Varnish, but there are a few choices out there.