PHP WordPress Cache XML Variable

I am currently loading an XML HTTP Request into an array and then caching it although sometimes the site I pull the data from isn’t the most reliable and I get back blank page or not everything filled out. One way of fixing this I thought would to be make sure that the array has the same or more amount of data in the array and then caching it for an hour. Now here is my issue when ever it array doesn’t have the data I don’t have the previous cache giving me an error on the cached variable. Do I know if I understand caching right? Can I cache a variable and only overwrite if given a certain condition? Anyhelp would be great. My thought would be to store the data in mysql and then query it if the variable doesn’t go through.
if ( $checksize>=8 ){
weather_result = wp_cache_get( 'my_result' );
if ( false == $weather_result ) {
$weather_result= $insert_array;
wp_cache_add( 'weather_cache', $weather_result,$flag = '',$expire = 0);
}
}

Now the checksize is just an array which counts the number of elements in the array. I would only like to update this when it has all the information.

Related posts

Leave a Reply

1 comment

  1. WordPress has an API for caching in database, the transient api. Its eas to use and you can ask for vars, is cached.

    $tag_cloud = get_transient( 'tag_cloud' );
    if ( false === $tag_cloud || '' === $tag_cloud ){
        $args = array('echo' => false);
        $tag_cloud = wp_tag_cloud( $args );
        set_transient( 'tag_cloud', $tag_cloud, 60*60*12 );
    }
    echo $tag_cloud;
    

    see also this post and the codex