WordPress Transients API is a powerful tool for caching data with an expiration date. But you always have to have a look on the database the transients are stored in.
Transients can mess up your and your clients database without any benefit. A poor implementation of transients can slow down the database and consequently the webpage instead.
I want to raise awareness for a lightweight way to use the Transients API. Additionally you’ll get an approach to reduce queries and fragmentation of the database.
4. “WordPress Transients API […]
offers a simple and standardized
way of storing cached data in the
database temporarily […]
44
https://codex.wordpress.org/Transients_API
7. Transients are designed to expire
▰set_transient() – sets a transient
▻ set_transient( $transient, $value, $expiration );
▰get_transient() – gets a transient
▻ get_transient( $transient );
▰delete_transient() – deletes a transient
▻ delete_transient( $transient );
7
8. “WordPress Transients API […]
offers a simple and standardized
way of storing cached data in the
database temporarily by giving it
a […] timeframe after which it will
expire and be deleted.
88
https://codex.wordpress.org/Transients_API
10. Downsides of deletion
▰Leads to database fragmentation
▻Can cause slow database performance
▰A new dataset is created for the same content
▻Just… why?!
10
12. My approach to a lightweight use of transients
▰Set a expiration time in a far away future
▰Set an own timeout and update!
12
If a transient exists, this function will update the
transient's expiration time.
https://codex.wordpress.org/Function_Reference/set_transient
13. Code example
// Setting my own timeout
$timeout_transient = 60 * 5;
$stream = get_transient('streaminglive');
// Fallback if transient was deleted before expiration
if (!is_object($stream)) $stream = new stdClass;
// I'll save the last check into a variable "last_check" (see below) -
if the transient is expired based on my timeout, renew it
if (!isset($stream->last_check) || $timeout_stream < (time() -
$stream->last_check)) {
// Do whatever you need to get/update your data
$stream->last_check = time();
/* Attention: - The timeout of the transient is much more than my
personal timeout - set_transient checks, if there is a valid transient
and will update it instead of creating a new one */
set_transient('streaminglive', $stream, 60 * 60);
}
13
16. Causes for not requesting transients anymore
▰Adding unique identifier to transient key
▻(Session ID, time(), some hashes, …)
▰Forget deletion when deactivating
plugins/themes
16
17. Expired and orphaned transients need to be deleted
manually
▰Plugins
▻ „Delete expired transients“
▰SQL Queries
▻Only useful in combination with cronjobs /
WP-Cron
17
18. “▰We deleted about 143,513
rows of transient entries bringing
the wp_options table from 8Gb
to 200mb
18
https://theeventscalendar.com/support/forums/topic/large-number-of-transients-in-wp_options-table/
19. “▰In wp_options was about 7000
rows (1,5 Gb). […]
At the moment 672 rows (2,6
Mb).
19
https://theeventscalendar.com/support/forums/topic/large-number-of-transients-in-wp_options-table/
21. Don‘t be that guy!
▰Tidy up after deactivation of your theme /
plugin
▻ delete_transient( $transient );
▰Use WP Cron event to delete orphaned
transients
▻delete-transients-by-prefix.php (GitHub)
21
23. Be lightweight!
▰Add your own timeout
▰Think if you need unique identifier which are for
temporary usage
▰Clean up your transients
23
24. 24
THANKS!Any questions?
You can find me at
@JustPhilMarx / nerdtalk.de
phil.marx@gmail.com
Thanks to
- SlidesCarnival for Slides template and Icons (CC BY 4.0)
- Alexandru Zdrobău for girl photo (CC0)
Hinweis der Redaktion
No expiration needed but then it‘s autoloaded (don‘t want!)
- Each time a new ID is generated – BIGINT is nice, but not expanding them to the limits is nicer