Download the PHP package kielabokkie/uber-cache without Composer
On this page you can find all versions of the php package kielabokkie/uber-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kielabokkie/uber-cache
More information about kielabokkie/uber-cache
Files in kielabokkie/uber-cache
Package uber-cache
Short Description Retrieve & store cache with a fallback for a specific duration
License MIT
Informations about the package uber-cache
UberCache for Laravel
UberCache for Laravel works very similar to the Retrieve & Store cache functionality of Laravel. The difference is that once Laravel's cache expires, and the retrieval of new data failed, you are left with no data at all. This is where UberCache comes in, as it allows you to reuse your old cache in case retrieving of new data failed.
Sponsor Me
If you find this package useful, or it somehow sparks joy, please consider sponsoring me.
Requirements
- PHP >= 7.4
- Laravel 6.x and higher
Installation
Install the package via composer:
TLDR
Usage
As mentioned before, UberCache provides just one function that is very similar to Laravel's remember
cache function. Let's start by looking at an example of this first:
As you can see it takes 3 parameters: the cache key, the lifetime of the value in your cache and a callback function that's responsible for retrieving the data.
Now let's look at UberCache's remember function, it takes one extra parameter before the callback called maxTtl
. This is used to set the maximum time your cache is allowed to be used.
It's clear that it's almost identical to Laravel's remember function but thanks to the maxTtl
parameter our cache is a little smarter. The example above will cache the todo that is fetched for 1 minute. If this function is called again after more than 1 minute the cache is expired, so the API call is executed again. If the API call fails for whatever reason, maybe it is down temporary, UberCache will restore the old cached value and continue as without breaking.
If the API calls keep on failing and no new data can be fetched within the time set as the maxTtl
then an exception will be thrown. This is to ensure you are not working with old cache data forever without being aware of it.