Symfony2 跳过 AppCache 中的 default_ttl 设置

标签 symfony caching

当我在 Controller 中显式设置 TTL 时

$response->setMaxAge(60);
$response->setSharedMaxAge(60);

一切正常,我得到了正确的响应 header :

X-Symfony-Cache GET /hello/show/: miss, store
... and then ...
X-Symfony-Cache GET /hello/show/: stale, invalid, store
... and then ...
X-Symfony-Cache GET /hello/show/: fresh

但是当我没有在 Controller 中显式设置 TTL 并希望使用默认 TTL 设置时:

class AppCache extends HttpCache {

    protected function getOptions() {
        return array(
            'debug' => true,
            'default_ttl' => 60,
            'private_headers' => array('Authorization', 'Cookie'),
            'allow_reload' => false,
            'allow_revalidate' => false,
            'stale_while_revalidate' => 2,
            'stale_if_error' => 60,
        );
    }
}

我总是得到“小姐”。

X-Symfony-Cache GET /hello/show/: miss

看起来 Symfony 没有考虑 default_ttl 设置。为什么?

最佳答案

我找到原因了。这是由于私有(private)/公共(public) react 。文档指出:

Public vs private Responses¶

Both gateway and proxy caches are considered "shared" caches as the cached content is shared by more than one user. If a user-specific response were ever mistakenly stored by a shared cache, it might be returned later to any number of different users. Imagine if your account information were cached and then returned to every subsequent user who asked for their account page!

To handle this situation, every response may be set to be public or private:

public: Indicates that the response may be cached by both private and shared caches;
private: Indicates that all or part of the response message is intended for a single user and must not be cached by a shared cache.

Symfony conservatively defaults each response to be private. To take advantage of shared caches (like the Symfony2 reverse proxy), the response will need to be explicitly set as public

因此,要使 Symfony 使用 default_ttl 设置,您必须显式地将您的响应设置为公开,如下所示:

$response->setPublic();

关于Symfony2 跳过 AppCache 中的 default_ttl 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25093617/

相关文章:

php - Symfony2 YAML 解析失败

php - 不能在同一个 Twig 文件中使用循环 2 次

caching - PCIE中的 'Cache line size'相当于什么?

php - Symfony2控制台必须选择mac

php - 交响乐 2 : How to get route defaults by route name?

checkbox - 如何使用 symfony2 表单生成器呈现默认选中的复选框?

caching - 如何设置 nginx 缓存头永不过期?

javascript - 'new Image()' 是否允许使用缓存 (JavaScript)

caching - 服务器启动时自动启动cachecluster

java - 运行形式 javaws - jakarta.ws.rs.client 在第一个之后不发送请求?客户端缓存?