{"id":37014072,"url":"https://github.com/chadicus/psr-cache-mongodb","last_synced_at":"2026-01-14T01:23:49.691Z","repository":{"id":62500891,"uuid":"141033291","full_name":"chadicus/psr-cache-mongodb","owner":"chadicus","description":"PSR-16 SimpleCache Implementation using MongoDB","archived":false,"fork":true,"pushed_at":"2022-09-19T16:14:18.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-09T19:51:48.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"subjective-php/psr-cache-mongodb","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chadicus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2018-07-15T14:18:37.000Z","updated_at":"2022-09-19T15:29:07.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/chadicus/psr-cache-mongodb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chadicus/psr-cache-mongodb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chadicus%2Fpsr-cache-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chadicus%2Fpsr-cache-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chadicus%2Fpsr-cache-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chadicus%2Fpsr-cache-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chadicus","download_url":"https://codeload.github.com/chadicus/psr-cache-mongodb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chadicus%2Fpsr-cache-mongodb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28407696,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T01:23:49.046Z","updated_at":"2026-01-14T01:23:49.679Z","avatar_url":"https://github.com/chadicus.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple MongoDB Cache\n\n[![Build Status](https://travis-ci.org/subjective-php/psr-cache-mongodb.svg?branch=master)](https://travis-ci.org/subjective-php/psr-cache-mongodb)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/subjective-php/psr-cache-mongodb/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/subjective-php/psr-cache-mongodb/?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/subjective-php/psr-cache-mongodb/badge.svg?branch=master)](https://coveralls.io/github/subjective-php/psr-cache-mongodb?branch=master)\n\n[![Latest Stable Version](https://poser.pugx.org/subjective-php/psr-cache-mongodb/v/stable)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n[![Latest Unstable Version](https://poser.pugx.org/subjective-php/psr-cache-mongodb/v/unstable)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n[![License](https://poser.pugx.org/subjective-php/psr-cache-mongodb/license)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n\n[![Total Downloads](https://poser.pugx.org/subjective-php/psr-cache-mongodb/downloads)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n[![Daily Downloads](https://poser.pugx.org/subjective-php/psr-cache-mongodb/d/daily)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n[![Monthly Downloads](https://poser.pugx.org/subjective-php/psr-cache-mongodb/d/monthly)](https://packagist.org/packages/subjective-php/psr-cache-mongodb)\n\n[![Documentation](https://img.shields.io/badge/reference-phpdoc-blue.svg?style=flat)](http://www.pholiophp.org/subjective-php/psr-cache-mongodb)\n\n[PSR-16 SimpleCache](http://www.php-fig.org/psr/psr-16/) Implementation using [MongoDB](https://docs.mongodb.com/php-library/master/)\n\n## Requirements\n\nRequires PHP 7.0 (or later).\n\n## Composer\nTo 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:\n\n```sh\ncomposer require subjective-php/psr-cache-mongodb\n```\n\n## Contact\nDevelopers may be contacted at:\n\n * [Pull Requests](https://github.com/subjective-php/psr-cache-mongodb/pulls)\n * [Issues](https://github.com/subjective-php/psr-cache-mongodb/issues)\n\n## Project Build\nWith a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:\n\n```sh\ncomposer install\n./vendor/bin/phpunit\n```\n## Example Caching PSR-7 Response Messages with Guzzle Client\n\nBelow is a very simplified example of caching responses to GET requests in mongo.\n\n```php\n\u003c?php\n\nuse Chadicus\\Psr\\SimplCache\\SerializerInterface;\nuse Chadicus\\Psr\\SimplCache\\MongoCache;\nuse GuzzleHttp\\Psr7;\nuse MongoDB\\Client;\nuse Psr\\SimpleCache\\InvalidArgumentException;\n\n/**\n * Provides serialization from mongo documents to PSR-7 response objects.\n */\nfinal class Psr7Serializer implements SerializerInterface\n{\n    /**\n     * Unserializes cached data into the original state.\n     *\n     * @param array $data The data to unserialize.\n     *\n     * @return Diactoros\\Response\n     */\n    public function unserialize(array $data)\n    {\n        return new Psr7\\Response(\n            $data['statusCode'],\n            $data['headers'],\n            $data['body'],\n            $data['protocolVersion'],\n            $data['reasonPhrase']\n        );\n    }\n\n    /**\n     * Serializes the given data for storage in caching.\n     *\n     * @param mixed $value The data to serialize for caching.\n     *\n     * @return array The result of serializing the given $data.\n     *\n     * @throws InvalidArgumentException Thrown if the given value is not a PSR-7 Response instance.\n     */\n    public function serialize($value) : array\n    {\n        if (!is_a($value, '\\\\Psr\\\\Http\\\\Message\\\\ResponseInterface')) {\n            throw new class('$value was not a PSR-7 Response') extends \\Exception implements InvalidArgumentException { };\n        }\n\n        return [\n            'statusCode' =\u003e $value-\u003egetStatusCode(),\n            'headers' =\u003e $value-\u003egetHeaders(),\n            'body' =\u003e (string)$value-\u003egetBody(),\n            'protocolVersion' =\u003e $value-\u003egetProtocolVersion(),\n            'reasonPhrase' =\u003e $value-\u003egetReasonPhrase(),\n        ];\n    }\n}\n\n//create the mongo collection\n$collection = (new Client('mongodb://locathost:27017'))-\u003eselectDatabase('psr')-\u003eselectCollection('cache');\n//Set a TTL index on the expires field\n$collection-\u003ecreateIndex(['expires' =\u003e 1], ['expireAfterSeconds' =\u003e 0]);\n\n$cache = new MongoCache($collection, new Psr7Serializer());\n\n// Use the cache when sending guzzle requests\n\n//Only caching GET responses\nif ($request-\u003egetMethod() === 'GET') {\n    $key = (string)$request-\u003egetUri();\n    $response = $cache-\u003eget($key);\n    if ($response === null) {\n        $response = $guzzleClient-\u003esend($request);\n        //Add to cache if valid Expires header\n        if ($response-\u003ehasHeader('Expires')) {\n            $expires = strtotime($response-\u003egetHeader('Expires')[0]);\n            $cache-\u003eset($key, $response, $expires - time());\n        }\n    }\n} else {\n    $response = $guzzleClient-\u003esend($request);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchadicus%2Fpsr-cache-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchadicus%2Fpsr-cache-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchadicus%2Fpsr-cache-mongodb/lists"}