soalnick/scn-http-cache

为ZF2应用添加多个功能以实现可缓存性。

1.1.3 2013-04-30 16:29 UTC

This package is auto-updated.

Last update: 2024-09-08 07:23:57 UTC


README

一个ZF2模块,帮助实现HTTP规范和网关缓存中内置的标准缓存。

Build Status

需求

功能

  • 网关缓存PHP实现(即将推出)
  • HTTP缓存头操作插件(即将推出)
  • 处理过期和验证的插件(即将推出)
  • ESI视图助手
    • 在代理能力模式下性能极强 - 只渲染ESI标签
    • 通过运行单独的应用生命周期在不使用代理能力的情况下工作

安装

建议使用Composer将此模块添加到您的Zend Framework 2应用中。在克隆ZendSkeletonApplication后,将"socalnick/scn-http-cache"添加到需求列表中,然后运行php composer.phar install/update。您的composer.json应类似于以下内容

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "socalnick/scn-http-cache": "1.*"
    }
}

接下来,将所需的模块添加到config/application.config.php中

<?php
return array(
    'modules' => array(
        'Application',
        'ScnHttpCache',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

Varnish

安装

Varnish可以安装在任何现代Linux发行版上:https://www.varnish-cache.org/docs/3.0/installation/它也适用于MacOSX的开发,通过运行brew install varnish

配置

这是开发环境中最基础的Varnish配置。它设置了后端主机/端口,设置了一个表示代理能力的请求头,并查找响应的代理控制头以启动ESI处理。在将Varnish用于生产环境之前,强烈建议您在https://www.varnish-cache.org/docs上了解更多信息

backend default {
    .host = "127.0.0.1";
    .port = "10088";
}

sub vcl_recv {
    # Set a header announcing Surrogate Capability to the origin
    # ScnEsiWidget sees this header and emits ESI tag for widgets
    set req.http.Surrogate-Capability = "varnish=ESI/1.0";
}

sub vcl_fetch {
    # Unset the Surrogate Control header and do ESI
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}

使用方法

在视图脚本中输出ESI

<div><?php echo $this->esi($this->url('route/to/recent/tweets')) ?></div>

创建ESI动作

public function recentTweetsAction()
{
    $headers = $this->getResponse()->getHeaders();
    $cacheControl = new \Zend\Http\Header\CacheControl();
    $cacheControl->addDirective('s-maxage', '10');
    $headers->addHeader($cacheControl);

    $viewModel = new ViewModel();
    $viewModel->setTerminal(true);

    return $viewModel;
}

为ESI小部件动作创建视图脚本

<ul>
    <li><?php echo date('h:i:s')?> @SocalNick: This is a recent tweet!</li>
    <li><?php echo date('h:i:s', time() - 10)?> @SocalNick: This is a slightly less recent tweet!</li>
</ul>