Support » Plugins » Data set via set_transient() doesn't persist beyond scope of function call

  • Resolved ironprogrammer

    (@ironprogrammer)


    I’m not sure if I’m completely missing the scope of set_transient() and set_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 immediately set_transient() before the code block. Otherwise, page refreshes just seconds apart always evaluate the transient value as missing and process the set statement within the if clause. It’s as if the data isn’t really being set, even though the set_transient() call returns true.

    After performing set_transient() or set_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

Viewing 1 replies (of 1 total)
  • It turns out that there was some cleanup an older version of W3 Total Cache didn’t complete when deactivated. After updating the plugin and reactivating it, the transient cache is functioning as it should, both at the site and network level.

    Brian

Viewing 1 replies (of 1 total)
  • The topic ‘Data set via set_transient() doesn't persist beyond scope of function call’ is closed to new replies.