文件缓存在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.

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);
?>