douping1825 2014-01-13 10:28
浏览 17
已采纳

文件缓存在gae PHP中

I'm having some trouble using gae php as a simple proxy using "file_get_contents"

When i load a file for the first time I get the latest version available. But if I change the content of the file, I dont get the latest version immediately.

$result = file_get_contents('http://example.com/'.$url);

The temporary solution I found was to add a random variable at the end of the query string, which allowed me to get a fresh version of the file every time :

$result = file_get_contents('http://example.com/'.$url.'?r=' . rand(0, 9999));

But this trick doesn't work for api calls with parameters for example.

I tried disabling APC cache in the php.ini of gae (using apc.enabled = "0") and i used clearstatcache(); in my script, but neither work.

Any ideas ?

Thanks.

  • 写回答

1条回答 默认 最新

  • doukeyong3746487 2014-01-13 21:26
    关注

    As described in the appengine documentation the http stream wrapper uses urlfetch. As seen in another question urlfetch provides a public/shared cache and as such does not allow individual apps to clear it. For your own services you can set the HTTP cache headers to reduce or void the cache as necessary.

    Additionally, you can also add HTTP request headers indicating the maximum age of data that is allowed to be returned. The python example given in mailing list thread is:

    result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-age=300'})
    

    Per php.net file_get_contents http header example and HTTP header documentation a modified example would be:

    <?php
    $opts = [
      'http' => [
        'method' => 'GET',
        'header' => "Cache-Control: max-age=60
    ",
      ],
    ];
    $context = stream_context_create($opts);
    $file = file_get_contents('http://www.example.com/', false, $context);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退