PHPackages                             henzeb/warmable-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. henzeb/warmable-laravel

ActiveLibrary[Caching](/categories/caching)

henzeb/warmable-laravel
=======================

A cache warmer for the Laravel Framework

v0.1.0(2y ago)161[1 issues](https://github.com/henzeb/warmable-laravel/issues)AGPL-3.0-onlyPHPPHP ^8.1

Since Feb 19Pushed 2y ago1 watchersCompare

[ Source](https://github.com/henzeb/warmable-laravel)[ Packagist](https://packagist.org/packages/henzeb/warmable-laravel)[ Docs](https://github.com/henzeb/warmable-laravel)[ RSS](/packages/henzeb-warmable-laravel/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Warmable for Laravel
====================

[](#warmable-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e0e8c1fe6139e3c6113059c61b033622866abdc0f53cb634717a46d936748bed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656e7a65622f7761726d61626c652d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henzeb/warmable-laravel)[![Total Downloads](https://camo.githubusercontent.com/aeda88bdc85b367737e00297dbc69a2a742264094f5a631e226e1ffc8228c3f8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656e7a65622f7761726d61626c652d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henzeb/warmable-laravel)

This package was inspired by a talk on LaraconEU 2024 by [@timacdonald](https://github.com/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 aims to take away this situation as much as possible, by utilizing both the Scheduler and the Queue.

It's build with Laravel in mind, but as I used the PSR-16 `psr/simple-cache`CacheInterface, anyone can use it. Just install the vanilla [Warmable](https://packagist.org/packages/henzeb/warmable), as this package is Laravel Specific

Installation
------------

[](#installation)

Just install with the following command.

```
composer require henzeb/warmable-laravel
```

Terminology
-----------

[](#terminology)

#### Warm up / warming up

[](#warm-up--warming-up)

The term warm up or warming up refers to caching during a cron or at the queue.

#### preheating

[](#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
-----

[](#usage)

The explanation here is specific to laravel. For more information, read the docs at [Warmable](https://packagist.org/packages/henzeb/warmable).

### Creating a Warmable

[](#creating-a-warmable)

Creating a `Warmable` is pretty easy. As you can see, no `cache` method is required, as out of the box, `Warmable` is using the default `Cache` driver.

You still can override it and use a different driver if you choose so. You don't have to stick with a `CacheInterface` implementation, you can use strings as well. Anywhere you can input the cache driver, you can use strings.

Note: In fact, using a string is performance wise better, because on serialization for the Queue, it needs to loop through all your configured stores to determine the name it came with.

```
use Henzeb\Warmable\Illuminate\Warmable;
use DateTimeInterface;
use DateInterval;

class HeavyOperation extends Warmable
{
    protected function warmable(): mixed
    {
         // ... your heavy operation
    }
}
```

### Dependency Injection

[](#dependency-injection)

`Warmable Laravel` utilizes the Dependency Injection system from laravel. It works similar to how dependencies are resolved on a `Controller`, as it uses the same implementation.

```
use Henzeb\Warmable\Illuminate\Warmable;
use DateTimeInterface;
use DateInterval;

class HeavyOperation extends Warmable
{
    protected function warmable(YourInjectedService $service): mixed
    {
         // ... your heavy operation
    }
}
```

Dependencies can still be injected with the `with` method, just like the vanilla `Warmable` implementation, with the addition that you can use an associated array to inject custom variables.

Below example will inject the instance of `YourInjectedService`, rather than resolving a new instance using the Application Container.

```
HeavyOperation::with([
    'service' => new YourInjectedService()
]);

// The following however isn't possible:
HeavyOperation::with(
    [
        'service' => new YourInjectedService()
    ],
    'another item'
);
```

Note: Be aware that services injected using `with` are changing the key, making it unique. See [Warmable](https://packagist.org/packages/henzeb/warmable) for more information.

#### injecting in the constructor

[](#injecting-in-the-constructor)

You can also inject dependencies in the constructor, just like as you are used to. You don't have to resolve the instance using `resolve`, because `Warmable` does that for you under the hood using `make`.

```
use Henzeb\Warmable\Illuminate\Warmable;

class HeavyOperation extends Warmable {
    public function __construct(
    private YourService $yourService
    private YourOtherService $otherService
    ) {
    }
}

HeavyOperation::make();

HeavyOperation::make(['yourService' => new YourService()]);
```

### get

[](#get)

The get method behaves just like its vanilla counterpart, except that when the cache item does not exist yet, it will dispatch itself as a job to the queue, And then immediately returns whatever is set as default. As you are used to, it accepts callables as default value.

Note: If the `connection` variable is set to `sync`, it will create a cache right away and returns its result. When a default is set, it will do that after returning it's response to the browser.

### Scheduling &amp; Queueing

[](#scheduling--queueing)

A `Warmable` is out of the box a `Job` implementing the `ShouldQueue` interface. This makes it very easy to schedule the `Warmable` and dispatch it to the queue.

The `Warmable` is invokable, allowing you to just throw in the FQCN and never look back.

```
$schedule->call(HeavyOperation::class)->daily(); // happens during execution
$schedule->job(HeavyOperation::class)->hourly(); // dispatches to the queue
```

Note: It also implements the `ShouldBeUnique` interface, this makes sure It won't run it twice at the same time. As `Warmable` is just a job, you can configure this further. See the Laravel documentation for more information.

#### Ensuring the cache is warmed up

[](#ensuring-the-cache-is-warmed-up)

Both will warm up the cache at the scheduled time. But if you need to make sure That the cache is warmed up as soon as the cache is expired or gone, you can utilize `withPreheating`, which will, depending on your configuration, dispatch a job or call immediately, even if the specified interval hasn't been met.

```
$schedule->call(HeavyOperation::withPreheating())->daily();

// Will dispatch a job on the queue when preheating needs to happen
$schedule->job(HeavyOperation::withPreheating(true))->hourly();
```

Note: The beauty in all this is that whenever a `Warmable` has preheated, it won't queue or execute anything. So it won't do the preheating and then do it again.

### preheat

[](#preheat)

A special method that is very useful when data changes sometimes and the cache needs to reflect those changes as soon as possible. It dispatches the `Warmable` as a job to the queue. This allows the user to move on and not having to wait for the save operation to be completed.

The method returns an instance of `PendingDispatch`, allowing you to modify configuration before actually getting dispatched.

```
HeavyOperation::preheat()->onQueue('sync');
```

### Events

[](#events)

In total, `Warmable` emits 4 different events. The names should be self-explanatory about when they are emitted.

eventHenzeb\\Warmable\\Illuminate\\Support\\Events\\CachePreheatingHenzeb\\Warmable\\Illuminate\\Support\\Events\\CacheWarmingUpHenzeb\\Warmable\\Illuminate\\Support\\Events\\CacheFailedWarmingUpHenzeb\\Warmable\\Illuminate\\Support\\Events\\CacheWarmedUpEach event receives the `key` and the `warmable` as FQCN. Except for `CacheFailedWarmingUp`, which receives the actual instance for debugging purposes.

`CacheWarmedUp` also receives the `result`. That's what in the cache.

#### Using custom events

[](#using-custom-events)

Each event can be changed by using instance variables. The custom events are emitted by this `Warmable` only. This allows you to only have to listen to events of that particular `Warmable`and not having to implement multiple `if` or `match` statements inside a listener.

```
use Henzeb\Warmable\Illuminate\Warmable;

class YourEvent {
    public function __construct(
        public string $key, // not required, the key of your Warmable
        public string $warmable // not required, FQCN of your Warmable
        public mixed $result = null // not required, only for CacheWarmedUp events
    ) {
    }
}

class HeavyOperation extends Warmable {
    protected string $preheatingEvent = YourEvent::class;

    protected string $warmingUpEvent = YourEvent::class;

    protected string $failedWarmingUpEvent = YourEvent::class;

    protected string $warmedUpEvent = YourEvent::class;
}
```

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Henze Berkheij](https://github.com/henzeb)

License
-------

[](#license)

The GNU AGPLv. Please see [License File](LICENSE.md) for more information.

###  Health Score

16

—

LowBetter than 4% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

864d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15928532?v=4)[henzeb](/maintainers/henzeb)[@henzeb](https://github.com/henzeb)

---

Top Contributors

[![henzeb](https://avatars.githubusercontent.com/u/15928532?v=4)](https://github.com/henzeb "henzeb (2 commits)")

---

Tags

laravelperformancecacheilluminatewarmupspeedhenzebspeedupUpwarmerwarmpreheatwarmableheat

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/henzeb-warmable-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/henzeb-warmable-laravel/health.svg)](https://phpackages.com/packages/henzeb-warmable-laravel)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k9.0M69](/packages/spatie-laravel-responsecache)[anahkiasen/flatten

A package for the Illuminate framework that flattens pages to plain HTML

33313.0k](/packages/anahkiasen-flatten)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

13314.7k](/packages/nexxai-laravel-cfcache)[henzeb/laravel-cache-index

Flexible replacement for tags

1216.1k](/packages/henzeb-laravel-cache-index)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
