authentik/eloquent-cache
Eloquent Cache
Easily cache your Laravel's Eloquent models.
Requirements
-
PHP >= 7.2
-
Laravel 6 / 7 / 8
Installation
Install via composer :
composer require authentik/eloquent-cache
How it works
-
When Eloquent fetches models, the JSON representations of the model instances are cached.
-
Subsequently, when eloquent fetches a model by ID, the cached JSON will be converted back into an instance.
Usage
Use the Cacheable trait in the models you want to cache.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Authentik\EloquentCache\Cacheable;
class Category extends Model
{
use Cacheable;
/*
* You can optionally override the following functions:
*/
// Time To Live in minutes (default value: 0 => no TTL)
public function getCacheTTL() {
return 60;
}
// default value: the lowercase name of the model
public function getCacheTagName() {
return 'cat';
}
// Cache busting will automatically invalidate the cache when model instances are updated or deleted.
// default value: true
public function isCacheBustingEnabled() {
return false;
}
// Whether or not to keep model instances in a static array cache
// (useful to avoid querying the cache store/building instances from json multiple times)
// default value: true
public function isStaticCacheEnabled() {
return false;
}
}
To manually cache a model instance, use the
cachemethod.
Category::find(1)->cache();
To invalidate the cache for a model instance, use the
refreshorflushmethod.
$refreshedInstance = Category::find(1)->refresh();
// or
Category::flush(Category::find(1));
To invalidate the cache for all instances of a model, use the
flushmethod.
Category::flush();
Changelog
Related Packages
A Redis based, automated and scalable database caching layer for Laravel
A Redis based, automated and scalable database caching layer for Laravel
Laravel 5 - Repositories to the database layer. Based on prettus/l5-repository
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.3.2 | >=7.2 | ^6.0 | | GPL-3.0-only | |
| 1.3.1 | >=7.2 | ^6.0 | | GPL-3.0-only | |
| 1.3.0 | >=7.2 | ^6.0 | | GPL-3.0-only | |
| 1.2.0 | >=7.2 | ^6.0 | | GPL-3.0-only | |
| 1.1.9 | >=5.6.4 | ^5.4 | | GPL-3.0-only | |
| 1.1.8 | >=5.6.4 | ^5.4 | | GPL-3.0-only | |
| 1.1.7 | >=5.6.4 | ^5.4 | | GPL-3.0-only | |
| 1.1.6 | >=5.6.4 | ^5.4 | | GPL-3.0-only | |
| 1.1.5 | >=5.6.4 | 5.4 - 5.8 | GPL-3.0-only | |
| 1.1.4 | >=5.6.4 | 5.4 - 5.8 | GPL-3.0-only | |
| 1.1.3 | >=5.6.4 | 5.4 - 5.8 | GPL-3.0-only | |
| 1.1.2 | >=7.0 | 5.4 - 5.7 | GPL-3.0-only | |
| 1.1.1 | >=7.0 | 5.4 - 5.7 | GPL-3.0-only | |
| 1.1.0 | >=7.0 | 5.4 - 5.7 | GPL-3.0-only | |
| 1.0.2 | >=7.0 | 5.5 - 5.6 | GNU GPL 3 | |
| 1.0.1 | >=7.0 | 5.5 - 5.6 | GNU GPL 3 | |
| 1.0.0 | ~7.2 | ~5.6 |