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

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择