PHPackages                             ymigval/laravel-model-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. ymigval/laravel-model-cache

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

ymigval/laravel-model-cache
===========================

Laravel package for caching Eloquent model queries

v1.2.0(2mo ago)7642.2k↓24%62MITPHPPHP ^7.4|^8.0|^8.1|^8.2|^8.3

Since May 1Pushed 2mo ago5 watchersCompare

[ Source](https://github.com/ymigval/laravel-model-cache)[ Packagist](https://packagist.org/packages/ymigval/laravel-model-cache)[ Docs](https://github.com/ymigval/laravel-model-cache)[ RSS](/packages/ymigval-laravel-model-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (6)Versions (8)Used By (2)

Laravel Model Cache
===================

[](#laravel-model-cache)

A simple and efficient caching solution for Laravel Eloquent models.

[![Latest Version on Packagist](https://camo.githubusercontent.com/13d24f6739201716f7ca27093128b26da308f900083cd96cd6395aec4dd20c66/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796d696776616c2f6c61726176656c2d6d6f64656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ymigval/laravel-model-cache)[![Total Downloads](https://camo.githubusercontent.com/484773e6eaf7d4b1faf0b14047a163d9a432601bc239a742902d10368c55ee8c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796d696776616c2f6c61726176656c2d6d6f64656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ymigval/laravel-model-cache)[![License](https://camo.githubusercontent.com/d0f9f8037e10a9b9767e269c283ef6f5b18b129e2f09a35893bb55f1a85fa093/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f796d696776616c2f6c61726176656c2d6d6f64656c2d63616368652e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Introduction
------------

[](#introduction)

Laravel Model Cache provides a powerful way to cache your Eloquent query results. It transparently integrates with Laravel's query builder system to automatically cache all query results, with zero changes to your existing query syntax. The package intelligently handles cache invalidation when models are created, updated, deleted, or restored.

Features
--------

[](#features)

- **Deep Integration**: Replaces Laravel's query builder with a cache-aware version
- **Transparent Caching**: All query methods (`get()`, `first()`, etc.) automatically use cache
- **Explicit Control**: Additional methods for when you want to be explicit about caching
- **Automatic Invalidation**: Cache is cleared when models are created, updated, deleted, or restored
- **Full Tag Support**: Works with Laravel's built-in cache tagging system
- **Performance Optimized**: Dramatically reduces database queries for read-heavy applications
- **Drop-in Solution**: Compatible with existing Laravel query builder syntax
- **Highly Configurable**: Easy to customize cache duration, store, and behavior

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

[](#requirements)

- PHP ^7.4, ^8.0, ^8.1, ^8.2, or ^8.3
- Laravel 8.x, 9.x, 10.x, 11.x, or 12.x

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

[](#installation)

Install the package via Composer:

```
composer require ymigval/laravel-model-cache
```

Publish the Configuration File (optional)
-----------------------------------------

[](#publish-the-configuration-file-optional)

To customize the package configuration, publish the configuration file:

```
php artisan vendor:publish --provider="YMigVal\LaravelModelCache\ModelCacheServiceProvider" --tag="config"
```

This creates with the following customizable options: `config/model-cache.php`

- `cache_duration`: Default cache TTL in minutes (default: 60)
- `cache_key_prefix`: Prefix for all cache keys (default: 'model\_cache\_')
- `cache_store`: Specific cache store to use for model caching
- `enabled`: Global toggle to enable/disable the cache functionality

Service Provider Registration
-----------------------------

[](#service-provider-registration)

The package comes with Laravel package auto-discovery for Laravel 5.5+. For older versions, register the service provider in : `config/app.php`

```
  'providers' => [
      // Other service providers...
      YMigVal\LaravelModelCache\ModelCacheServiceProvider::class,
  ],
```

Cache Driver Configuration
--------------------------

[](#cache-driver-configuration)

### Supported Cache Drivers

[](#supported-cache-drivers)

This package uses Laravel's tagging system for cache, so you must use a cache driver that supports tags:

1. **Redis** (Recommended)
    - Offers excellent performance and tag support
    - Configure in `.env`:

```
     CACHE_STORE=redis

     REDIS_CLIENT=phpredis  # or predis
     REDIS_HOST=127.0.0.1
     REDIS_PASSWORD=null
     REDIS_PORT=6379

```

2. **Memcached**
    - Another good option with tag support
    - Configure in `.env`:

```
     CACHE_STORE=memcached
     MEMCACHED_HOST=127.0.0.1
     MEMCACHED_PORT=11211

```

3. **Database**
    - Supports tags but slower than Redis/Memcached
    - Requires cache table creation:

```
     php artisan cache:table
     php artisan migrate

```

- Configure in `.env`:

```
     CACHE_STORE=database

```

4. **File** and **Array** drivers do not support tags, but the package includes fallback mechanisms that make them compatible:
    - These drivers will work for basic caching functionality
    - When using these drivers, cache invalidation will clear the entire cache rather than just specific model entries
    - While not as efficient as tag-supporting drivers, they can be used in development or when other drivers are not available
    - Configure in `.env`:

```
     CACHE_STORE=file

```

Optional Configuration in Config File
-------------------------------------

[](#optional-configuration-in-config-file)

The file allows you to adjust: `config/model-cache.php`

- Default cache Time-To-Live (TTL)
- Globally enable/disable caching
- Prefix for all cache keys
- Specific cache driver for model caching

### Cache Store Selection

[](#cache-store-selection)

You can specify which cache store to use specifically for model caching in the config file:

```
// config/model-cache.php
'cache_store' => env('MODEL_CACHE_STORE', 'redis'),
```

This allows you to use a different cache store for models than your application's default cache store.

Implementation in Models
------------------------

[](#implementation-in-models)

Add the trait to any Eloquent model you want to cache:

```
