Skip to content

Commit 1777bb5

Browse files
authored
Merge pull request #65 from RMoorePHP/master
Move the logic into a trait, so it is accessible without extending the provided base model (eg for projects which already have a base model)
2 parents 3a40713 + bc68fda commit 1777bb5

File tree

3 files changed

+37
-59
lines changed

3 files changed

+37
-59
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ mean that the entire cache is cleared each time a model is created, saved,
4949
updated, or deleted.
5050

5151
For ease of maintenance, I would recommend adding a `BaseModel` model that
52-
extends `CachedModel`, from which all your other models are extended. If you
52+
uses `Cachable`, from which all your other models are extended. If you
5353
don't want to do that, simply extend your models directly from `CachedModel`.
5454

5555
Here's an example `BaseModel` class:
5656

5757
```php
5858
<?php namespace App;
5959

60-
use GeneaLabs\LaravelModelCaching\CachedModel;
60+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
6161

62-
abstract class BaseModel extends CachedModel
62+
abstract class BaseModel
6363
{
64+
use Cachable;
6465
//
6566
}
6667
```

src/CachedModel.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,9 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3-
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
43
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
5-
use Illuminate\Cache\CacheManager;
6-
use Illuminate\Cache\TaggableStore;
7-
use Illuminate\Cache\TaggedCache;
8-
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
94
use Illuminate\Database\Eloquent\Model;
10-
use Illuminate\Database\Eloquent\Relations\Relation;
11-
use Illuminate\Support\Collection;
12-
use LogicException;
135

146
abstract class CachedModel extends Model
157
{
168
use Cachable;
17-
18-
public function newEloquentBuilder($query)
19-
{
20-
if (session('genealabs-laravel-model-caching-is-disabled')) {
21-
session()->forget('genealabs-laravel-model-caching-is-disabled');
22-
23-
return new EloquentBuilder($query);
24-
}
25-
26-
return new Builder($query);
27-
}
28-
29-
public static function boot()
30-
{
31-
parent::boot();
32-
33-
$class = get_called_class();
34-
$instance = new $class;
35-
36-
static::created(function () use ($instance) {
37-
$instance->flushCache();
38-
});
39-
40-
static::deleted(function () use ($instance) {
41-
$instance->flushCache();
42-
});
43-
44-
static::saved(function () use ($instance) {
45-
$instance->flushCache();
46-
});
47-
48-
static::updated(function () use ($instance) {
49-
$instance->flushCache();
50-
});
51-
}
52-
53-
public static function all($columns = ['*'])
54-
{
55-
$class = get_called_class();
56-
$instance = new $class;
57-
$tags = [str_slug(get_called_class())];
58-
$key = $instance->makeCacheKey();
59-
60-
return $instance->cache($tags)
61-
->rememberForever($key, function () use ($columns) {
62-
return parent::all($columns);
63-
});
64-
}
659
}

src/Traits/Cachable.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use GeneaLabs\LaravelModelCaching\CachedModel;
66
use Illuminate\Cache\TaggableStore;
77
use Illuminate\Database\Query\Builder;
8+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
9+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
810

911
trait Cachable
1012
{
@@ -60,4 +62,35 @@ protected function makeCacheTags() : array
6062
return (new CacheTags($this->eagerLoad, $this->model))
6163
->make();
6264
}
65+
66+
public static function bootCachable()
67+
{
68+
static::saved(function ($instance) {
69+
$instance->flushCache();
70+
});
71+
}
72+
73+
public static function all($columns = ['*'])
74+
{
75+
$class = get_called_class();
76+
$instance = new $class;
77+
$tags = [str_slug(get_called_class())];
78+
$key = $instance->makeCacheKey();
79+
80+
return $instance->cache($tags)
81+
->rememberForever($key, function () use ($columns) {
82+
return parent::all($columns);
83+
});
84+
}
85+
86+
public function newEloquentBuilder($query)
87+
{
88+
if (session('genealabs-laravel-model-caching-is-disabled')) {
89+
session()->forget('genealabs-laravel-model-caching-is-disabled');
90+
91+
return new EloquentBuilder($query);
92+
}
93+
94+
return new CachedBuilder($query);
95+
}
6396
}

0 commit comments

Comments
 (0)