PHPackages                             darkghosthunter/rememberable-query - 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. [Database &amp; ORM](/categories/database)
4. /
5. darkghosthunter/rememberable-query

Abandoned → [laragear/cache-query](/?search=laragear%2Fcache-query)ArchivedLibrary[Database &amp; ORM](/categories/database)

darkghosthunter/rememberable-query
==================================

Remember a Query results in one expressive line

v3.1.1(4y ago)4819.9k↓50%92MITPHPPHP ^8.0

Since Dec 24Pushed 4y ago2 watchersCompare

[ Source](https://github.com/DarkGhostHunter/RememberableQuery)[ Packagist](https://packagist.org/packages/darkghosthunter/rememberable-query)[ Docs](https://github.com/darkghosthunter/rememberablequery)[ Fund](https://paypal.me/darkghosthunter)[ Fund](https://ko-fi.com/DarkGhostHunter)[ RSS](/packages/darkghosthunter-rememberable-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (5)Versions (12)Used By (2)

Package superseeded by [Laragear/CacheQuery](https://github.com/Laragear/CacheQuery)
====================================================================================

[](#package-superseeded-by-laragearcachequery)

---

Rememberable Queries
====================

[](#rememberable-queries)

Remember your Query results using only one method. Yes, only one.

```
Articles::latest('published_at')->take(10)->remember()->get();
```

Requirements
------------

[](#requirements)

- PHP 8.0
- Laravel 8.x

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

[](#installation)

You can install the package via composer:

```
composer require darkghosthunter/rememberable-query
```

Usage
-----

[](#usage)

Just use the `remember()` method to remember a Query result **before the execution**. That's it. The method automatically remembers the result for 60 seconds.

```
use Illuminate\Support\Facades\DB;
use App\Models\Article;

$database = DB::table('articles')->latest('published_at')->take(10)->remember()->get();

$eloquent = Article::latest('published_at')->take(10)->remember()->get();
```

The next time you call the **same** query, the result will be retrieved from the cache instead of running the SQL statement in the database, even if the result is `null` or `false`.

> The `remember()` will throw an error if you build a query instead of executing it.

### Time-to-live

[](#time-to-live)

By default, queries are remembered by 60 seconds, but you're free to use any length, `Datetime`, `DateInterval` or Carbon instance.

```
DB::table('articles')->latest('published_at')->take(10)->remember(60 * 60)->get();

Article::latest('published_at')->take(10)->remember(now()->addHour())->get();
```

### Custom Cache Key

[](#custom-cache-key)

The auto-generated cache key is an BASE64-MD5 hash of the SQL query and its bindings, which avoids any collision with other queries while keeping the cache key short.

You can use any string as you want, but is recommended to append `query|` to avoid conflicts with other cache keys in your application.

```
Article::latest('published_at')->take(10)->remember(30, 'query|latest_articles')->get();
```

Alternatively, you can use an [custom Cache Store](#custom-cache-store) for remembering queries.

### Custom Cache Store

[](#custom-cache-store)

In some scenarios, using the default cache of your application may be detrimental compared to the database performance, or may conflict with other keys. You can use any other Cache Store by setting a third parameter, or a named parameter.

```
Article::latest('published_at')->take(10)->remember(store: 'redis')->get();
```

### Cache Lock (data races)

[](#cache-lock-data-races)

On multiple processes, the Query may be executed multiple times until the first process is able to store the result in the cache, specially when these take more than 1 second. To avoid this, set the `wait` parameter with the number of seconds to hold the lock acquired.

```
Article::latest('published_at')->take(200)->remember(wait: 5)->get();
```

The first process will acquire the lock for the given seconds, execute the query and store the result. The next processes will wait until the cache data is available to retrieve the result from there.

> If you need to use this across multiple processes, use the [cache lock](https://laravel.com/docs/cache#managing-locks-across-processes) directly.

### Idempotent queries

[](#idempotent-queries)

While the reason behind remembering a Query is to cache the data retrieved from a database, you can use this to your advantage to create [idempotent](https://en.wikipedia.org/wiki/Idempotence) queries.

For example, you can make this query only execute once every day for a given user ID.

```
$key = auth()->user()->getAuthIdentifier();

Article::whereKey(54)->remember(now()->addHour(), "query|user:$key")->increment('unique_views');
```

Subsequent executions of this query won't be executed at all until the cache expires, so in the above example we have surprisingly created a "unique views" mechanic.

Operations are **NOT** commutative
----------------------------------

[](#operations-are-not-commutative)

Altering the Builder methods order will change the auto-generated cache key hash. Even if they are *visually* the same, the order of statements makes the hash completely different.

For example, given two similar queries in different parts of the application, these both will **not** share the same cached result:

```
User::whereName('Joe')->whereAge(20)->remember()->first();
// Cache key: "query|/XreUO1yaZ4BzH2W6LtBSA=="

User::whereAge(20)->whereName('Joe')->remember()->first();
// Cache key: "query|muDJevbVppCsTFcdeZBxsA=="
```

To ensure you're hitting the same cache on similar queries, use a [custom cache key](#custom-cache-key). With this, all queries using the same key will share the same cached result:

```
User::whereName('Joe')->whereAge(20)->remember(60, 'query|find_joe')->first();
User::whereAge(20)->whereName('Joe')->remember(60, 'query|find_joe')->first();
```

This will allow you to even retrieve the data outside the query, by just asking directly to the cache.

```
$joe = Cache::get('query|find_joe');
```

License
-------

[](#license)

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

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 88.6% 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 ~98 days

Recently: every ~74 days

Total

9

Last Release

1552d ago

Major Versions

v1.2.1 → v2.0.02021-04-25

v2.0.0 → v3.0.02021-08-03

PHP version history (4 changes)v1.0.0PHP ^7.2

v1.1.0PHP ^7.2.15

v1.2.0PHP ^7.4||^8.0

v3.0.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![DarkGhostHunter](https://avatars.githubusercontent.com/u/5141911?v=4)](https://github.com/DarkGhostHunter "DarkGhostHunter (39 commits)")[![pktharindu](https://avatars.githubusercontent.com/u/23132672?v=4)](https://github.com/pktharindu "pktharindu (3 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![EcoinTest](https://avatars.githubusercontent.com/u/153815470?v=4)](https://github.com/EcoinTest "EcoinTest (1 commits)")

---

Tags

laraveldatabasesqlcachedarkghosthunter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/darkghosthunter-rememberable-query/health.svg)

```
[![Health](https://phpackages.com/badges/darkghosthunter-rememberable-query/health.svg)](https://phpackages.com/packages/darkghosthunter-rememberable-query)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[eusonlito/laravel-database-cache

Cache Database Query results on Laravel Query Builder or Eloquent

194.2k](/packages/eusonlito-laravel-database-cache)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)[ytake/laravel-couchbase

Couchbase providers for Laravel

3051.9k](/packages/ytake-laravel-couchbase)[vectorial1024/laravel-cache-evict

Efficiently remove expired Laravel file/database cache data

5813.2k](/packages/vectorial1024-laravel-cache-evict)

PHPackages © 2026

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