SKR Tips logo SKR TipsWebsite tips that actually work
WordPress

WordPress Transients for Caching Live Game Data

Our clan site had a block on the front page that showed the current server status and the top five players from the ladder. It pulled both from the game's public endpoint on every single page load.

A laptop showing a WordPress dashboard beside a notebook listing cache expiry times for a game ladder widget

Our clan site had a block on the front page that showed the current server status and the top five players from the ladder. It pulled both from the game's public endpoint on every single page load. For about four months nobody noticed, because our traffic was forty people checking raid times. Then a patch dropped, a streamer linked us, and the front page started taking nine seconds to render while the host emailed me about CPU.

The fix took twenty minutes and it was not a plugin. It was the transients API, which has shipped with WordPress since 2.8 and which almost nobody building a community site seems to know about. If your site reads anything from outside, a ladder, a patch feed, a Discord member count, a store price, this is the piece you are missing.

What a transient actually is

A transient is a named value with an expiry date. You hand WordPress a key, a value and a number of seconds, and it gives the value back to you until that time runs out. After that it returns false and you go fetch fresh data.

Three functions do the whole job. set_transient() stores, get_transient() reads, and delete_transient() throws a value away early. The pattern you want looks like this in plain terms: ask for the transient, and if it comes back false, call the remote endpoint with wp_remote_get(), store the result, then use it. Every visitor after the first one in that window gets the stored copy.

Where the value physically lives depends on your host. With no object cache it goes into the wp_options table as two rows, one for the value and one for the timeout. With Redis or Memcached in front of WordPress it goes into memory instead and never touches the database at all. That difference matters later, so hold onto it.

The one thing a transient is not is permanent storage. The name is doing honest work. If you cannot regenerate the value, it does not belong in a transient.

Choosing the expiry, which is the only hard part

Developers agonise over the code and then pick the expiry at random. It is the other way round. The code is four lines; the expiry is the decision that determines whether your site is fast and wrong or slow and right.

WordPress gives you constants so you do not have to count seconds: MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS. Here is roughly how I set them on a site that tracks a live game.

What you are fetchingExpiry I useReasoning
Server status, up or down2 to 5 minutesPlayers check this during an outage; stale data here makes you look broken
Ladder or leaderboard top ten15 to 30 minutesPositions rarely change faster than that, and nobody refreshes to watch rank 7 move
Patch notes feed1 hour, plus a manual purge buttonPatches land unpredictably, so you want the override more than a short window
Item or cosmetic prices6 hoursDaily rotation means the data is barely fresher than that at source
A static list, such as class or map names1 weekChanges once a season; the week keeps it from going permanently stale

Two habits save trouble. Put the remote host's rate limit in a comment next to the expiry, because the two are related and the next person will not know. And always include a way to clear a transient by hand. I added a link under Tools that calls delete_transient() and redirects back, which has saved me from explaining stale patch notes in Discord more times than I can count.

Where the usual advice falls down

Ask about a slow site in a WordPress group and the first answer is always install a caching plugin. That advice is not wrong, it is aimed at a different problem, and on a community site it hides the real one for just long enough to hurt.

A page cache serves a saved copy of HTML to visitors who are not logged in. On a brochure site that is nearly everyone, which is why the advice works there. On a clan or guild site a large share of your traffic is logged in, and logged in users bypass the page cache by design, because otherwise one member would see another member's dashboard. Those people hit your uncached code path and pay the full cost of the remote call. So does every admin page load, every editor previewing a guide, and every cron run.

Our nine second front page was being served from a page cache the entire time. The cache was working perfectly. It was just that the twelve of us doing the actual work were the ones waiting.

There is a second trap, and it is specific to transients. Under an external object cache, transients live in memory with no guaranteed lifetime. Redis will evict them under memory pressure well before your expiry, so a transient you set for a week might vanish in an hour. That is fine for caching. It is a disaster if you have quietly started using transients to hold something like a queue of pending submissions, which I have seen done on a fan wiki and which lost about thirty entries before anyone worked out why. Regenerable data only.

The pattern that removes the wait entirely

Basic transient caching still leaves one unlucky visitor holding the bag. When the expiry passes, the next person through the door waits for the remote request, and if the endpoint is slow or down they wait for the timeout. On our site that was a two second penalty roughly every fifteen minutes, which is not terrible, except that the unlucky visitor was often a bot that then recorded the slow response.

The better arrangement is to let a scheduled job do the fetching and let visitors only ever read. Register a recurring event with wp_schedule_event(), have its callback hit the endpoint and call set_transient() with an expiry noticeably longer than the schedule interval, say a 45 minute expiry on a 15 minute refresh. Front end code then only calls get_transient() and renders whatever is there, with a quiet fallback if it is empty.

Nobody on the site ever waits for the network again. The staleness window stays small, and the overlap between expiry and interval means two missed cron runs do not blank your widget. If scheduled jobs on your install are unreliable, and on low traffic sites they often are because WordPress only fires cron when someone visits, that is worth fixing before you lean on this pattern.

While you are in there, check what else is sitting in your options table. Expired transients do not always get cleaned up, and a site that has been running on this pattern for two years can accumulate thousands of orphaned rows, which is one of the things worth hunting during a database cleanup on a busy fan site. If the data you are caching is heading into structured pages rather than a sidebar widget, it also pairs well with keeping guides in their own custom post type so the cached values and the editorial content stay separate.

FAQ

Do I need a plugin to use transients?

No. The functions are core, so a small snippet in a child theme's functions file or a tiny site specific plugin is enough. Using a plugin for this mostly means someone else chose your expiry times for you.

What happens to transients when the site is not visited?

They sit there until something asks for them. Expiry is checked on read, not by a background sweeper, so an expired transient can stay in the database for months before anything notices. That is why cleanup matters on long running sites.

Should I use the options API instead for long lived data?

Yes, if the data cannot be fetched again. Store it with update_option() and set autoload to false so it is not loaded on every page. Reserve transients for anything you could rebuild from the source in a second.

How do I tell whether my host has an object cache?

Look for an object-cache.php file in your wp-content directory. If it is there, transients are in memory and subject to eviction. If it is not, they are database rows and your expiry times are honoured exactly.

MO
Matthias Okeke

Matthias ran a clan website and a fan wiki for years before writing about them. He covers the sites gaming communities build, from guides to patch note pages.

More posts by Matthias

More in WordPress

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto