4

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.

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);
jetlej
  • 3,382
  • 6
  • 29
  • 41
  • have a look at the q and my answer here,http://stackoverflow.com/questions/36349568/make-wordpress-wp-api-faster-by-not-loading-theme-and-plugins/36438831#36438831 wp_cache_set needs memcache to persist so you could save the data to file (memcached needs its own server realistically, it keeps values in memory on top of instances of php, mysql etc). But other than that list your db queries (look in the comments on the other answer on the page, you'll prob be shocked how much db data is loaded on every page, not wp fault, usually dev's who use update option with autoload= true – David May 19 '16 at 20:53
  • Why don't you store your results in a [transient](https://codex.wordpress.org/Transients_API)? – dingo_d May 20 '16 at 07:54

2 Answers2

1

I ended up using Cloudflare Page Rules to cache that specific URL. Clean and easy :)

jetlej
  • 3,382
  • 6
  • 29
  • 41
0

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.

  • I am using W3TC, but W3 won't cache pages with GET parameters unless you tell it to always cache pages with GET variables as the same page as the page *without* the parameters. I have other pages on the site that are displayed differently with certain parameters.. – jetlej May 19 '16 at 19:14
  • So, looking again at your code, all you want is to md5 a URL sent as a GET parameter and receive it back as JSON? Do you have any other reason for saving it within WP? I'm thinking you could just exclude WP from this code entirely to improve performance, unless this code is stuffing the WP internal cache for another reason. – Andy Macaulay-Brook May 19 '16 at 19:50
  • No I removed the code that grabs the data from the DB. MD5 is just used to create a cache key from the URL. – jetlej May 19 '16 at 20:54