2017 © Pedro Peláez
 

library cacheasy

I hate slow APIs, I cache things on disk.

image

mattmezza/cacheasy

I hate slow APIs, I cache things on disk.

  • Tuesday, April 3, 2018
  • by mattmezza
  • Repository
  • 1 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 40 % Grown

The README.md

cacheasy

Scrutinizer Code Quality Code Coverage Build Status, (*1)

I hate slow APIs, I cache things on disk., (*2)

Install

composer require mattmezza/cacheasy, (*3)

Usage

With string responses:, (*4)

$providerStrAPI = new class implements Cacheasy\StringProvider {
    public function get() : string
    {
        return (new SlowAPIsClient())->slowAPI();
    }
};
// ./cache is the cache path, 86400 is the time to live
$cache = new Cache("./cache", 86400);
// if slowAPI is not cached let's get the data and cache them
$result = $cache->getString("slowAPI", $providerStrAPI); # this is slow :(
$result2 = $cache->getString("slowAPI", $providerStrAPI); # this is blazing fast :)
echo $result2;

With JSON responses:, (*5)

$providerJsonAPI = new class implements Cacheasy\JsonProvider {
    public function get() : array
    {
        return (new SlowAPIsClient())->slowAPI();
    }
};
// ./cache is the cache path, 86400 is the time to live
$cache = new Cache("./cache", 86400);
// if slowAPI is not cached let's get the data and cache them
$result = $cache->getJson("slowjsonAPI", $providerJsonAPI); # this is slow :(
$result2 = $cache->getJson("slowjsonAPI", $providerJsonAPI); # this is blazing fast :)
echo $result2["property"];

API

  • cacheString($key, $string) : string: caches a string with key
  • cacheJson($key, $array) : array: caches an array to json with key
  • hitString($key) : string: tries to resume from cache a string with key
  • hitJson($key) : array: tries to resume from cache a json with key
  • isCached($key) : bool: checks if key is cached on disk and if it is not expired
  • getJson($key, $provider = null, bool $forceFresh = false) : array: returns hitJson(...) if key is cached, calls provider otherwise. Throws exception if $provider is null and $key is not cached. If $forceFresh is set to true skips isCached check and calls the provider (ultimately caching the data).
  • getString($key, $provider = null, bool $forceFresh = false) : string: returns hitString(...) if key is cached, calls provider otherwise. Throws exception if $provider is null and $key is not cached. If $forceFresh is set to true skips isCached check and calls the provider (ultimately caching the data).
  • invalidate($key) : void: deletes the cached resource
  • invalidateAll() : void: deletes all the cached resources

Exceptions

MissingProviderException: when get..(...) is called for a non cached resource and no provider is passed, or when, even if the resource is cached, the method is invoked with null provider and with true force fresh values. NotCachedException: when you wanna hit the cache but the resource is not cached yet., (*6)

Development

  • git clone https://github.com/mattmezza/cacheasy.git
  • cd cacheasy
  • composer test
Matteo Merola mattmezza@gmail.com

The Versions

03/04 2018

dev-master

9999999-dev

I hate slow APIs, I cache things on disk.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

03/04 2018

1.1

1.1.0.0

I hate slow APIs, I cache things on disk.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

30/03 2018

1.0.0

1.0.0.0

I hate slow APIs, I cache things on disk.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires