duanlvxi8652 2011-06-17 07:40
浏览 79
已采纳

PHP Wordpress缓存XML变量

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.

  • 写回答

1条回答 默认 最新

  • douxiong2001 2011-06-17 08:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