如何为外部云文件设置缓存控制

时间:2010-09-27 14:36:16

标签: php caching controls performance

嘿伙计们, 我一心想提高我的页面速度因素,昨天我在rackspacecloud上获得了一些云空间。在此之前,我通过htaccess通过适当的缓存控制从无Cookie域提供静态内容。

现在我转向云后,我的htaccess无法控制云文件。机架空间上有一个TTL参数,用于设置文件在CDN上保留多长时间的值。该值反映在我的Page Speed设置(google + firebug)上。现在默认设置可以最多72小时,但我需要超过7天的东西。我需要一些api,它有点复杂..

有什么方法可以对我的云文件强制执行缓存控制? 这些查询字符串做了什么domain.com/file.css?cache=0.54454334 ???

他们能实现我想要的吗? 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可能已经弄清楚了,但是有一个指向结帐的链接:Set far-future expires headers with Rackspace Cloud Files (sort of)

他正在使用cloudfiles PHP API,我也是。您可以手动将TTL(也称为过期)标头设置为您想要的任何内容。现在我将它们设置为365天(可能有点过分)。

文档相当简单。如果您需要任何帮助,此代码应该可以帮助您入门:

<?php

// include the API
require('cloudfiles.php');

// cloud info
$username = "myusername"; // username
$key = "c2dfa30bf91f345cf01cb26d8d5ea821"; // api key

// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);

// Get the container we want to use
$container = $conn->create_container('images');

// store file information
$filename  = "images/logo.jpg";

// upload file to Rackspace
$object = $container->create_object($filename);
$object->load_from_filename($localfile);

// make public, and set headers
$container->make_public(86400 * 365); // expires headers set to 365 days

?>