I hate slow APIs, I cache things on disk.
composer require mattmezza/cacheasy
With string responses:
$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:
$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"];
cacheString($key, $string) : string
: caches a string with keycacheJson($key, $array) : array
: caches an array to json with keyhitString($key) : string
: tries to resume from cache a string with keyhitJson($key) : array
: tries to resume from cache a json with keyisCached($key) : bool
: checks if key is cached on disk and if it is not expiredgetJson($key, $provider = null, bool $forceFresh = false) : array
: returnshitJson(...)
if key is cached, calls provider otherwise. Throws exception if $provider is null and $key is not cached. If $forceFresh is set totrue
skips isCached check and calls the provider (ultimately caching the data).getString($key, $provider = null, bool $forceFresh = false) : string
: returnshitString(...)
if key is cached, calls provider otherwise. Throws exception if $provider is null and $key is not cached. If $forceFresh is set totrue
skips isCached check and calls the provider (ultimately caching the data).invalidate($key) : void
: deletes the cached resourceinvalidateAll() : void
: deletes all the cached resources
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.
git clone https://github.com/mattmezza/cacheasy.git
cd cacheasy
composer test