Skip to content

Commit 72716ed

Browse files
committed
Fix builder class
1 parent 1992f44 commit 72716ed

File tree

2 files changed

+18
-64
lines changed

2 files changed

+18
-64
lines changed

src/Builder.php

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,26 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

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;
3+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
84

9-
abstract class CachedModel extends Model
5+
class Builder extends EloquentBuilder
106
{
11-
protected function getRelationshipFromMethod($method)
7+
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
128
{
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();
9+
$relation = $this->getRelation($name);
10+
$relation->addEagerConstraints($models);
11+
$constraints($relation);
12+
13+
$parentName = str_slug(get_class($relation->getParent()));
14+
$childName = str_slug(get_class($relation->getModel()));
15+
$results = cache()->tags([$parentName, $childName])
16+
->rememberForever("{$parentName}-{$childName}-relation", function () use ($relation) {
17+
return $relation->getEager();
2218
});
2319

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();
20+
return $relation->match(
21+
$relation->initRelation($models, $name),
22+
$results,
23+
$name
24+
);
7125
}
7226
}

src/CachedModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3-
use Builder;
3+
use GeneaLabs\LaravelModelCaching\Builder;
44
use Illuminate\Cache\CacheManager;
55
use Illuminate\Cache\TaggableStore;
66
use Illuminate\Database\Eloquent\Model;

0 commit comments

Comments
 (0)