CacheableAll::invalidateCache()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 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