Passed
Pull Request — master (#246)
by
unknown
03:05
created

ModelCaching::scopeWithCacheCooldownSeconds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use Carbon\Carbon;
4
use GeneaLabs\LaravelModelCaching\CachedBuilder;
5
use GeneaLabs\LaravelModelCaching\CacheBelongsToMany;
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(EloquentBuilder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
83
                                        $parentKey, $relatedKey, $relationName = null)
84
    {
85
        return new CacheBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
86
    }
87
88
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
89
    {
90
        if ($this->isCachable()) {
91
            $query = $query->disableModelCaching();
92
        }
93
94
        return $query;
95
    }
96
97
    public function scopeWithCacheCooldownSeconds(
98
        EloquentBuilder $query,
99
        int $seconds = null
100
    ) : EloquentBuilder {
101
        if (! $seconds) {
102
            $seconds = $this->cacheCooldownSeconds;
103
        }
104
105
        $cachePrefix = $this->getCachePrefix();
106
        $modelClassName = get_class($this);
107
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
108
109
        $this->cache()
110
            ->rememberForever($cacheKey, function () use ($seconds) {
111
                return $seconds;
112
            });
113
114
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
115
        $this->cache()
116
            ->rememberForever($cacheKey, function () {
117
                return (new Carbon)->now();
118
            });
119
120
        return $query;
121
    }
122
123
    public function getcacheCooldownSecondsAttribute() : int
124
    {
125
        return $this->cacheCooldownSeconds;
126
    }
127
}
128