From f940d7a71d47fd73345bde798170eed81576c30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 23 Aug 2021 20:03:17 +0200 Subject: [PATCH] Eloquent model cache for Brand and Service composer require rennokki/laravel-eloquent-query-cache --- app/Models/Brand.php | 41 ++++++++++++++++++++++++++ app/Models/Service.php | 57 +++++++++++++++++++++++++++++++++-- composer.json | 3 +- composer.lock | 67 +++++++++++++++++++++++++++++++++++++++++- config/api.php | 8 ++++- 5 files changed, 170 insertions(+), 6 deletions(-) diff --git a/app/Models/Brand.php b/app/Models/Brand.php index a628480..d80fae1 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -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 * diff --git a/app/Models/Service.php b/app/Models/Service.php index 2efe59f..d970710 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -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'); diff --git a/composer.json b/composer.json index 598eb39..b6b8b65 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 00d42ba..39cc9e3 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/api.php b/config/api.php index 1f6a224..c8d1914 100644 --- a/config/api.php +++ b/config/api.php @@ -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 ];