SlideShare a Scribd company logo
Lightweight use of
transients
Phil Marx
@JustPhilMarx
www.nerdtalk.de / phil.marx@gmail.com
Works with WordPress since v2.x
2
WHAT ARE
TRANSIENTS?
A short roundup
3
1
“WordPress Transients API […]
offers a simple and standardized
way of storing cached data in the
database temporarily […]
44
https://codex.wordpress.org/Transients_API
Examples for Transients
▰Results of complex queries
▰Results of remote calls
▰Complex aggregations of different values
5
Advantage: Simple usage
▰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 );
6
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
“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
WORKAROUND DELETION
9
2
Downsides of deletion
▰Leads to database fragmentation
▻Can cause slow database performance
▰A new dataset is created for the same content
▻Just… why?!
10
What codex recommends
if(false===$value=get_transient('value’))) {
// this code runs when there is no valid
transient set
}
https://codex.wordpress.org/Transients_API
11
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
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
14
Expired transients are only deleted, if they‘re requested again
CLEAN UP YOUR OWN
TRANSIENTS!
Just. Do. It!
15
3
Causes for not requesting transients anymore
▰Adding unique identifier to transient key
▻(Session ID, time(), some hashes, …)
▰Forget deletion when deactivating
plugins/themes
16
Expired and orphaned transients need to be deleted
manually
▰Plugins
▻ „Delete expired transients“
▰SQL Queries
▻Only useful in combination with cronjobs /
WP-Cron
17
“▰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/
“▰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/
DON’T LET THE USER DO YOUR WORK!
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
SUMMARY
Be lightweight!
22
Be lightweight!
▰Add your own timeout
▰Think if you need unique identifier which are for
temporary usage
▰Clean up your transients
23
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)

More Related Content

