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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化