Data set via set_transient() doesn't persist beyond scope of function call
-
I’m not sure if I’m completely missing the scope of
set_transient()
andset_site_transient()
, but neither appear to persist the data I’m trying to cache beyond the scope of the function in which it’s set. I wish to have some data quickly available across numerous user requests that depend on the same data — which sounds like what the Transient API was designed for.I’ve setup some shortcodes in a custom plugin that call functions, and within those functions I’m attempting to push a SimpleXML object into the transient cache, ideally loading the transient value if it hasn’t expired. Starting from square one, I even tried using the sample code directly from the Codex:
if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $special_query_results = 'test'; //changed from object to simple string to test set_transient( 'special_query_results', $special_query_results, 60 ); //60 sec expiry } // Use the data like you would have normally...
However, the only time the above clause returns
true
is if I immediatelyset_transient()
before the code block. Otherwise, page refreshes just seconds apart always evaluate the transient value as missing and process the set statement within theif
clause. It’s as if the data isn’t really being set, even though theset_transient()
call returnstrue
.After performing
set_transient()
orset_site_transient()
I’ve checked the following DB tables and can’t find any option_names or meta_keys that match the name I’m giving to the stored transient data: wp_options, wp_X_options, wp_sitemeta.My dev installation is a WP Multisite setup, with the code above running in a custom plugin activated within several sites (it’s okay, and preferable for them to share data). WP Total Cache has been previously activated (but not turned “on”) for some of the sites, but has recently been deactivated completely, as I’m aware that WP’s caching behavior can change significantly with such plugins.
Thank you for any suggestions or pointers!
Brian
- The topic ‘Data set via set_transient() doesn't persist beyond scope of function call’ is closed to new replies.