PHPackages                             juampi92/laravel-query-cache - 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. juampi92/laravel-query-cache

ActiveLibrary[Caching](/categories/caching)

juampi92/laravel-query-cache
============================

Provide easy interface for caching laravel queries

v1.2.0(3y ago)2517.2k3[1 PRs](https://github.com/juampi92/laravel-query-cache/pulls)MITPHPPHP &gt;=7.4

Since Mar 5Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/juampi92/laravel-query-cache)[ Packagist](https://packagist.org/packages/juampi92/laravel-query-cache)[ Docs](https://github.com/juampi92/laravel-query-cache)[ GitHub Sponsors](https://github.com/Juampi92)[ RSS](/packages/juampi92-laravel-query-cache/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (8)Versions (5)Used By (0)

Laravel Query Cache
===================

[](#laravel-query-cache)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ee5d542f73eeeb61bf07d6167a9041fe68fdcbf4c42b6f92d62d5c57de01b3e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a75616d706939322f6c61726176656c2d71756572792d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juampi92/laravel-query-cache)[![GitHub Tests Action Status](https://camo.githubusercontent.com/4466596546c416f62b0f951181c5ab8e2bf34e7118f4829f2e2a98c116382a5f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a75616d706939322f6c61726176656c2d71756572792d63616368652f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/juampi92/laravel-query-cache/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a1a6f307895201ba3c32573506fc2a41f4e01ef0a7a631359f6a5095f2168557/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a75616d706939322f6c61726176656c2d71756572792d63616368652f7068702d63732d66697865722e796d6c3f6272616e63683d6d6173746572266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/juampi92/laravel-query-cache/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/8a562fe1a28fe787749424ce971a10dae94efd36af8b7080e43b028ebd4b3f0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a75616d706939322f6c61726176656c2d71756572792d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juampi92/laravel-query-cache)

This package provides a set of macros to cache your Laravel Queries just like Cache::remember.

```
$featuredPost = Post::published()->orderByMostViews()
    ->cacheDay('post:featured') // first();
```

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

[](#installation)

You can install the package via composer:

```
composer require juampi92/laravel-query-cache
```

That's it! No config or Trait necessary. The package auto-discovery will boot the macros.

Usage
-----

[](#usage)

Instead of doing:

```
Cache::remember('post:count', $ttl, () => Post::published()->count());
```

You now do:

```
Post::published()->cache('post:count', $ttl)->count();
```

You can use it in Eloquent Queries as well as in normal Queries.

```
DB::table('posts')
    ->whereNotNull('published_at')
    ->latest()
    ->cacheHour('post:latest')
    ->first();

Posts::published()
    ->cacheHour('post:count')
    ->count();
```

List of macros:

```
Post::cache('cache:key', $ttl)->get();
Post::cacheMinute('cache:key')->first();
Post::cacheHour('cache:key')->pluck('id');
Post::cacheDay("cache:key:$id")->find($id);
Post::cacheWeek('cache:key:paginate:10')->paginate(10);
Post::cacheForever('cache:key')->count();
```

Advanced usage
--------------

[](#advanced-usage)

### Different store

[](#different-store)

```
Post::query()
    ->where(...)
    ->cache('post:count')->store('redis')
    ->count();
```

### Add your custom cache duration

[](#add-your-custom-cache-duration)

This is maybe more advanced, but you can do so by [opting out of discovery](https://laravel.com/docs/8.x/packages#opting-out-of-package-discovery), and then importing it yourself:

```
use Juampi92\LaravelQueryCache\LaravelQueryCacheServiceProvider as BaseServiceProvider;

class LaravelQueryCacheServiceProvider extends BaseServiceProvider
{
    protected function getCustomTimes(): array
    {
        return array_merge(
            parent::getCustomTimes(),
            [
                'rememberForever' => null,
                'cacheFifteenMinutes' => 60 * 15,
            ]
        );
    }
}
```

The original method has

```
[
    'cacheForever' => null,
    'cacheMinute' => 60,
    'cacheHour' => 60 * 60,
    'cacheDay' => 60 * 60 * 24,
    'cacheWeek' => 60 * 60 * 24 * 7,
]
```

Disclaimer
----------

[](#disclaimer)

This package is supposed to be a nice integration of Cache remember inside the Query Builder. If you're looking for a advanced eloquent specific cache, I recommend to check out [laravel-eloquent-query-cache](https://github.com/renoki-co/laravel-eloquent-query-cache).

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Juampi92](https://github.com/juampi92)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

37

—

LowBetter than 82% of packages

Maintenance40

Moderate activity, may be stable

Popularity31

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

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

Every ~384 days

Total

3

Last Release

1123d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f8d31c9bb6d7862e21aac7af0f7b2615dc06485b4c30b19b8d9fdffaa42c712?d=identicon)[juampi92](/maintainers/juampi92)

---

Top Contributors

[![juampi92](https://avatars.githubusercontent.com/u/2080215?v=4)](https://github.com/juampi92 "juampi92 (24 commits)")

---

Tags

cacheeloquentlaravellaravelcachequeryrememberqueries

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/juampi92-laravel-query-cache/health.svg)

```
[![Health](https://phpackages.com/badges/juampi92-laravel-query-cache/health.svg)](https://phpackages.com/packages/juampi92-laravel-query-cache)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[dragon-code/laravel-cache

An improved interface for working with cache

6844.8k9](/packages/dragon-code-laravel-cache)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

754.3k2](/packages/omaralalwi-lexi-translate)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)[suitmedia/laravel-cacheable

Decorate your repositories and make them cacheable

1237.7k](/packages/suitmedia-laravel-cacheable)[michele-angioni/support

Support is a Laravel package which promotes the use of best practices and design patterns.

181.4k1](/packages/michele-angioni-support)

PHPackages © 2026

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