Smooth caching of HTTP requested data. It runs non-blocking and uses background processes to request the data.

1.4.2 2024-04-22 14:40 UTC

This package is auto-updated.

Last update: 2024-05-22 14:51:09 UTC


PHP from Packagist Codacy Badge Latest Stable Version Total Downloads License

Request Cache

Smooth caching of HTTP requested data. It runs non-blocking and uses background processes to request the data.


This library is available for the use with Composer. Add it to your project by running $ composer require bitandblack/request-cache.


At first initialize a cache type and a process object, and use them to initialize the request handler object. In this example we're going to use the FileSystemCache:


use BitAndBlack\RequestCache\CacheType\FileSystemCache;
use BitAndBlack\RequestCache\Process;
use BitAndBlack\RequestCache\RequestHandler;

$cache = new FileSystemCache(__DIR__);
$process = new Process();
$requestHandler = new RequestHandler($cache, $process);

The request handler is now ready to be used. Now create a request object with the URL you want to request and the time to live (ttl). This example requests the URL and allows the data to be stored for 1 hour:


use BitAndBlack\RequestCache\Request;

$request = new Request(

The data can be requested now:


$response = $requestHandler->getResponse($request);

The request handler will return the response immediately and without blocking the script. If the data has been expired, the last response will be returned from cache, while the new data will be requested in the background.

When successful, the response will be a GuzzleHttp\Psr7\Response object, that implements the Psr\Http\Message\ResponseInterface.


Custom request client

Per default, this library uses Guzzle to make requests. You can use the request callback to set up a custom request client or to modify the configuration.


use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;

    function(string $url): ResponseInterface 
        $client = new Client();
        return $client->request(
                'allow_redirects' => false


The ttl value may be easier to set with the help of the bitandblack/duration library. For example instead of writing 86400 for the duration of a whole day, you can write Duration::createFromDays(1)->getSeconds() then.


If you have any questions, feel free to contact us under

Further information about Bit&Black can be found under