CacheableAll   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidateCache() 0 3 2
A all() 0 6 1
1
<?php
2
3
namespace Sfneal\Models\Traits;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Support\Facades\Cache;
7
use Sfneal\Helpers\Laravel\LaravelHelpers;
8
use Sfneal\Helpers\Redis\RedisCache;
9
10
trait CacheableAll
11
{
12
    /**
13
     * Retrieve a Collection of all instances of this model.
14
     *
15
     * @param  array  $columns
16
     * @return Collection|mixed
17
     */
18
    public static function all($columns = ['*'])
19
    {
20
        // todo: fix use of serializeHash() function
21
        return Cache::rememberForever(parent::getTableName().':all:'.LaravelHelpers::serializeHash($columns),
22
            function () use ($columns) {
23
                return parent::all($columns);
24
            }
25
        );
26
    }
27
28
    /**
29
     * Invalidate a Model's cache.
30
     *
31
     * @param  string|null  $key
32
     * @return array
33
     */
34
    public static function invalidateCache(string $key = null)
35
    {
36
        return RedisCache::delete(parent::getTableName().(isset($key) ? ":{$key}" : ''));
37
    }
38
}
39