https://github.com/attitude/rfpl-cache-php
Respond first, process later – PHP Caching
https://github.com/attitude/rfpl-cache-php
Last synced: about 1 month ago
JSON representation
Respond first, process later – PHP Caching
- Host: GitHub
- URL: https://github.com/attitude/rfpl-cache-php
- Owner: attitude
- License: mit
- Created: 2016-05-22T21:24:05.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-03-20T07:47:36.000Z (about 4 years ago)
- Last Synced: 2025-04-24T03:47:04.781Z (about 1 month ago)
- Language: PHP
- Size: 10.7 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Respond First Process Later Cache
> PHP caching drop-in class
Features:
1. only GET requests are cached
1. always serves previously cached version if exists
1. request is passed down to the application only if TTL expires
1. if processing occurs, it is never sent to output if cached response was sent beforeThis method is experimental and might not be suitable for all applications due to how the caching mechanism works.
## Installation using Composer
1. Create `composer.json`:
```
{
"name": "yourproject",
"repositories": [
{
"type": "vcs",
"url": "[email protected]:attitude/rfpl-cache-php.git"
}
],
"require": {
"attitude/rfpl-cache-php": "dev-master"
}
}
```
2. run `$ composer install`## Usage
1. Require using composer autoload: `require_once 'vendor/autoload.php';` or directly `require_once 'src/Cache.php';`
2. Insert before any processing:
```php
$cache = new \RFPL\Cache();
try { $cache->serve(); } catch (\Exception $e) {}
```## Options
Constructor accepts array of arguments:
- `path`: relative or absolute path, where to store cache; default value is `'cache'`;
- `ttl`: time to live in seconds specifies how long cache is considered *fresh*; default is 5 minutes, `300` secondsExample:
```php
$cache = new \RFPL\Cache([
'path' => 'cache/html',
'ttl' => 120
]);
````$cache->serve()` method has optional filter argument. By passing a callable it's possible to filter response, e.g. to add timestamp to response.
A script by [@martin_adamko](https://twitter.com/martin_adamko)