Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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 Jul 9 at 14:39
    
Here's a great tutorial by Tom McFarlin on AJAX in WP: tommcfarlin.com/wordpress-ajax-api-example –  TomHarrigan Jul 9 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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