0

I have a plugin that needs to check for update. Function below - get_version_to_update is fired when someone enters plugin admin page. In theory after request is done, data should be cached via transients for 24h. And this works for most sites, but for some specific sites I get data that wp_remote_request fires over and over, which (as I think) means that transient isn't saving.

Why this might happen? Is there more reliable way to check this?

public function get_version_to_update(){

    // I get the transient
    $data = get_transient("my_plugin_remote_version");

    // I check if it exists
    if($data === false){
        // here I get data from server using wp_remote_request, server return just version number, e.g. "2.0"   
        $data = wp_remote_request(...);

        // I cache it for 24h
        set_transient("my_plugin_remote_version", $data, 60 * 60 * 24);
    }

     // I check if returned data from server is correct
     if ( !$data || is_wp_error( $data ) || 200 != $data['response']['code']) {
        return false;
     } else {
        if( version_compare(MY_PLUGIN_VERSION, $new_version, '<') ) {
                    // return new version, if it's newer then current 
            return $data['body'];
        } else {
            return false;
        }
     }
}

This issue is killing me, thank you for any help!

4
  • Database caching is disabled?
    – fuxia
    Commented Dec 17, 2012 at 9:10
  • I have no idea, as I said it's for public plugin, I just see data that from some sites I get too much requests. Should I check if caching is enabled or not?
    – Marvin3
    Commented Dec 17, 2012 at 9:13
  • If the false return value is cached you might get that even when there are data in your transient. I run into this issue once, but it happened in a controlled environment, and I turned it off.
    – fuxia
    Commented Dec 17, 2012 at 9:17
  • Thank you. Do you have any recommendation about what can I do about that, maybe should I use cron job instead? Or save last update time via update_option?
    – Marvin3
    Commented Dec 17, 2012 at 9:24

1 Answer 1

3

By the definition and the usage pattern, transients might be deleted at any point in time, the expiry time parameter really specifies only the maximal time the value will be cached. It might be that the particular site has a problem with transients not being cleaned on time and employs some code to clean them automatically every hour. I think using options and cron for caching is more reliable then transients, but it is probably doesn't worth your effort if it is only the odd site that misbehaves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.