https://github.com/chadicus/psr-cache-mongodb
PSR-16 SimpleCache Implementation using MongoDB
https://github.com/chadicus/psr-cache-mongodb
Last synced: 5 months ago
JSON representation
PSR-16 SimpleCache Implementation using MongoDB
- Host: GitHub
- URL: https://github.com/chadicus/psr-cache-mongodb
- Owner: chadicus
- License: mit
- Fork: true (subjective-php/psr-cache-mongodb)
- Created: 2018-07-15T14:18:37.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-09-19T16:14:18.000Z (over 3 years ago)
- Last Synced: 2024-12-09T19:51:48.739Z (over 1 year ago)
- Language: PHP
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Simple MongoDB Cache
[](https://travis-ci.org/subjective-php/psr-cache-mongodb)
[](https://scrutinizer-ci.com/g/subjective-php/psr-cache-mongodb/?branch=master)
[](https://coveralls.io/github/subjective-php/psr-cache-mongodb?branch=master)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](https://packagist.org/packages/subjective-php/psr-cache-mongodb)
[](http://www.pholiophp.org/subjective-php/psr-cache-mongodb)
[PSR-16 SimpleCache](http://www.php-fig.org/psr/psr-16/) Implementation using [MongoDB](https://docs.mongodb.com/php-library/master/)
## Requirements
Requires PHP 7.0 (or later).
## Composer
To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on `subjective-php/psr-cache-mongodb` to your project's `composer.json` file such as:
```sh
composer require subjective-php/psr-cache-mongodb
```
## Contact
Developers may be contacted at:
* [Pull Requests](https://github.com/subjective-php/psr-cache-mongodb/pulls)
* [Issues](https://github.com/subjective-php/psr-cache-mongodb/issues)
## Project Build
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:
```sh
composer install
./vendor/bin/phpunit
```
## Example Caching PSR-7 Response Messages with Guzzle Client
Below is a very simplified example of caching responses to GET requests in mongo.
```php
$value->getStatusCode(),
'headers' => $value->getHeaders(),
'body' => (string)$value->getBody(),
'protocolVersion' => $value->getProtocolVersion(),
'reasonPhrase' => $value->getReasonPhrase(),
];
}
}
//create the mongo collection
$collection = (new Client('mongodb://locathost:27017'))->selectDatabase('psr')->selectCollection('cache');
//Set a TTL index on the expires field
$collection->createIndex(['expires' => 1], ['expireAfterSeconds' => 0]);
$cache = new MongoCache($collection, new Psr7Serializer());
// Use the cache when sending guzzle requests
//Only caching GET responses
if ($request->getMethod() === 'GET') {
$key = (string)$request->getUri();
$response = $cache->get($key);
if ($response === null) {
$response = $guzzleClient->send($request);
//Add to cache if valid Expires header
if ($response->hasHeader('Expires')) {
$expires = strtotime($response->getHeader('Expires')[0]);
$cache->set($key, $response, $expires - time());
}
}
} else {
$response = $guzzleClient->send($request);
}
```