2017 © Pedro Peláez
 

library psr-cache-mongodb

PSR-16 SimpleCache Implementation using MongoDB

image

subjective-php/psr-cache-mongodb

PSR-16 SimpleCache Implementation using MongoDB

  • Tuesday, July 17, 2018
  • by chadicus
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Simple MongoDB Cache

Build Status Scrutinizer Code Quality Coverage Status, (*1)

Latest Stable Version Latest Unstable Version License, (*2)

Total Downloads Daily Downloads Monthly Downloads, (*3)

Documentation, (*4)

PSR-16 SimpleCache Implementation using MongoDB, (*5)

Requirements

Requires PHP 7.0 (or later)., (*6)

Composer

To add the library as a local, per-project dependency use Composer! Simply add a dependency on subjective-php/psr-cache-mongodb to your project's composer.json file such as:, (*7)

composer require subjective-php/psr-cache-mongodb

Contact

Developers may be contacted at:, (*8)

Project Build

With a checkout of the code get Composer in your PATH and run:, (*9)

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., (*10)

<?php

use Chadicus\Psr\SimplCache\SerializerInterface;
use Chadicus\Psr\SimplCache\MongoCache;
use GuzzleHttp\Psr7;
use MongoDB\Client;
use Psr\SimpleCache\InvalidArgumentException;

/**
 * Provides serialization from mongo documents to PSR-7 response objects.
 */
final class Psr7Serializer implements SerializerInterface
{
    /**
     * Unserializes cached data into the original state.
     *
     * @param array $data The data to unserialize.
     *
     * @return Diactoros\Response
     */
    public function unserialize(array $data)
    {
        return new Psr7\Response(
            $data['statusCode'],
            $data['headers'],
            $data['body'],
            $data['protocolVersion'],
            $data['reasonPhrase']
        );
    }

    /**
     * Serializes the given data for storage in caching.
     *
     * @param mixed $value The data to serialize for caching.
     *
     * @return array The result of serializing the given $data.
     *
     * @throws InvalidArgumentException Thrown if the given value is not a PSR-7 Response instance.
     */
    public function serialize($value) : array
    {
        if (!is_a($value, '\\Psr\\Http\\Message\\ResponseInterface')) {
            throw new class('$value was not a PSR-7 Response') extends \Exception implements InvalidArgumentException { };
        }

        return [
            'statusCode' => $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);
}

The Versions

17/07 2018

dev-master

9999999-dev

PSR-16 SimpleCache Implementation using MongoDB

  Sources   Download

MIT

The Requires

 

The Development Requires

psr cache mongo psr16

17/07 2018

v2.0.0

2.0.0.0

PSR-16 SimpleCache Implementation using MongoDB

  Sources   Download

MIT

The Requires

 

The Development Requires

psr cache mongo psr16

07/01 2018

v1.0.0

1.0.0.0

PSR-16 SimpleCache Implementation using MongoDB

  Sources   Download

MIT

The Requires

 

The Development Requires

psr cache mongo psr16

10/04 2017

v0.1.0

0.1.0.0

PSR-16 SimpleCache Implementation using MongoDB

  Sources   Download

MIT

The Requires

 

The Development Requires

psr cache mongo psr16