I read about transient in the codex and some questions and answers here.
So i’m developing a web app based on wp. It works now, but i want to optimize it. Users manage content regulary, so crud operations is common. Due to this i don’t want to use page caching with expirations.
I want to implement some caching functions on “get” operations.
This is in my mind:
- a user add some content, populate a list
- i create a transient “record” for that list object, and for the item itself
- when the list changes (update, delete, create) i delete the previous cache record and add the new one
Mainly i use WP_Query (posts and meta).
Questions:
Do i need this caching method, or using transient cache with wp_query won’t take a large effet?
My other concern to put all results into another table feel a bit irrelevant..?!
For my WordPress testimonials widget plugin I’ve successfully used transients with expiration dates to drop page loads by 0.1 to 0.5 seconds.
Using WordPress’s own transients is a life-saver. It’s so much easier to package or use straight as needed and then concentrate again on application logic. Rolling yet another caching system is useless. Further, as you get into using caching plugin like W3 Total Cache and WP Super Cache, there’s additional caching tie-ins that’ll speed things further along.
At this time, expired transients do fill up your
wp_options
table. Future versions of WordPress will better handle transient cleanup. For now, check https://gist.github.com/ScottPhillips/2907732 for scheduled transient purging or roll it into your application.Since I do free and premium plugins, I’ve abstracted the caching operations, but there’s no reason why you couldn’t move my abstractions into your own work.
While in your case, you’re talking about users creating data, you could think of that data creation being the widget or shortcode options being passed into
testimonialswidget_list
as$atts
in below.The
$content = apply_filters( 'testimonials_widget_cache_get', false, $atts );
is checking that there’s a current cache entry for the given$atts
. As such, if the incoming$atts
have changed, then the caching will fail andfalse
is returned so that a new set of data is created.Line
$content = apply_filters( 'testimonials_widget_cache_set', $content, $atts );
is where our newly generated data is saved back to the cache for quick, easy reuse.The data grabbing from testimonials-widget.php.
The caching operations
Note The code above are scraps. Copying and pasting into your own application will not work right off. I would suggest looking at where
self::
andTestimonials_Widget_Settings::
is used as probable points to alter code to suit your needs.