aalfiann/buffer-cache

使用缓冲支持缓存响应页面。

1.6.0 2020-06-12 10:47 UTC

This package is auto-updated.

Last update: 2024-09-12 20:05:42 UTC


README

Latest Stable Version Total Downloads License

使用缓冲支持缓存响应页面。
有时我们只想简单地缓存输出响应页面。

依赖

  • Symfony Cache >> symfony/cache
  • Doctrine Cache >> doctrine/cache

安装

通过 Composer 安装此包。

composer require aalfiann/buffer-cache
  • 使用 SQLite
    确保您的服务器已安装 php7-sqlite3

  • 使用 Redis

    composer require predis/predis
    

使用方法

use aalfiann\BufferCache\FilesystemBufferCache;

require 'vendor/autoload.php';

// Callback to modify html source before cache
function modify($buffer) {
    // Test inject javascript
    $javascript = '<script>console.log("Cache was generated at '.date('Y-m-d H:i:s').'")</script>';
    $buffer = explode('</body>',$buffer);
    $buffer = implode($javascript.'</body>',$buffer);
    return $buffer;
}

$cache = new FilesystemBufferCache([
    // Set ttl cache
    'ttl' => 120
]);

// Start cache
$cache->start();

// Start buffer
//$cache->startBuffer();        // without callback
$cache->startBuffer('modify');  // with callback

// Example to render page
echo '<html>
    <head>
        <title>Test Page</title>
    </head>
    <body>Just test to cache the response page</body>
</html>';

// for condition if page failed to render
//$cache->cancelBuffer();

// End cache
//$cache->end();        // without callback
$cache->end('modify');  // with callback

可用的构造函数

  1. 使用文件系统缓存

    use aalfiann\BufferCache\FilesystemBufferCache;
    $cache = new FilesystemBufferCache([
        // options here
    ]);
  2. 使用 SQLite3 缓存

    use aalfiann\BufferCache\SQLiteBufferCache;
    $cache = new SQLiteBufferCache([
        // options here
    ]);
  3. 使用 Redis 缓存

    use aalfiann\BufferCache\PredisBufferCache;
    $cache = new PredisBufferCache([
        // options here
    ]);

构造函数类中的选项

以下是构造函数类中的默认选项

[
    'namespace' => 'page',              // Namespace for cache
    'ttl' => 18000,                     // Time to live cache
    'http_cache' => false,              // Use http cache
    'http_maxage' => 3600,              // Maxage of http cache
    'cache_empty_content' => false,     // Cache empty content
    'cache_query_param' => false,       // Allow cache for url with query parameter
    'ext' => [                          // Allow cache for url with extension 
        '.htm','.html','.xhtml','.asp','.aspx','.css',
        '.php','.js','.jsp','.cfm','.md','.xml','.rss'
    ],
    'filesystem' => [                   // filesystem parameters or options
        'path' => 'cache/page'
    ],
    'sqlite3' => [                      // sqlite3 parameters or options
        'table' => 'cache',
        'path' => 'cache/page/page_cache.sqlite3'
    ],
    'predis' => [                       // predis parameters or options.
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379
    ]
]

有关 predis 选项的更多信息,请参阅 https://packagist.org.cn/packages/predis/predis

带有扩展名的 URL 页面

这将不会缓存带有二进制扩展名(如 .exe、.rar、.zip、.mp4、.mp3 等)的 URL 页面。
如果您想缓存,您必须将扩展名列入白名单。

已允许的默认扩展名

var $ext = [
    '.htm','.html','.xhtml','.asp','.aspx','.css',
    '.php','.js','.jsp','.cfm','.md','.xml','.rss'
];

示例:如果您想添加更多 .py.txt

$cache->addExtension('.py');
$cache->addExtension('.txt');

示例:如果您只想允许 .js.css

  1. 通过构造函数类的选项

    $cache = new SQLiteBufferCache([
        'ext' => ['.js', '.css']
    ]);
  2. 或通过属性

    $cache->ext = ['.js', '.css'];

HTTP 缓存

此库支持 http 缓存,但默认情况下未激活。
如果您想使用此功能,有以下三种方法

  1. 通过构造函数类的选项

    $cache = new SQLiteBufferCache([
        'http_cache' => true,
        'http_maxage' => 3600
    ]);
  2. 或通过函数

    $cache->useHttpCache(3600);
  3. 或通过属性

    $cache->http_cache = true;
    $cache->http_maxage = 3600;

注意

  • 我仅使用 文件系统SQLite3Redis 创建缓冲缓存,因此欢迎贡献。