Download the PHP package henzeb/warmable without Composer
On this page you can find all versions of the php package henzeb/warmable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package warmable
Warmable
This package was inspired by a talk by @timacdonald where he showed a technique to warm up a cache entry scheduled by using a simple invokable class.
When you cache the result of a heavy operation, once in a while, some user will have to wait for the operation to be cached again. If the request rate is high enough, multiple people may have to do so at the same time.
This package provides the framework in which you can take all that away. It is implementing the PSR16 interface for caching, so it pretty much supports every caching mechanism you can think of, and if not, one can implement a wrapper, and it still supports every caching mechanism you can think of.
Laravel-developers can use this package as well, but in order to harvest the full power of Laravel, it's better to install and use Warmable for Laravel
Installation
Just install with the following command.
Terminology
Warm up / warming up
The term warm up
or warming up
refers to caching
during a cron job or in a queue job.
preheating
Preheating is the term used when the cache is populated during execution, which is the default operation when the cache does not exist. This may also be dispatched to a background operation like a Queueing System.
Usage
Creating a Warmable
Creating a Warmable
is pretty easy.
Note: The key
and the ttl
can be omitted. When omitting the key
,
a key is generated based on the FQCN of your object. When ttl
is omitted,
the Warmable
result will be stored in cache forever.
chaining methods.
Warmable
allows chaining methods. You can start with any method you wish.
make
With make
you can easily get an instance of your Warmable
. You can add
arguments that will be passed on to your constructor.
get
With this method you can easily access the cached data.
By default,Warmable
preheats the data for the first time if it does
not exist yet. If you don't want that to happen before response,
you can set a default. In that case it will postpone that action until
after the response is sent to the browser.
Note: this doesn't work with null
.
Use withoutPreheating instead.
with
The warmable method supports a rudimentary version of dependency injection. Using this method, you can make your cache unique per user or item. You don´t have to do anything else besides passing it into the constructor.
key
By default, Warmable
will generate a unique key for you. You don't need to do
anything. Even if you want to reuse the Warmable
for different items,
Warmable
got your back. But if you do want to customize the key, there are 2
ways to do it.
The first is by setting the key
method on your Warmable
class.
The second option is inline:
Cache
The cache is, obviously, a required element. It's set through the mandatory
cache
method. There is also a withCache
method that can be used inline.
Note: Warmable
accepts any Cache implementation that implements the PSR-16
CacheInterface
. If the cache system you are using does not implement it,
you can easily create a wrapper that does implement it.
Time To Live (Ttl)
Allows you to use a different TTL than the one defined inside your class.
Note Ttl is null
, and thus forever
, by default.
Or you can set things inline inline:
Grace periods
In combination with a Ttl, a grace period allows you to give the user the old data while updating the cache. This updating happens after returning the response to the browser.
The example below will create a cache item that lives for 600 seconds. 300+ seconds in, the next call will return the value in the cache, and registers a shutdown function that updates the cache item, which will live for 600 seconds, and so on.
getKey
There may be occasions where you need to see the key used by your Warmable
.
missing
isPreheated
This method tells you if the cache was preheated.
withoutPreheating
This method allows you to explicitly disable preheating.
withPreheating
Works exactly like withoutPreheating, except it switches the preheating on.
shouldPreheat
A method that tells you if preheat flag is turned on
cooldown
If for some reason you need to delete the cache, you can call cooldown
.
Custom warmup strategy
If your application has access to a queue, it is easy to change the preheat strategy. By default, it preheats during execution.
Note: Returning null
causes the get
method to use the default.
Overriding public interface
Under the hood, Warmable
uses __call
and __callStatic
to allow chaining
static and dynamic calls. If you want to override a method on the public interface,
you should find it's call<method>
counterpart.
For Example: If you want to override the with
method, you should extend the
callWith
method.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The GNU AGPLv. Please see License File for more information.