定制 aalfiann/buffer-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

aalfiann/buffer-cache

Composer 安装命令:

composer require aalfiann/buffer-cache

包简介

Cache the response page with buffer support.

README 文档

README

Latest Stable Version Total Downloads License

Cache the response page with buffer support.
Sometimes we just want to simply cache the output response page.

Dependencies

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

Installation

Install this package via Composer.

composer require aalfiann/buffer-cache
  • With SQLite
    Make sure your server already installed php7-sqlite3.

  • With Redis

    composer require predis/predis
    

Usage

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

Available Constructor

  1. Cache with Filesystem

    use aalfiann\BufferCache\FilesystemBufferCache;
    $cache = new FilesystemBufferCache([
        // options here
    ]);
  2. Cache with SQLite3

    use aalfiann\BufferCache\SQLiteBufferCache;
    $cache = new SQLiteBufferCache([
        // options here
    ]);
  3. Cache with Redis

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

Options in constructor class

Here is the default options in constructor class

[
    '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
    ]
]

For more detail information about predis options. See https://packagist.org/packages/predis/predis.

Url page with extension

This will not cache for url page with binary extension like .exe, .rar, .zip, .mp4, .mp3, etc.
You have to whitelist the extension if you want to cache.

The default extensions which is already allowed are:

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

Example if you want to add more .py and .txt.

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

Example if you want just allow .js and .css only.

  1. By options in constructor class

    $cache = new SQLiteBufferCache([
        'ext' => ['.js', '.css']
    ]);
  2. Or by properties

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

Http Cache

This library is support http cache but inactivated by default.
If you want to use this, there is three ways :

  1. By options in constructor class

    $cache = new SQLiteBufferCache([
        'http_cache' => true,
        'http_maxage' => 3600
    ]);
  2. Or by function

    $cache->useHttpCache(3600);
  3. Or by properties

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

Note

  • I only create buffer cache with using Filesystem, SQLite3 and Redis, so contribution are welcome.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固