noflash/supercache-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

noflash/supercache-bundle

Composer 安装命令:

composer require noflash/supercache-bundle

包简介

Supercache bundle for Symfony

README 文档

README

This project was born in a need for faster pages rendering years ago. Now, we have much better techniques (CDNs, HTTP/2, SPAs) to deal with the problem. The code should work with Symfony 2 & 3, but newer versions of Symfony would need an overhaul.

This project is left here to stay for legacy apps still using it, as well as a historical piece.

Supercache Bundle

Static pages caching for Symfony Framework.

What is Supercache?

Some of you may be annoyed looking at simple pages loading times - exactly like me. There's a lot solution for that problem - this is extreme one. If you ever used Wordpress you probably seen WP Super Cache plugin. This bundle is build around similar principles (but it is not affiliated in any way) as that plugin.

Basically SupercacheBundle caches whole response to static files and serve them at no cost. It's very useful for pages where there're a lot of static routes visited often.

Installation

Before you start using this bundle you should carefully read HTTP Cache chapter of Symfony Cookbook - it provides a bunch of excellent techniques and tips about caching.

Bundle can be installed like any other Symfony bundle.

  1. Open a command console, enter your project directory and execute following command:
    composer require noflash/supercache-bundle
  2. Enable the bundle by adding following line to app/AppKernel.php file:
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new noFlash\SupercacheBundle\SupercacheBundle(),
        );

        // ...
    }

    // ...
}
  1. Execute following command and follow onscreen instructions app/console supercache:install Currently manual installation is required. You should create webcache directory in root folder and than perform first two steps described in Troubleshooting section below.

Configuration

TBD Default configuration:

supercache:

    # Enable/disable cache while running prod environment
    enable_prod:          true

    # Enable/disable cache while running dev environment
    enable_dev:           false

    # Cache directory, must be http-accessible (so it cannot be located under app/)
    cache_dir:            '%kernel.root_dir%/../webcache'

    # Enable/disable adding X-Supercache header
    cache_status_header:  true

Troubleshooting

Installation command failed: Failed to create cache directory

Looks like specified cache directory cannot be created. It can be permission related problem. You can try creating it yourself. To do that simply create desired cache directory (by default it's set to ./webcache/). Next put following content in .htaccess inside cache directory:

RemoveHandler .php
RemoveType .php
Options -ExecCGI

<IfModule mod_php5.c>
    php_flag engine off
</IfModule>

<IfModule mod_headers.c>
    Header set Cache-Control 'max-age=3600, must-revalidate'
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html A3600
</IfModule>

<IfModule mod_mime.c>
    AddType application/javascript .js
    AddType text/html .html
    AddType application/octet-stream .bin
</IfModule>

Installation command failed: Failed to modify .htaccess (...)

If you (or some other bundle) modified web/.htaccess file, installer may be have trouble automatically applying required changes. You can add following lines manually - they should be placed just below RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] (or similar):

### >>>SUPERCACHE BUNDLE 
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD) [OR]
RewriteCond %{QUERY_STRING} !^$
RewriteRule . - [S=3]

RewriteCond %{DOCUMENT_ROOT}/../webcache/$1/index.html -f
RewriteRule ^(.*) %{DOCUMENT_ROOT}/../webcache/$1/index.html [L]

RewriteCond %{DOCUMENT_ROOT}/../webcache/$1/index.js -f
RewriteRule ^(.*) %{DOCUMENT_ROOT}/../webcache/$1/index.js [L]

RewriteCond %{DOCUMENT_ROOT}/../webcache/$1/index.bin -f
RewriteRule ^(.*) %{DOCUMENT_ROOT}/../webcache/$1/index.bin [L]
### <<<SUPERCACHE BUNDLE

Please note cache path need to be adjusted. Path is relative to web/ directory.

My server doesn't support .htaccess

You have two options: translate rewrite rules (specified above) to format supported by server you use or use PHP-PULL mode. SupercacheBundle contains fallback mode for servers without .htaccess support (e.g. CLI server issued using app/console server:run command). This mode works by attaching to Symfony HTTP kernel and trying locate cached response for given request. Of course it comes with a price - fallback will run PHP intepretter and initialize core of the framework. In my tests DIRECT mode returns response in 3-5ms while PHP-PULL mode need around 30ms.

There's no need to change any configuration options to enable PHP-PULL.

FAQ

Do all responses gets cached?

No, request & response must meet some criteria to be cached:

  • GET request method
  • No query string
  • Response code between 200 and 300 (excluding 204)
  • No private or no-store cache control directives
  • Route is not marked as excluded (see #3 for details)

Are there any limitations?

Yes, there are few:

  • It's impossible to serve different content on /sandbox and /sandbox/
  • You cannot have routes .. and . (they are illegal in HTTP RFC anyway)
  • There are no automatic check for authentication token (it's your responsibility to set private cache policy if you're presenting user-specific information)
  • Due to performance reasons files are served from cache with one of the following MIME-Types: text/html, application/javascript or application/octet-stream. See issue #2 for details.

noflash/supercache-bundle 适用场景与选型建议

noflash/supercache-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 661 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cache」 「static cache」 「supercache」 「pages caching」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 noflash/supercache-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 noflash/supercache-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 noflash/supercache-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 661
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-24