clickalicious/cachingmiddleware

The PSR-7 middleware for caching compatible to PSR-6.

v1.1.1 2016-04-16 00:21 UTC

This package is not auto-updated.

Last update: 2024-12-21 20:03:18 UTC


README

Logo of Caching-Middleware

The PSR-7 middleware for caching compatible to PSR-6.

Table of Contents

Features

  • Caching of almost any content produced in a PHP execution loop like PHP, HTML (...)
  • Stackable into PSR-7 stacks for easy integration
  • Compatible to all PSR-6 caching implementations and a lot of caching backends
  • High performance (developed using a profiler)
  • Lightweight and high-quality codebase (following PSR-1,2,4,7)
  • 100% PSR-7 middleware compatibility
  • 100% PSR-6 caching compatibility
  • Clean & well documented code
  • Unit-tested with a good coverage

Example

This is just a simple demonstration on how to get started using this middleware library in a very simple context. Put a Cache in queue and use a PSR-6 Cache (Filesystem as backend - but you could also use Redis, Memcached, MySQL or any other backend implemented a PSR-6 caching layer ...):

/**
 * Fill queue for running "Caching Middleware"
 *
 * @param \Psr\Http\Message\ServerRequestInterface $request  Request (PSR) to process
 * @param \Psr\Http\Message\ResponseInterface      $response Response (PSR) to use
 * @param callable                                 $next     Next middleware in stack
 *
 * @return \Psr\Http\Message\ResponseInterface A PSR compatible response
 */
$queue[] = function (Request $request, Response $response, callable $next) {

    // Create cache item factory
    $cacheItemFactory = function ($key) {
        return new CacheItem($key);
    };

    // Create cache item key factory
    $cacheItemKeyFactory = function (Request $request) {
        static $key = null;
        if (null === $key) {
            $uri     = $request->getUri();
            $slugify = new Slugify();
            $key     = $slugify->slugify(trim($uri->getPath(), '/').($uri->getQuery() ? '?'.$uri->getQuery() : ''));
        }

        return $key;
    };

    // Get cache
    $cachingMiddleWare = new Clickalicious\Caching\Middleware\Cache(
        new CacheItemPool('Filesystem'),
        $cacheItemFactory,
        $cacheItemKeyFactory
    );

    return $cachingMiddleWare($request, $response, $next);
};

Requirements

  • PHP >= 5.6 (compatible up to version 7.2 as well as HHVM)

Philosophy

Caching Middleware is a PSR-7 compatible middleware based on PSR-6 compatible cache implementations. Caching Middleware isn't a unicorn - it's good but it maybe going to change with time. Try it, run it ... ♥ it ;)

Versioning

For a consistent versioning we decided to make use of Semantic Versioning 2.0.0 http://semver.org. Its easy to understand, very common and known from many other software projects.

Roadmap

  • Cache whole response instead just rendered HTML (Headers as well for example)
  • Implement flysystem as Driver for PSR-Cache

Throughput Graph

Security Issues

If you encounter a (potential) security issue don't hesitate to get in contact with us opensource@clickalicious.de before releasing it to the public. So i get a chance to prepare and release an update before the issue is getting shared. Thank you!

Participate & Share

... yeah. If you're a code monkey too - maybe we can build a force ;) If you would like to participate in either Code, Comments, Documentation, Wiki, Bug-Reports, Unit-Tests, Bug-Fixes, Feedback and/or Critic then please let us know as well!

Sponsors

Thanks to our sponsors and supporters:

Copyright
Icons made by Prosymbols from www.flaticon.com is licensed by CC 3.0 BY