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-04-13 16:18:10 UTC


README

26927954?v=3&s=80

Logo of Caching-Middleware

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

Build Status Codacy grade Codacy coverage clickalicious open source
GitHub release license Issue Stats Dependency Status

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! 687474703a2f2f6a70696c6c6f72612e636f6d2f6769746875622d747769747465722d627574746f6e2f696d672f74776565742e706e67

Sponsors

Thanks to our sponsors and supporters:

JetBrains Navicat
68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f6173736574732f6d656469612f6f70656e2d67726170682f6a6574627261696e735f323530783235302e706e67 687474703a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f656e2f392f39302f5072656d69756d536f66745f4e6176696361745f5072656d69756d5f4c6f676f2e706e67
Copyright
Icons made by Prosymbols from www.flaticon.com is licensed by CC 3.0 BY