Passed
Push — master ( 17add5...305685 )
by Mike
06:16 queued 03:24
created

ModelCaching::scopeDisableCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use Carbon\Carbon;
4
use GeneaLabs\LaravelModelCaching\CachedBelongsToMany;
5
use GeneaLabs\LaravelModelCaching\CachedBuilder;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
use Illuminate\Database\Eloquent\Model;
8
9
trait ModelCaching
10
{
11
    protected $cacheCooldownSeconds = 0;
12
13
    public static function all($columns = ['*'])
14
    {
15
        if (config('laravel-model-caching.disabled')) {
16
            return parent::all($columns);
17
        }
18
19
        $class = get_called_class();
20
        $instance = new $class;
21
        $tags = $instance->makeCacheTags();
22
        $key = $instance->makeCacheKey();
23
24
        return $instance->cache($tags)
25
            ->rememberForever($key, function () use ($columns) {
26
                return parent::all($columns);
27
            });
28
    }
29
30
    public static function bootCachable()
31
    {
32
        static::created(function ($instance) {
33
            $instance->checkCooldownAndFlushAfterPersisting($instance);
34
        });
35
36
        static::deleted(function ($instance) {
37
            $instance->checkCooldownAndFlushAfterPersisting($instance);
38
        });
39
40
        static::saved(function ($instance) {
41
            $instance->checkCooldownAndFlushAfterPersisting($instance);
42
        });
43
44
        // TODO: figure out how to add this listener
45
        // static::restored(function ($instance) {
46
        //     $instance->checkCooldownAndFlushAfterPersisting($instance);
47
        // });
48
49
        static::pivotAttached(function ($instance) {
50
            $instance->checkCooldownAndFlushAfterPersisting($instance);
51
        });
52
53
        static::pivotDetached(function ($instance) {
54
            $instance->checkCooldownAndFlushAfterPersisting($instance);
55
        });
56
57
        static::pivotUpdated(function ($instance) {
58
            $instance->checkCooldownAndFlushAfterPersisting($instance);
59
        });
60
    }
61
62
    public static function destroy($ids)
63
    {
64
        $class = get_called_class();
65
        $instance = new $class;
66
        $instance->flushCache();
67
68
        return parent::destroy($ids);
69
    }
70
71
    public function newEloquentBuilder($query)
72
    {
73
        if (! $this->isCachable()) {
74
            $this->isCachable = false;
75
76
            return new EloquentBuilder($query);
77
        }
78
79
        return new CachedBuilder($query);
80
    }
81
82
    protected function newBelongsToMany(
83
        EloquentBuilder $query,
84
        Model $parent,
85
        $table,
86
        $foreignPivotKey,
87
        $relatedPivotKey,
88
        $parentKey,
89
        $relatedKey,
90
        $relationName = null
91
    ) {
92
        return new CachedBelongsToMany(
93
            $query,
94
            $parent,
95
            $table,
96
            $foreignPivotKey,
97
            $relatedPivotKey,
98
            $parentKey,
99
            $relatedKey,
100
            $relationName
101
        );
102
    }
103
104
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
105
    {
106
        if ($this->isCachable()) {
107
            $query = $query->disableModelCaching();
108
        }
109
110
        return $query;
111
    }
112
113
    public function scopeWithCacheCooldownSeconds(
114
        EloquentBuilder $query,
115
        int $seconds = null
116
    ) : EloquentBuilder {
117
        if (! $seconds) {
118
            $seconds = $this->cacheCooldownSeconds;
119
        }
120
121
        $cachePrefix = $this->getCachePrefix();
122
        $modelClassName = get_class($this);
123
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
124
125
        $this->cache()
126
            ->rememberForever($cacheKey, function () use ($seconds) {
127
                return $seconds;
128
            });
129
130
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
131
        $this->cache()
132
            ->rememberForever($cacheKey, function () {
133
                return (new Carbon)->now();
134
            });
135
136
        return $query;
137
    }
138
139
    public function getcacheCooldownSecondsAttribute() : int
140
    {
141
        return $this->cacheCooldownSeconds;
142
    }
143
}
144