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(''.$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(''.$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 ?


图片转代码服务由CSDN问答提供 功能建议

我使用gae php作为使用“file_get_contents”的简单代理时遇到了一些麻烦 \ n

当我第一次加载文件时,我获得了最新版本。 但是,如果我更改文件的内容,我不会立即获得最新版本。

   $ result = file_get_contents(''.$url); 

我找到的临时解决方案是随机添加 查询字符串末尾的变量,这使我每次都能得到一个新版本的文件:

  $ result = file_get_contents('  /'.$url.'?r='。rand(0,9999)); 

但是这个技巧不适用于带参数的api调用 例如。

我尝试在gae的php.ini中禁用APC缓存(使用apc.enabled =“0”)并使用 clearstatcache(); in 我的剧本,但都不起作用。



  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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 file_get_contents http header example and HTTP header documentation a modified example would be:

    $opts = [
      'http' => [
        'method' => 'GET',
        'header' => "Cache-Control: max-age=60
    $context = stream_context_create($opts);
    $file = file_get_contents('', false, $context);
    解决 无用
    打赏 举报

相关推荐 更多相似问题