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

ActiveLibrary[Caching](/categories/caching)

testingap/laravel-model-caching
===============================

Automatic caching for Eloquent models.

0.3.6(7y ago)03MITPHPPHP &gt;=7.1.3

Since Sep 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ringkubd/laravel-model-caching)[ Packagist](https://packagist.org/packages/testingap/laravel-model-caching)[ RSS](/packages/testingap-laravel-model-caching/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (18)Versions (78)Used By (0)

[![pexels-photo-325229](https://user-images.githubusercontent.com/1791050/30768358-0df9d0f2-9fbb-11e7-9f10-ad40b83bbf59.jpg)](https://user-images.githubusercontent.com/1791050/30768358-0df9d0f2-9fbb-11e7-9f10-ad40b83bbf59.jpg)

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

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

[![Gitter](https://camo.githubusercontent.com/d94ae0014b683604aa2060e74684ea3ca278209fecaaa8e87697af02911f7c65/68747470733a2f2f6261646765732e6769747465722e696d2f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672e737667)](https://gitter.im/GeneaLabs/laravel-model-caching?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge)[![Travis](https://camo.githubusercontent.com/1c3e233eae743af62c77fb7c4af366b742e13ddbef52d695d2617da30eabfb03/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672f6d61737465722e737667)](https://travis-ci.org/GeneaLabs/laravel-model-caching)[![Scrutinizer](https://camo.githubusercontent.com/75694a4307718782ddf954b78f6d082ce784ba07dfa0c074b941cc664c75f4c1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672f6d61737465722e737667)](https://scrutinizer-ci.com/g/GeneaLabs/laravel-model-caching)[![Coveralls](https://camo.githubusercontent.com/951a731610f85cbaaf8f0d923576b4b94d66ea4e39408b50a843fcbdad7a6cf8/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672f6d61737465722e737667)](https://coveralls.io/github/GeneaLabs/laravel-model-caching)[![GitHub (pre-)release](https://camo.githubusercontent.com/0f85ada192c2906d5a51ff77b69d41a9f295311093866cff6f1213b7c4fb68b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672f616c6c2e737667)](https://github.com/GeneaLabs/laravel-model-caching)[![Packagist](https://camo.githubusercontent.com/44978e2b054ec3165284b05886ef02f198abad3d18e0b36958cca1bb240c04f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672e737667)](https://packagist.org/packages/genealabs/laravel-model-caching)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/GeneaLabs/laravel-model-caching/master/LICENSE)

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).

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

[](#requirements)

- PHP &gt;= 7.1.3
- Laravel 5.4 - 5.7

### Possible Conflicting Packages

[](#possible-conflicting-packages)

Any packages that also override `newEloquentModel()` from the `Model` class will likely conflict with this package. So far these may include the following:

- [grimzy/laravel-mysql-spatial](https://github.com/grimzy/laravel-mysql-spatial)

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

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

[](#installation)

```
composer require genealabs/laravel-model-caching

```

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:

```
