Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lmammino/guzzle-apcu-fs-cache
A zero config cache storage for guzzle-cache-middleware that tries to cache on Apcu and fallbacks to filesystem
https://github.com/lmammino/guzzle-apcu-fs-cache
apcu cache cache-storage filesystem guzzle http middleware php
Last synced: 2 months ago
JSON representation
A zero config cache storage for guzzle-cache-middleware that tries to cache on Apcu and fallbacks to filesystem
- Host: GitHub
- URL: https://github.com/lmammino/guzzle-apcu-fs-cache
- Owner: lmammino
- License: mit
- Created: 2020-04-01T11:32:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T19:31:35.000Z (over 1 year ago)
- Last Synced: 2024-10-04T15:41:08.203Z (3 months ago)
- Topics: apcu, cache, cache-storage, filesystem, guzzle, http, middleware, php
- Language: PHP
- Size: 41 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# guzzle-apcu-fs-cache
A zero config cache storage for guzzle-cache-middleware that tries to cache on Apcu and fallbacks to filesystem.
![Tests](https://github.com/lmammino/guzzle-apcu-fs-cache/workflows/Tests/badge.svg)
[![Latest Stable Version](https://poser.pugx.org/lmammino/guzzle-apcu-fs-cache/v/stable?)](https://packagist.org/packages/lmammino/guzzle-apcu-fs-cache)
[![Total Downloads](https://poser.pugx.org/lmammino/guzzle-apcu-fs-cache/downloads?)](https://packagist.org/packages/lmammino/guzzle-apcu-fs-cache)
[![Latest Unstable Version](https://poser.pugx.org/lmammino/guzzle-apcu-fs-cache/v/unstable?)](https://packagist.org/packages/lmammino/guzzle-apcu-fs-cache)
[![License](https://poser.pugx.org/lmammino/guzzle-apcu-fs-cache/license?)](https://packagist.org/packages/lmammino/guzzle-apcu-fs-cache)
[![composer.lock](https://poser.pugx.org/lmammino/guzzle-apcu-fs-cache/composerlock?)](https://packagist.org/packages/lmammino/guzzle-apcu-fs-cache)## Install
Requires PHP 5.5+.
Using composer:
```bash
composer require lmammino/guzzle-apcu-fs-cache
```## Usage
This is an example of how you can use this storage engine with [Guzzle](https://guzzlephp.org) and [GuzzleCacheMiddleware](https://github.com/Kevinrob/guzzle-cache-middleware):
```php
setHandler(new CurlHandler());
$stack->push($cacheMiddleware, 'cache');
$client = new Client(['handler' => $stack]);// make a request
$res = $client->request('GET', 'https://loige.co/');
// print response headers
$headers = $res->getHeaders();
foreach ($headers as $k => $h) {
foreach ($h as $v) {
echo $k . ": " . $v . "\n";
}
}
// print response body
echo "\n\n" . $res->getBody()->__toString() . "\n\n";
```### Use with Launch Darkly PHP SDK
If you want to use this cache storage with the [Launch Darkly PHP SDK](https://docs.launchdarkly.com/sdk/server-side/php), here's how you can do it:
```php
$cacheStorage]);
$LDUser = new LDUser("[email protected]");// use the client
var_dump($LDClient->variation('some-flag', $LDUser));
```## Configuration
The storage layer can be configured at construction time.
These are the following parameters accepted by the constructor:
- (string) `$dir`: The directory where the cache should be saved if using the filesystem (Default: the temp directory).
- (string) `$namespace`: A namespace for the cache storage, useful when using multiple instances and cache should not be mixed between them. It will create a subfolder on the filesystme and a prefix on APC. (Default: `'default'`).
- (integer) `$ttl` The duration of a cache entry in seconds (Default: `60`).
- (callable) `$onHit` An optional function that gets called when there's a cache hit.
- (callable) `$onMiss` An optional function that gets called when there's a cache miss.If you want to inspect whether your current instance is using APC or the filesystem you can use the following public properties:
- `$storage->usingApcu` (returns `true` or `false`)
- `$storage->usingFilesystem` (returns `true` or `false`)## Rationale and Notes
This was originally created to cache requests made by the Launch Darkly client in a more controlled and configurable way.
**Note**: this middleware will always respect the TTL you provide and will ignore any HTTP cache header returned as response. This is by design. Use this middleware only if you want to enforce cache or for micro-caching scenarios (e.g. very expensive and frequent API calls).
## Contributing
Everyone is very welcome to contribute to this project.
You can contribute just by submitting bugs or suggesting improvements by
[opening an issue on GitHub](https://github.com/lmammino/guzzle-apcu-fs-cache/issues).## License
Licensed under [MIT License](LICENSE). © Luciano Mammino.