From 1d5fa824f41a5a275125f4fad98665a6e7f74a5f Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:08:46 +0000 Subject: [PATCH 1/6] get instance from function --- src/CachedModel.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index 11b32f4..f740386 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -30,22 +30,19 @@ public static function boot() { parent::boot(); - $class = get_called_class(); - $instance = new $class; - - static::created(function () use ($instance) { + static::created(function ($instance) { $instance->flushCache(); }); - static::deleted(function () use ($instance) { + static::deleted(function ($instance) { $instance->flushCache(); }); - static::saved(function () use ($instance) { + static::saved(function ($instance) { $instance->flushCache(); }); - static::updated(function () use ($instance) { + static::updated(function ($instance) { $instance->flushCache(); }); } From 7533fbef45f4472ee747dbeab1c68a8490cc8d0d Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:09:46 +0000 Subject: [PATCH 2/6] only saved is needed - the other methods fire saved event --- src/CachedModel.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index f740386..c5676bf 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -30,21 +30,9 @@ public static function boot() { parent::boot(); - static::created(function ($instance) { - $instance->flushCache(); - }); - - static::deleted(function ($instance) { - $instance->flushCache(); - }); - static::saved(function ($instance) { $instance->flushCache(); }); - - static::updated(function ($instance) { - $instance->flushCache(); - }); } public static function all($columns = ['*']) From db51a86cff39865d906d22370781412053f94a81 Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:10:47 +0000 Subject: [PATCH 3/6] move boot method into trait --- src/CachedModel.php | 9 --------- src/Traits/Cachable.php | 7 +++++++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index c5676bf..7f87164 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -26,15 +26,6 @@ public function newEloquentBuilder($query) return new Builder($query); } - public static function boot() - { - parent::boot(); - - static::saved(function ($instance) { - $instance->flushCache(); - }); - } - public static function all($columns = ['*']) { $class = get_called_class(); diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index 3e16c37..064c6a8 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -60,4 +60,11 @@ protected function makeCacheTags() : array return (new CacheTags($this->eagerLoad, $this->model)) ->make(); } + + public static function bootCachable() + { + static::saved(function ($instance) { + $instance->flushCache(); + }); + } } From a30586aaac8f64e19b70654194376314b439ceb9 Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:12:18 +0000 Subject: [PATCH 4/6] move all method into trait --- src/CachedModel.php | 13 ------------- src/Traits/Cachable.php | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index 7f87164..9bf15a6 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -25,17 +25,4 @@ public function newEloquentBuilder($query) return new Builder($query); } - - public static function all($columns = ['*']) - { - $class = get_called_class(); - $instance = new $class; - $tags = [str_slug(get_called_class())]; - $key = $instance->makeCacheKey(); - - return $instance->cache($tags) - ->rememberForever($key, function () use ($columns) { - return parent::all($columns); - }); - } } diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index 064c6a8..a811bec 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -67,4 +67,17 @@ public static function bootCachable() $instance->flushCache(); }); } + + public static function all($columns = ['*']) + { + $class = get_called_class(); + $instance = new $class; + $tags = [str_slug(get_called_class())]; + $key = $instance->makeCacheKey(); + + return $instance->cache($tags) + ->rememberForever($key, function () use ($columns) { + return parent::all($columns); + }); + } } From be3354ac17cf0c55e3f64f2617151e791bda0a2c Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:14:52 +0000 Subject: [PATCH 5/6] move new eloquent builder method into trait --- src/CachedModel.php | 19 ------------------- src/Traits/Cachable.php | 13 +++++++++++++ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/CachedModel.php b/src/CachedModel.php index 9bf15a6..3dc0e6c 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -1,28 +1,9 @@ forget('genealabs-laravel-model-caching-is-disabled'); - - return new EloquentBuilder($query); - } - - return new Builder($query); - } } diff --git a/src/Traits/Cachable.php b/src/Traits/Cachable.php index a811bec..11c00c2 100644 --- a/src/Traits/Cachable.php +++ b/src/Traits/Cachable.php @@ -5,6 +5,8 @@ use GeneaLabs\LaravelModelCaching\CachedModel; use Illuminate\Cache\TaggableStore; use Illuminate\Database\Query\Builder; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use GeneaLabs\LaravelModelCaching\CachedBuilder; trait Cachable { @@ -80,4 +82,15 @@ public static function all($columns = ['*']) return parent::all($columns); }); } + + public function newEloquentBuilder($query) + { + if (session('genealabs-laravel-model-caching-is-disabled')) { + session()->forget('genealabs-laravel-model-caching-is-disabled'); + + return new EloquentBuilder($query); + } + + return new CachedBuilder($query); + } } From bc68fda8aecccb85053d40c02819e2ea8c43a0ca Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Sun, 11 Feb 2018 11:17:37 +0000 Subject: [PATCH 6/6] update readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a79d6e6..fdcc259 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ 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 -extends `CachedModel`, from which all your other models are extended. If you +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: @@ -57,10 +57,11 @@ Here's an example `BaseModel` class: ```php