Repolode
mike-bronner

mike-bronner/laravel-model-caching

A Laravel package that automatically caches Eloquent model and relationship queries and invalidates them when models change.

Stars
2,378
Forks
231
Open issues
2
Last commit
4 days ago

What is it?

Model Caching for Laravel adds a Cachable trait to Eloquent models so that query results, eager-loaded relationships and aggregations are stored in the application's cache without manual cache keys. When a model is created, updated or deleted, the related cache entries are flushed automatically, and pivot changes on belongsToMany relations are handled too.

The package works with Redis, Memcached, APC and DynamoDB cache stores, and offers options such as a global or per-model TTL, a cool-down window that debounces invalidation on write-heavy models, a fallback to the database when the cache is unavailable, and per-tenant cache prefixes. Queries using inRandomOrder() or row locks are not cached, and a related model without the trait prevents its parent query from being cached.

It requires PHP 8.3+ and Laravel 12 or 13.

Key features

  • 01Adds caching to Eloquent queries, eager loads and aggregations via a trait or base model
  • 02Invalidates related cache entries automatically on create, update, delete and pivot changes
  • 03Supports Redis, Memcached, APC and DynamoDB cache stores
  • 04Offers global and per-model cache TTL settings
  • 05Provides a cool-down period to limit invalidation from frequent writes
  • 06Can fall back to direct database queries when the cache backend is down

Use cases

  • Speeding up read-heavy Laravel pages that run many repeated Eloquent queries
  • Caching model data without writing manual Cache::remember and forget logic
  • Multi-tenant Laravel apps that need per-tenant cache isolation
  • Applications using DynamoDB as a managed cache store on AWS

Quick start

  1. 1

    Install the package with Composer

    composer require mike-bronner/laravel-model-caching
  2. 2

    Add the Cachable trait to a base model that your models extend

  3. 3

    Optionally publish the config file

    php artisan modelCache:publish --config

Things to know

  • Requires PHP 8.3+ and Laravel 12 or 13.
  • The Array, File and Database cache drivers are not supported.
  • Queries that eager-load a relation whose model does not use Cachable are not cached at all.
  • DynamoDB invalidation is logical only, so stale rows linger until TTL cleanup.

Topics

FAQ

What is laravel-model-caching?
A Laravel package that automatically caches Eloquent model and relationship queries and invalidates them when models change. Model Caching for Laravel adds a Cachable trait to Eloquent models so that query results, eager-loaded relationships and aggregations are stored in the application's cache without manual cache keys. When a model is created, updated or deleted, the related cache entries are flushed automatically, and pivot changes on belongsToMany relations are handled too.
Is laravel-model-caching free to use? What license does it have?
laravel-model-caching is open source under the MIT license. Check the license terms before using it in commercial products.
Is laravel-model-caching still maintained?
Actively maintained. The last commit to laravel-model-caching was on Nov 3, 2026.
What can I use laravel-model-caching for?
Speeding up read-heavy Laravel pages that run many repeated Eloquent queries Caching model data without writing manual Cache::remember and forget logic Multi-tenant Laravel apps that need per-tenant cache isolation Applications using DynamoDB as a managed cache store on AWS
How do I install laravel-model-caching?
Install the package with Composer: composer require mike-bronner/laravel-model-caching Add the Cachable trait to a base model that your models extend Optionally publish the config file: php artisan modelCache:publish --config
Where can I get laravel-model-caching?
The source code and installation instructions are on GitHub: https://github.com/mike-bronner/laravel-model-caching

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects