|
1 | 1 | <?php namespace GeneaLabs\LaravelModelCaching;
|
2 | 2 |
|
3 |
| -use Closure; |
4 |
| -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
| 3 | +use Illuminate\Cache\CacheManager; |
| 4 | +use Illuminate\Cache\TaggableStore; |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Database\Eloquent\Relations\Relation; |
| 7 | +use LogicException; |
5 | 8 |
|
6 |
| -class Builder extends EloquentBuilder |
| 9 | +abstract class CachedModel extends Model |
7 | 10 | {
|
8 |
| - protected function eagerLoadRelation(array $models, $name, Closure $constraints) |
| 11 | + protected function getRelationshipFromMethod($method) |
9 | 12 | {
|
10 |
| - $relation = $this->getRelation($name); |
11 |
| - $relation->addEagerConstraints($models); |
12 |
| - $constraints($relation); |
13 |
| - |
14 |
| - $parentName = str_slug(get_class($relation->getParent())); |
15 |
| - $childName = str_slug(get_class($relation->getModel())); |
16 |
| - $results = cache()->tags([$parentName, $childName]) |
17 |
| - ->rememberForever("{$parentName}-{$childName}-relation", function () use ($relation) { |
18 |
| - return $relation->getEager(); |
| 13 | + $relation = $this->$method(); |
| 14 | + |
| 15 | + if (! $relation instanceof Relation) { |
| 16 | + throw new LogicException(get_class($this).'::'.$method.' must return a relationship instance.'); |
| 17 | + } |
| 18 | + |
| 19 | + $results = $this->cache([$method]) |
| 20 | + ->rememberForever(str_slug(get_called_class()) . "-{$method}", function () use ($relation) { |
| 21 | + return $relation->getResults(); |
19 | 22 | });
|
20 | 23 |
|
21 |
| - return $relation->match( |
22 |
| - $relation->initRelation($models, $name), |
23 |
| - $results, |
24 |
| - $name |
25 |
| - ); |
| 24 | + return tap($results, function ($results) use ($method) { |
| 25 | + $this->setRelation($method, $results); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + public function newEloquentBuilder($query) |
| 30 | + { |
| 31 | + return new Builder($query); |
| 32 | + } |
| 33 | + |
| 34 | + public static function boot() |
| 35 | + { |
| 36 | + parent::boot(); |
| 37 | + |
| 38 | + static::created(function () { |
| 39 | + self::flushCache(); |
| 40 | + }); |
| 41 | + |
| 42 | + static::deleted(function () { |
| 43 | + self::flushCache(); |
| 44 | + }); |
| 45 | + |
| 46 | + static::saved(function () { |
| 47 | + self::flushCache(); |
| 48 | + }); |
| 49 | + |
| 50 | + static::updated(function () { |
| 51 | + self::flushCache(); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public function cache(array $tags = []) |
| 56 | + { |
| 57 | + $cache = cache(); |
| 58 | + |
| 59 | + if (is_subclass_of(cache()->getStore(), TaggableStore::class)) { |
| 60 | + array_push($tags, str_slug(get_called_class())); |
| 61 | + $cache = $cache->tags($tags); |
| 62 | + } |
| 63 | + |
| 64 | + return $cache; |
| 65 | + } |
| 66 | + |
| 67 | + public static function flushCache() |
| 68 | + { |
| 69 | + cache()->tags([str_slug(get_called_class())]) |
| 70 | + ->flush(); |
26 | 71 | }
|
27 | 72 | }
|
0 commit comments