5

I would like to know if it's a good practice to use transient api to cache social counters for every post. Loading post pages get quite slow while processing and getting all social APIs.

0

1 Answer 1

7

Yes, social counts are a great use case for using transients. Aside from the slow page loading, as you mentioned, this will also help prevent you from blowing through API limits if the external requests are being made on every page load.

Let's say you have it set to cache for 30 minutes and the check for cache expiration is occurring when the data is loaded on the front end, then every 30 minutes you're going to have a slower page load time until the data is re-cached. Ways around that include running the refresh via cron job so that it's not affecting user experience, or checking that the cache is valid on some other hook, such as post edit/post save if the site is super active and has new content posted throughout the day, though that can lead to the social counts becoming a bit stale if there are periods where no new content is being produced.

You can also compute and load the counts via AJAX so that the page loads and then the social counts get brought in, completely removing the load time issue from the front end user.

I highly recommend using Mark Jaquith's TLC Transients plugin. It allows for soft expiration and background updating (by doing the data fetch on the shutdown hook), it also does locking which will save you in race conditions. Example: The cache expired and 10k people hit your page at the same time, they will all fire off processes to recompute the count and cache it until the data has been cached. The locking will check if something else is trying to compute that already and keep the next guy from computing as well. I've had times where 30k requests were being made because of race conditions.

2
  • Hey Tom, thanks for answer. I will definitely look into Mark Jaquith's plugin. Do you maybe have some kind of example where I could get more info about AJAX loads.
    – Webfreak
    Commented Jul 9, 2014 at 14:39
  • Here's a great tutorial by Tom McFarlin on AJAX in WP: tommcfarlin.com/wordpress-ajax-api-example Commented Jul 9, 2014 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.