PHPackages                             knovator/laravel-model-caching - 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. knovator/laravel-model-caching

ActiveLibrary[Caching](/categories/caching)

knovator/laravel-model-caching
==============================

Automatic caching for Eloquent models.

v1.0.3(6y ago)04MITPHPPHP &gt;=7.2.5

Since Apr 11Pushed 4y ago2 watchersCompare

[ Source](https://github.com/knovator/laravel-model-caching)[ Packagist](https://packagist.org/packages/knovator/laravel-model-caching)[ GitHub Sponsors](https://github.com/mikebronner)[ RSS](/packages/knovator-laravel-model-caching/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (10)Versions (6)Used By (0)

Model Caching for Laravel
=========================

[](#model-caching-for-laravel)

[![Model Caching for Laravel masthead image](https://repository-images.githubusercontent.com/103836049/b0d89480-f1b1-11e9-8e13-a7055f008fe6)](https://repository-images.githubusercontent.com/103836049/b0d89480-f1b1-11e9-8e13-a7055f008fe6)

Supporting This Package
-----------------------

[](#supporting-this-package)

This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider [becoming a sponsor](https://github.com/sponsors/mikebronner).

Impetus
-------

[](#impetus)

I created this package in response to a client project that had complex, nested forms with many ``'s that resulted in over 700 database queries on one page. I needed a package that abstracted the caching process out of the model for me, and one that would let me cache custom queries, as well as cache model relationships. This package is an attempt to address those requirements.

Features
--------

[](#features)

- automatic, self-invalidating relationship (only eager-loading) caching.
- automatic, self-invalidating model query caching.
- automatic use of cache tags for cache providers that support them (will flush entire cache for providers that don't).

Cache Drivers
-------------

[](#cache-drivers)

This package is best suited for taggable cache drivers:

```
+ Redis
+ MemCached
+ APC
+ Array
```

It will not work with non-taggable drivers:

```
- Database
- File
- DynamoDB
```

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

[](#requirements)

- Laravel 6.0+ ```
    - Please note that prior Laravel versions are not supported and the package
    - versions that are compatible with prior versions of Laravel contain bugs.
    - Please only use with the versions of Laravel noted above to be compatible.
    ```

### Possible Conflicting Packages

[](#possible-conflicting-packages)

Any packages that override `newEloquentModel()` from the `Model` class will likely conflict with this package. Of course, any packages that implement their own Querybuilder class effectively circumvent this package, rendering them incompatible.

The following are packages we have identified as incompatible:

- [grimzy/laravel-mysql-spatial](https://github.com/grimzy/laravel-mysql-spatial)
- [fico7489/laravel-pivot](https://github.com/fico7489/laravel-pivot)
- [chelout/laravel-relationship-events](https://github.com/chelout/laravel-relationship-events)
- [spatie/laravel-query-builder](https://github.com/spatie/laravel-query-builder)
- [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable)

### Things That Don't Work Currently

[](#things-that-dont-work-currently)

The following items currently do no work with this package:

```
- caching of lazy-loaded relationships, see #127. Currently lazy-loaded belongs-to relationships are cached. Caching of other relationships is in the works.
- using select() clauses in Eloquent queries, see #238 (work-around discussed in the issue)
- using transactions. If you are using transactions, you will likely have to manually flush the cache, see [issue #305](https://github.com/knovator/laravel-model-caching/issues/305).
```

[![installation guide cover](https://user-images.githubusercontent.com/1791050/36356190-fc1982b2-14a2-11e8-85ed-06f8e3b57ae8.png)](https://vimeo.com/256318402)

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

[](#installation)

Be sure to not require a specific version of this package when requiring it:

```
composer require knovator/laravel-model-caching:*

```

Upgrade Notes
-------------

[](#upgrade-notes)

### 0.6.0

[](#060)

The environment and config variables for disabling this package have changed.

- If you have previously published the config file, publish it again, and adjust as necessary:

    ```
    php artisan modelCache:publish --config
    ```
- If you have disabled the package in your .env file, change the entry from `MODEL_CACHE_DISABLED=true` to `MODEL_CACHE_ENABLED=false`.

### 0.5.0

[](#050)

The following implementations have changed (see the respective sections below):

- model-specific cache prefix

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

[](#configuration)

### Recommended (Optional) Custom Cache Store

[](#recommended-optional-custom-cache-store)

If you would like to use a different cache store than the default one used by your Laravel application, you may do so by setting the `MODEL_CACHE_STORE`environment variable in your `.env` file to the name of a cache store configured in `config/cache.php` (you can define any custom cache store based on your specific needs there). For example:

```
MODEL_CACHE_STORE=redis2

```

Usage
-----

[](#usage)

For best performance a taggable cache provider is recommended (redis, memcached). While this is optional, using a non-taggable cache provider will mean that the entire cache is cleared each time a model is created, saved, updated, or deleted.

For ease of maintenance, I would recommend adding a `BaseModel` model that uses `Cachable`, from which all your other models are extended. If you don't want to do that, simply extend your models directly from `CachedModel`.

Here's an example `BaseModel` class:

```
