volkerschulz/file-cache

PHP file cache handler

1.2.0 2023-11-01 23:30 UTC

This package is auto-updated.

Last update: 2024-09-30 01:57:36 UTC


README

PHP file cache handler

Installation

The recommended way to install FileCache is through Composer.

composer require volkerschulz/file-cache

Usage

Minimal:

use volkerschulz\FileCache;

// Create instance 
$filecache = new FileCache('testfiles/cache.png');

// Respond to request
$filecache->respond();


With custom header:

use volkerschulz\FileCache;

// Create instance for file 'testfiles/utf.json' 
// with default options
$filecache = new FileCache('testfiles/utf.json');

// Set additional custom headers (optional)
$filecache->addHeader('Content-Type', 'application/json; charset=UTF8');

// Respond to request
$filecache->respond();


With custom options:

use volkerschulz\FileCache;

$options = [
    'use_checksum'  => true,
    'hash_algo'     => 'xxh128',
    'fresh_for'     => 300
];

// Create instance for file 'testfiles/utf.json' 
// with custom options
$filecache = new FileCache('testfiles/utf.json', $options);

// Set additional custom headers (optional)
$filecache->addHeader('Content-Type', 'application/json; charset=UTF8');

// Respond to request
$filecache->respond();

Options

use_filetime bool
Default: true - Whether to use the file's last modification time when creating the ETag.

use_filesize bool
Default: true - Whether to use the file's size when creating the ETag.

use_checksum bool
Default: false - Whether to use the file's hash when creating the ETag. Depending on the filesize this might consume a lot of CPU and I/O time.

At least one of use_filetime, use_filesize, use_checksum needs to be true to create or compare an ETag.

hash_algo String
Default: 'crc32' - Which hash algorithm to use when use_checksum is true. Must be supported by the current PHP version.

use_etag bool
Default: true - Whether to use an ETag at all. It is strongly recommended to leave that option set to true.

fresh_for int
Default: 0 - Number of seconds (from now) the resource is guaranteed not to be stale and should not be revalidated.

add_missing_headers bool
Default: true - If set to true, missing headers (currently Content-Type only) will be added automatically, if possible.

Security

If you discover a security vulnerability within this package, please send an email to security@volkerschulz.de. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced.

License

This package is made available under the MIT License (MIT). Please see License File for more information.