Not All WP Code Is Public Code
If you are going to release something public, then all the things kovshenin said are perfectly valid.
Things are different if you are going to write private code for yourself or your company.
External Object Cache Is A Big Benefit, In Any Case
To set a external persistent object cache is very recommended, when you can.
All the things said in the kovshenin's answer about transients and MySQL are very true, and considering that WP itself and a bunch of plugins make use of object cache... then the performance improvement you got, absolutely worth the (small) effort to setup a modern cache system like Redis or Memcached.
Cached Values May Not Be There: That's Fine
Moreover, yes, an external object cache is not reliable. You should never rely on the fact that a transient is there. You need to make sure it works if cached are not where they should be.
Cache is notstorage, cache is cache.
Use Cache Selectively
See this example:
function my_get_some_value($key) {
// by default no cache when debug and if no external object_cache
$defUse = ! (defined('WP_DEBUG') && WP_DEBUG) && wp_using_ext_object_cache();
// make the usage of cache filterable
$useCache = apply_filters('my_use_cache', $defUse);
// return cached value if any
if ($useCache && ($cached = get_transient($key))) {
return $cached;
}
// no cached value, make sure your code works with no cache
$value = my_get_some_value_in_some_expensive_way();
// set cache, if allowed
$useCache and set_transient($key, $value, HOUR_IN_SECONDS);
return $value;
}
Using a code like this, in your private site, site performance can improve a lot, especially if you have a lot of users.
Note that:
- By default the cache is not used when debug is on, so hopefully on your development environment. Believe me, cache can make debug an hell
- By default the cache is also not used when WP is not set to use an external object cache. It means that all the problem connected with MySQL does not exist, because you use no transient when they use MySQL. A probably easier alternative would be to use
wp_cache_*
functions, so if no external cache is setup, then the cache happen in memory, and database is never involved.
- The usage of cache is filterable, to handle some edge cases you may encounter
No Webscale If No Cache
You should not try to solve speed issues with cache. If you have speed issues, then you should re-think you code.
But to scale a website at webscale, cache is pretty required.
And a lot of times (but not always) fragment, context-aware cache is much more flexible and suitable than aggressive fullpage caching.
Your Questions:
Should I use Transient API at all here?
It depends.
Is your code consuming a lot of resources? If not, maybe there's no need of cache.
As said, is not just a matter of speed. If your code run fast but it requires a bunch of CPU and memory for a couple users... what happen when you have 100 or 1000 concurrent users?
If you realize cache would be a good idea..
...and is public code: probably no. You can consider to cache selectively, like in my example above in public code, but usually is better if you leave such decisions to implementers.
...and is private code: very probably yes. But even for private code, to cache selectively is still a good thing, for example for debug.
Remember, anyway, that wp_cache_*
functions can give you access to cache without the risk of polluting database.
Should I use Transient API to cache $related_posts array, or $html_output string?
It depends on a lot of things. How big are the string? Which external cache are you using? If you are going to cache posts, storing ID as array can be a good idea, querying a decent number of posts by their ID is quite fast.
Final Notes
Transient API is probably one of the best things of WordPress. Thanks to the plugins you can find for any kind of cache systems, it becomes a stupid simple API to a great number of software that can work under the hood.
Outside WordPress, such abstraction that works out of the box with a bunch of different caching system, and allow you to switch from one system to another with no effort is very hard to find.
You rarely can hear me saying that WordPress is better than other modern things, but the transient API is one of the few things I miss when I don't work with WordPress.
Surely cache is hard, does not solve code issues and is not a silver bullet, but it something you need to build an high-traffic site that works.
The WordPress idea to use a under-optimized MySQL table to do cache is quite insane, but is not better to keep yourself away from cache just because WordPress, by default, do it.
You just need to understand how things works, then make your choice.