PHPackages                             webo3/laravel-db-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. [Database &amp; ORM](/categories/database)
4. /
5. webo3/laravel-db-cache

ActiveLibrary[Database &amp; ORM](/categories/database)

webo3/laravel-db-cache
======================

Transparent database query caching for Laravel — zero code changes, smart invalidation, multi-driver.

v1.3.0(2mo ago)01.7kMITPHPPHP ^8.1

Since Feb 7Pushed 2mo agoCompare

[ Source](https://github.com/webo3/laravel-db-cache)[ Packagist](https://packagist.org/packages/webo3/laravel-db-cache)[ RSS](/packages/webo3-laravel-db-cache/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (30)Versions (12)Used By (0)

Laravel DB Cache
================

[](#laravel-db-cache)

Transparent database query caching for Laravel — zero code changes, smart invalidation, multi-driver.

The package intercepts queries at the connection level: `SELECT` results are cached, and any mutation (`INSERT`, `UPDATE`, `DELETE`, …) automatically invalidates the cached queries that reference the same tables. Your application code doesn't change.

Features
--------

[](#features)

- **Zero-config caching** — works transparently at the database connection level
- **Smart invalidation** — mutations invalidate exactly the cached queries that touch the affected tables
- **Three drivers** — `array` (per-request), `redis` (persistent, two-tier L1/L2), `null` (no-op)
- **Multi-tenant isolation** — `setTenantContext()` namespaces cache keys per tenant, with an opt-in fail-safe
- **Safe by default** — never caches transactions, locking reads, nondeterministic queries (`NOW()`, `RAND()`, `nextval()`, …) or cursors; Redis payloads are HMAC-signed with `APP_KEY`
- **Production-ready Redis driver** — circuit breaker on outages, TTL jitter, O(1) table-based invalidation, AWS ElastiCache / Valkey compatible

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

[](#requirements)

PHP 8.1+ · Laravel 9–12 · MySQL, PostgreSQL or SQLite · Redis (optional, for the `redis` driver)

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

[](#installation)

```
composer require webo3/laravel-db-cache
```

The service provider is auto-discovered. Optionally publish the config:

```
php artisan vendor:publish --tag=db-cache-config
```

Quick start
-----------

[](#quick-start)

Add to your `.env`:

```
DB_QUERY_CACHE_ENABLED=true
DB_QUERY_CACHE_DRIVER=array
```

That's it — `SELECT`s on your default `mysql` connection are now cached for the duration of each request. For a persistent cache shared across workers, switch to the [Redis driver](https://github.com/webo3/laravel-db-cache/blob/main/docs/redis.md).

Configuration
-------------

[](#configuration)

VariableDefaultDescription`DB_QUERY_CACHE_ENABLED``false`Enable/disable query caching`DB_QUERY_CACHE_DRIVER``array`Cache driver: `array`, `redis`, or `null``DB_QUERY_CACHE_TTL``180`Cache time-to-live in seconds`DB_QUERY_CACHE_CONNECTION``mysql`Connection name(s) to cache (comma-separated for several)`DB_QUERY_CACHE_EXCLUDED_TABLES`*(empty)*Identifiers (views, etc.) that must never be cached`DB_QUERY_CACHE_LOG_ENABLED``false`Cache hit/miss/invalidation logging + stats middleware`DB_QUERY_CACHE_TENANT_REQUIRED``false`Bypass caching until `setTenantContext()` is called (multi-tenant fail-safe)The full reference (Redis connection, size limits, runtime API) lives in [docs/configuration.md](https://github.com/webo3/laravel-db-cache/blob/main/docs/configuration.md).

Artisan commands
----------------

[](#artisan-commands)

```
php artisan db-cache:clear   # clear the cache (all tenant namespaces)
php artisan db-cache:prune   # remove stale index references (redis); schedule hourly
```

Good to know
------------

[](#good-to-know)

- Reads are **eventually consistent**: a cached result can be at most `ttl` seconds stale. Keep the TTL short for write-heavy tables.
- The cache is **transaction-aware**: `SELECT`s inside a transaction are never cached, and invalidation defers to `COMMIT` (dropped on `ROLLBACK`).
- SQL **views can't be auto-invalidated** — list them in `DB_QUERY_CACHE_EXCLUDED_TABLES`.
- **Redis Cluster is not supported**; point the driver at a single Redis node/database.

Details in [docs/how-it-works.md](https://github.com/webo3/laravel-db-cache/blob/main/docs/how-it-works.md).

Documentation
-------------

[](#documentation)

GuideContents[Configuration](https://github.com/webo3/laravel-db-cache/blob/main/docs/configuration.md)Full config reference, drivers, multiple connections, excluded views, long-running workers (Octane/Horizon)[Redis driver](https://github.com/webo3/laravel-db-cache/blob/main/docs/redis.md)Connection setup, TLS/ElastiCache, predis vs phpredis, igbinary, pruning, cluster caveat[Multi-tenancy](https://github.com/webo3/laravel-db-cache/blob/main/docs/multi-tenancy.md)Tenant-scoped caching, per-driver behavior, the `tenant_required` fail-safe[Usage &amp; monitoring](https://github.com/webo3/laravel-db-cache/blob/main/docs/usage.md)Programmatic API, stats middleware, artisan commands, custom drivers[How it works](https://github.com/webo3/laravel-db-cache/blob/main/docs/how-it-works.md)Caching pipeline, query normalization, consistency model and caveatsTesting
-------

[](#testing)

```
composer install
vendor/bin/phpunit
```

Tests need a MySQL connection (copy `.env.example` to `.env`); Redis tests skip automatically when Redis is unavailable.

License
-------

[](#license)

MIT

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance86

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

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 ~12 days

Recently: every ~20 days

Total

11

Last Release

67d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a175e082beae5946d428cc9e5675c903fe5df3691351b6db3ec22eb6b2ea1ab?d=identicon)[moihuguesjoyal](/maintainers/moihuguesjoyal)

---

Top Contributors

[![huguesjoyal](https://avatars.githubusercontent.com/u/16695693?v=4)](https://github.com/huguesjoyal "huguesjoyal (17 commits)")

---

Tags

laraveldatabaseperformancemysqlsqlitepostgresqlcacheQuery Cachedb-cache

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webo3-laravel-db-cache/health.svg)

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.9M110](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k35.7M52](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)[api-platform/laravel

API Platform support for Laravel

58190.1k21](/packages/api-platform-laravel)[itpathsolutions/dbstan

Database Standardization and Analysis Tool for Laravel

503.4k](/packages/itpathsolutions-dbstan)

PHPackages © 2026

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