chanshige/slim-bodycache

This package is abandoned and no longer maintained. No replacement package was suggested.

A slim middleware for Filesystem Cache based on symfony/cache

v0.0.3 2019-02-20 10:57 UTC

This package is auto-updated.

Last update: 2020-10-04 08:25:35 UTC


README

Packagist Build Status

A slim middleware for Filesystem Cache based on symfony/cache.

Install

Via Composer

$ composer require chanshige/slim-bodycache
  
# symfony/cacheを使う場合はこちらも   
$ composer require symfony/cache

Requires Slim 3.0.0 or newer.

Usage

<?php

$app = new \Slim\App();

// CacheConfig
$namespace = 'cache';
$defaultLifetime = 3600;
$directory = '/path/to/dir';

// Symfony Filesystem Cache
// PSR-6/16 Interfaceを持つコンポーネントを使用
$sfCache = new \Symfony\Component\Cache\Simple\FilesystemCache(
    $namespace,
    $defaultLifetime,
    $directory
);

// Register middleware
$app->add(new \Chanshige\Slim\BodyCache\Cache($sfCache));

// Fetch DI Container
$container = $app->getContainer();

$container['body_cache'] = function () use ($sfCache) {
    return new \Chanshige\Slim\BodyCache\Cache($sfCache);
};

// Json Response
$app->get('/foo', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
    
    if($request->getAttribute('has_body_cache')) {
        return $response
            ->withHeader("Content-type", "application/json;charset=utf-8");
    }

    return $response
        ->withStatus(200)
        ->withJson(['Hello World!!']);
})->add($container->get('body_cache'));

$app->run();

Contributing

We welcome bug reports, feature requests and pull requests through Slim-BodyCache Github issue tracker.

License

The Slim-BodyCache is licensed under the MIT license.