Eloquent model cache for Brand and Service

composer require rennokki/laravel-eloquent-query-cache
This commit is contained in:
Jérémy Munsch 2021-08-23 20:03:17 +02:00
parent cfa87b7924
commit f940d7a71d
5 changed files with 170 additions and 6 deletions

View file

@ -4,15 +4,56 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Rennokki\QueryCache\Traits\QueryCacheable;
class Brand extends Model
{
use HasFactory;
use QueryCacheable;
protected $cacheFor = 0;
protected $fillable = [
'name'
];
public function __construct(array $attributes = [])
{
$this->cacheFor = config('api.servicesDBCache') * 60;
parent::__construct($attributes);
}
/**
* Set the base cache tags that will be present
* on all queries.
*
*! This is used to flush caching
*
* @return array
*/
protected function getCacheBaseTags(): array
{
return [
'crm_brands',
];
}
protected static function booted()
{
static::created(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::updated(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::deleted(function () {
// flush Eloquent cache
self::flushQueryCache();
});
}
/**
* Service hasMany relation
*

View file

@ -8,15 +8,40 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Rennokki\QueryCache\Traits\QueryCacheable;
class Service extends Model
{
use HasFactory;
use QueryCacheable;
protected $cacheFor = 0;
protected $fillable = [
'name'
];
public function __construct(array $attributes = [])
{
$this->cacheFor = config('api.servicesDBCache') * 60;
parent::__construct($attributes);
}
/**
* Set the base cache tags that will be present
* on all queries.
*
*! This is used to flush caching
*
* @return array
*/
protected function getCacheBaseTags(): array
{
return [
'crm_services',
];
}
protected static function booted()
{
static::creating(function (self $service) {
@ -42,6 +67,18 @@ class Service extends Model
$service->api_token = Hash::make($service->api_token);
}
});
static::created(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::updated(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::deleted(function () {
// flush Eloquent cache
self::flushQueryCache();
});
}
/**
@ -89,10 +126,19 @@ class Service extends Model
*/
public static function getFromApiToken(string $apiToken): self
{
$services = static::all();
$services = static::with([
'brand' => function ($query) {
return $query->cacheTags(["service:author"]);
},
])->get();
/** @var \App\Models\Service $service */
foreach ($services as $service) {
if ($service->api_token and Hash::check($apiToken, $service->api_token)) {
return $service;
return $service->load([
'brand' => function ($query) use ($service) {
return $query->cacheTags(["service:{$service->id}:brand"]);
},
]);
}
}
throw new ModelNotFoundException('Could not find service from Api Token');
@ -108,9 +154,14 @@ class Service extends Model
public static function getFromBearerToken(string $bearerToken): self
{
$services = static::all();
/** @var \App\Models\Service $service */
foreach ($services as $service) {
if ($service->bearer_token and Hash::check($bearerToken, $service->bearer_token)) {
return $service;
return $service->load([
'brand' => function ($query) use ($service) {
return $query->cacheTags(["service:{$service->id}:brand"]);
},
]);
}
}
throw new ModelNotFoundException('Could not find service from Service Token');

View file

@ -35,7 +35,8 @@
"kwaadpepper/enum": "^2.0",
"laravel-lang/lang": "~7.0",
"laravel/framework": "^8.39",
"laravel/tinker": "^2.5"
"laravel/tinker": "^2.5",
"rennokki/laravel-eloquent-query-cache": "^3.0"
},
"require-dev": {
"facade/ignition": "^2.5",

67
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2d86c215e8970135d800e76cda338075",
"content-hash": "0df11a64ace2bcbbbc3710af5cc6868f",
"packages": [
{
"name": "arondeparon/laravel-request-sanitizer",
@ -3271,6 +3271,71 @@
],
"time": "2020-08-18T17:17:46+00:00"
},
{
"name": "rennokki/laravel-eloquent-query-cache",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://github.com/renoki-co/laravel-eloquent-query-cache.git",
"reference": "a253bb0fd23abcff13d580d90906f112716c1da0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/renoki-co/laravel-eloquent-query-cache/zipball/a253bb0fd23abcff13d580d90906f112716c1da0",
"reference": "a253bb0fd23abcff13d580d90906f112716c1da0",
"shasum": ""
},
"require": {
"illuminate/database": "^7.30|^8.23",
"illuminate/support": "^7.30|^8.23"
},
"require-dev": {
"chelout/laravel-relationship-events": "^1.4",
"laravel/legacy-factories": "^1.1",
"mockery/mockery": "^1.4",
"orchestra/database": "^5.0|^6.0",
"orchestra/testbench": "^5.0|^6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Rennokki\\QueryCache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Alex Renoki",
"email": "alex@renoki.org",
"homepage": "https://github.com/rennokki",
"role": "Developer"
}
],
"description": "Adding cache on your Laravel Eloquent queries' results is now a breeze.",
"homepage": "https://github.com/renoki-co/laravel-eloquent-query-cache",
"keywords": [
"caching",
"eloquent",
"laravel",
"query",
"remember",
"sql"
],
"support": {
"issues": "https://github.com/renoki-co/laravel-eloquent-query-cache/issues",
"source": "https://github.com/renoki-co/laravel-eloquent-query-cache/tree/3.0.0"
},
"funding": [
{
"url": "https://ko-fi.com/rennokki",
"type": "ko_fi"
}
],
"time": "2021-07-17T17:42:40+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.7",

View file

@ -18,5 +18,11 @@ return [
* The number of request
* per user or ip per minutes
*/
'apiRequestRatePerMinute' => 256
'apiRequestRatePerMinute' => 256,
/**
* Service requests cache
* time in minutes
*/
'servicesDBCache' => 10
];