没有缓存的Google云端存储公共访问

时间:2015-01-29 10:05:39

标签: php google-app-engine caching google-cloud-storage

我在 Google-Appengine 上创建了一个网站。为了降低 Google-Cloud-SQL 的成本,我的API会在 Google-Cloud-Storage 中从Cloud-SQL生成公共 json文件应该通过我的Android-App和我的网站(通过ajax)阅读。

问题是,Google-Cloud-Storage的文件会自动缓存一小时,但我想禁用特定文件的缓存。

如果我在开发人员控制台中上传文件手册,我可以点击"编辑元数据"更改此行为。并将Cache-Control设置为private, max-age=0,no-transform(参见下图)。然后,如果我尝试访问此文件,则会获得所需的标头private, max-age=0,no-transform [Edit Metadata manual

但在我的应用程序中,json文件是由 PHP 生成的,我总是得到标题cache-control → public, max-age=3600。我使用以下代码创建文件(这是官方文档:Docs):

$options = ['gs' => [
    'Content-Type' => 'text/json',
    'acl' => 'public-read',
    'enable_cache' => false,
    'read_cache_expiry_seconds' => 0]
];
$ctx = stream_context_create($options);
file_put_contents($url,$content,0,$ctx);

有人知道为什么没有关闭缓存吗?

1 个答案:

答案 0 :(得分:3)

related question中提到,除了Cache-Controlenable_cache之外,您可能需要明确/单独设置read_cache_expiry_seconds;特别是,您设置的选项控制App Engine一侧的缓存为memcache,而Cache-Control设置为specific to Google Cloud Storage本身。所以你想要:

$options = ['gs' => [
    'Content-Type' => 'text/json',
    'acl' => 'public-read',
    'enable_cache' => false,
    'read_cache_expiry_seconds' => 0,
    'Cache-Control' => 'private, max-age=0,no-transform']
];