GithubHelp home page GithubHelp logo

peroks / guzzle-file-cache Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 37 KB

A lightweight file cache for Guzzle 7+ implementing the PSR-6 Caching Interface

License: MIT License

PHP 100.00%
caching file-cache psr-16 lightweight api-client php cache guzzle guzzle-handler

guzzle-file-cache's Introduction

Simple file caching for the Guzzle http client

A lightweight file cache for Guzzle 7+ implementing the PSR-16 Caching Interface

How to use

<?php

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Peroks\GuzzleFileCache\Cache;
use Peroks\GuzzleFileCache\FileStorage;

function getGuzzleClient( array $options = [] ): Client {
    $stack = HandlerStack::create();
    $store = new FileStorage( 'your/cache/directory' );
    $cache = new Cache( $store );

    $stack->push( $cache );
    $options['handler'] = $stack;

    return new Client( $options );
}

You use an instance of the Peroks\GuzzleFileCache\Cache class to add file-caching as a handler (middleware) to the Guzzle http client.

Peroks\GuzzleFileCache\FileStorage does the real work, implementing the PSR-16: Common Interface for Caching Libraries.

After you have added caching to the Guzzle http client, there is a new option for caching: ttl (time-to-live). The ttl value is in seconds.

You can set this option for each request sent through the client. If you omit this option or set ttl to 0, the request is not cached.

$response = $client->send( $request, [ 'ttl' => 3600 ] ); // 1 hour caching.

Installing

You need composer to download and install peroks/guzzle-file-cache. Just run composer require peroks/guzzle-file-cache in your project.

guzzle-file-cache's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.