Package Data | |
---|---|
Maintainer Username: | mikebronner |
Maintainer Contact: | hello@genealabs.com (Mike Bronner) |
Package Create Date: | 2017-09-17 |
Package Last Update: | 2024-07-28 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-21 03:01:00 |
Package Statistics | |
---|---|
Total Downloads: | 3,548,754 |
Monthly Downloads: | 76,555 |
Daily Downloads: | 3,212 |
Total Stars: | 2,268 |
Total Watchers: | 41 |
Total Forks: | 218 |
Total Open Issues: | 48 |
I created this package in response to a client project that had complex, nested
forms with many <select>
'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.
Any packages that also override newEloquentModel()
from the Model
class will
likely conflict with this package. So far these may include the following:
The following items currently do no work with this package:
- caching of lazy-loaded relationships, see #127
- using select() clauses in Eloquent queries, see #238 (work-around discussed in the issue)
- using SoftDeletes on Models, see #237
Be sure to not require a specific version of this package when requiring it:
composer require genealabs/laravel-model-caching:*
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
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:
<?php namespace App;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
abstract class BaseModel
{
use Cachable;
//
}
Thanks to @dtvmedia for suggestion this feature. This is actually a more robust solution than cache-prefixes.
Keeping keys separate for multiple database connections is automatically handled. This is especially important for multi-tenant applications, and of course any application using multiple database connections.
Thanks to @lucian-dragomir for suggesting this feature! You can use cache key prefixing to keep cache entries separate for multi-tenant applications. For this it is recommended to add the Cachable trait to a base model, then set the cache key prefix config value there.
Note that the config setting is included before the parent method is called, so that the setting is available in the parent as well. If you are developing a multi-tenant application, see the note above.
Here's is an example:
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class BaseModel extends Model
{
use Cachable;
public function __construct($attributes = [])
{
config(['laravel-model-caching.cache-prefix' => 'test-prefix']);
parent::__construct($attributes);
}
}
I would not recommend caching the user model, as it is a special case, since it
extends Illuminate\Foundation\Auth\User
. Overriding that would break functionality.
Not only that, but it probably isn't a good idea to cache the user model anyway,
since you always want to pull the most up-to-date info on it.
In some instances, you may want to add a cache invalidation cool-down period. For example you might have a busy site where comments are submitted at a high rate, and you don't want every comment submission to invalidate the cache. While I don't necessarily recommend this, you might experiment it's effectiveness.
To use it, it must be enabled in the model (or base model if you want to use it on multiple or all models):
class MyModel extends Model
{
use Cachable;
protected $cacheCooldownSeconds = 300; // 5 minutes
// ...
}
After that it can be implemented in queries:
(new Comment)
->withCacheCooldownSeconds(30) // override default cooldown seconds in model
->get();
or:
(new Comment)
->withCacheCooldownSeconds() // use default cooldown seconds in model
->get();
There are two methods by which model-caching can be disabled:
->disableCache()
in a query-by-query instance.MODEL_CACHE_DISABLED=TRUE
in your .env
file.$result = app("model-cache")->runDisabled(function () {
return (new MyModel)->get(); // or any other stuff you need to run with model-caching disabled
});
Recommendation: use option #1 in all your seeder queries to avoid pulling in
cached information when reseeding multiple times.
You can disable a given query by using disableCache()
anywhere in the query chain. For example:
$results = $myModel->disableCache()->where('field', $value)->get();
You can flush the cache of a specific model using the following artisan command:
php artisan modelCache:clear --model=App\Model
This comes in handy when manually making updates to the database. You could also trigger this after making updates to the database from sources outside your Laravel app.
That's all you need to do. All model queries and relationships are now cached!
In testing this has optimized performance on some pages up to 900%! Most often you should see somewhere around 100% performance increase.
During package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can be. My checklist for package development includes:
Please observe and respect all aspects of the included Code of Conduct https://github.com/GeneaLabs/laravel-model-caching/blob/master/CODE_OF_CONDUCT.md.
When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.
Please review the Contribution Guidelines https://github.com/GeneaLabs/laravel-model-caching/blob/master/CONTRIBUTING.md. Only PRs that meet all criterium will be accepted.
We have included the awesome symfony/thanks
composer package as a dev
dependency. Let your OS package maintainers know you appreciate them by starring
the packages you use. Simply run composer thanks after installing this package.
(And not to worry, since it's a dev-dependency it won't be installed in your
live environment.)