DOCX
Wait Interface
PDF
Redis & ZeroMQ: How to scale your application
PDF
Unix cheatsheet
PDF
A Shortcut to Awesome: Cassandra Data Modeling By Example (Jon Haddad, The La...
PDF
Replication MongoDB Days 2013
PDF
Lucene revolution 2011
PDF
Node.js API 서버 성능 개선기
PDF
Redis as a message queue
Wait Interface
Redis & ZeroMQ: How to scale your application
Unix cheatsheet
A Shortcut to Awesome: Cassandra Data Modeling By Example (Jon Haddad, The La...
Replication MongoDB Days 2013
Lucene revolution 2011
Node.js API 서버 성능 개선기
Redis as a message queue

What's hot (9)

PDF
Learning Dtrace
ZIP
AnyMQ, Hippie, and the real-time web
PDF
Commencer avec le TDD
PDF
Vagrant + Rouster at salesforce.com - PuppetConf 2013
PDF
Guarding Your Code Against Bugs with Continuous Testing
PPTX
Common Performance Pitfalls in Odoo apps
PDF
Continuous testing In PHP
PDF
Func dyn column_set.c
PDF
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Learning Dtrace
AnyMQ, Hippie, and the real-time web
Commencer avec le TDD
Vagrant + Rouster at salesforce.com - PuppetConf 2013
Guarding Your Code Against Bugs with Continuous Testing
Common Performance Pitfalls in Odoo apps
Continuous testing In PHP
Func dyn column_set.c
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Ad

Similar to Lightweight use of transients (20)

PPTX
Transients are good for you - WordCamp London 2016
PDF
Introduction to the WordPress Transients API
PDF
Speed Things Up with Transients
PDF
Options, and Transients, and Theme Mods — Oh my!
PDF
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
PDF
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
PDF
Intro to advanced caching in WordPress
PDF
WordPress Transients
PDF
Caching in WordPress
PDF
WordPress Speed & Performance from Pagely's CTO
PPTX
Consuming & embedding external content in WordPress
PDF
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
PPTX
Caching, Scaling, and What I've Learned from WordPress.com VIP
PDF
Balancing Act of Caching LoopConf 2018
PDF
Less and faster – Cache tips for WordPress developers
PDF
Balancing Act of Caching, WordCamp Europe 2018
PDF
WPCampus 2019 - Website Renewal Services
PPTX
Best Practices for Building WordPress Applications
PDF
WordPress as an application framework
PDF
Caching and Scaling WordPress using Fragment Caching
Transients are good for you - WordCamp London 2016
Introduction to the WordPress Transients API
Speed Things Up with Transients
Options, and Transients, and Theme Mods — Oh my!
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Intro to advanced caching in WordPress
WordPress Transients
Caching in WordPress
WordPress Speed & Performance from Pagely's CTO
Consuming & embedding external content in WordPress
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
Caching, Scaling, and What I've Learned from WordPress.com VIP
Balancing Act of Caching LoopConf 2018
Less and faster – Cache tips for WordPress developers
Balancing Act of Caching, WordCamp Europe 2018
WPCampus 2019 - Website Renewal Services
Best Practices for Building WordPress Applications
WordPress as an application framework
Caching and Scaling WordPress using Fragment Caching
Ad

More from Phil Marx (7)

PPTX
DSGVO und WordPress - 2024er-Edition / WordPress Meetup Berlin 25.09.2024
PPTX
New Ranking Metrics by Google
PPTX
Die neuen Bewertungskriterien von Google ab 05 / 2021
PPTX
Podcasting unter WordPress
PPTX
DSGVO und WordPress
PPTX
Aus Englisch mach Deutsch
PPTX
Evernote - Dein virtuelles Gedächtnis - a.k.a. das unterschätzte Tool
DSGVO und WordPress - 2024er-Edition / WordPress Meetup Berlin 25.09.2024
New Ranking Metrics by Google
Die neuen Bewertungskriterien von Google ab 05 / 2021
Podcasting unter WordPress
DSGVO und WordPress
Aus Englisch mach Deutsch
Evernote - Dein virtuelles Gedächtnis - a.k.a. das unterschätzte Tool

Recently uploaded (20)

PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PPTX
Funds Management Learning Material for Beg
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PDF
Introduction to the IoT system, how the IoT system works
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Internet___Basics___Styled_ presentation
PptxGenJS_Demo_Chart_20250317130215833.pptx
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Funds Management Learning Material for Beg
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Introduction to the IoT system, how the IoT system works
An introduction to the IFRS (ISSB) Stndards.pdf
Tenda Login Guide: Access Your Router in 5 Easy Steps
Job_Card_System_Styled_lorem_ipsum_.pptx
Introuction about WHO-FIC in ICD-10.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Cloud-Scale Log Monitoring _ Datadog.pdf
Unit-1 introduction to cyber security discuss about how to secure a system
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
INTERNET------BASICS-------UPDATED PPT PRESENTATION
international classification of diseases ICD-10 review PPT.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Internet___Basics___Styled_ presentation

Lightweight use of transients

  • 2. Phil Marx @JustPhilMarx www.nerdtalk.de / phil.marx@gmail.com Works with WordPress since v2.x 2
  • 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
  • 5. Examples for Transients ▰Results of complex queries ▰Results of remote calls ▰Complex aggregations of different values 5
  • 6. Advantage: Simple usage ▰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 ); 6
  • 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
  • 11. What codex recommends if(false===$value=get_transient('value’))) { // this code runs when there is no valid transient set } https://codex.wordpress.org/Transients_API 11
  • 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
  • 14. 14 Expired transients are only deleted, if they‘re requested again
  • 15. CLEAN UP YOUR OWN TRANSIENTS! Just. Do. It! 15 3
  • 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/
  • 20. DON’T LET THE USER DO YOUR WORK!
  • 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)

Editor's Notes

  • #8: No expiration needed  but then it‘s autoloaded (don‘t want!)
  • #11: - Each time a new ID is generated – BIGINT is nice, but not expanding them to the limits is nicer
  • #12: - A transient can expire before expiration date