aalfiann / buffer-cache
Cache the response page with buffer support.
Requires
- php: >=7.1
- doctrine/cache: ^1.8
- symfony/cache: ^4.2
Suggests
- ext-sqlite3: SQLite3 extension is required to use the SQLite3 database as a cache storage
- predis/predis: Allows you to use cache with Redis
README
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 installedphp7-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
-
Cache with Filesystem
use aalfiann\BufferCache\FilesystemBufferCache; $cache = new FilesystemBufferCache([ // options here ]);
-
Cache with SQLite3
use aalfiann\BufferCache\SQLiteBufferCache; $cache = new SQLiteBufferCache([ // options here ]);
-
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.
-
By options in constructor class
$cache = new SQLiteBufferCache([ 'ext' => ['.js', '.css'] ]);
-
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 :
-
By options in constructor class
$cache = new SQLiteBufferCache([ 'http_cache' => true, 'http_maxage' => 3600 ]);
-
Or by function
$cache->useHttpCache(3600);
-
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.