Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

ModelCache::findOrRequery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 11.10.18
6
 * Time: 14:11.
7
 */
8
9
namespace Foundation\Cache;
10
11
use Cache;
12
use Foundation\Exceptions\Exception;
13
use Illuminate\Support\Facades\Redis;
14
15
class ModelCache
16
{
17
    /**
18
     * @param $id
19
     * @param string $modelClass
20
     *
21
     * @return mixed
22
     */
23 3
    public static function find($id, $modelClass)
24
    {
25 3
        return Cache::get(self::getCacheName($id, $modelClass));
26
27
    }
28
29
    public static function findOrRequery($id, $modelClass)
30
    {
31
        return Cache::remember(self::getCacheName($id, $modelClass), self::getCacheTime(),function () use ($id, $modelClass) {
32
            return $modelClass::findWithoutCache($id);
33
        });
34
    }
35
36
    /**
37
     * @param string
38
     */
39 19
    public static function getCacheName($id, $modelClass)
40
    {
41 19
        return config('model.cache_prefix') . ':' . strtolower(get_short_class_name($modelClass)) . ':' . $id;
42
    }
43
44
    /**
45
     * @return \Illuminate\Config\Repository|mixed
46
     */
47 19
    private static function getCacheTime()
48
    {
49 19
        return config('model.cache_time');
50
    }
51
52
    /**
53
     * @param $id
54
     * @param string $modelClass
55
     *
56
     * @return mixed
57
     */
58 2
    public static function findWithoutRequery($id, $modelClass)
59
    {
60 2
        return Cache::get(self::getCacheName($id, $modelClass));
61
    }
62
63
    /**
64
     * @param \Eloquent $model
65
     */
66 19
    public static function store($model)
67
    {
68 19
        Cache::put(self::getCacheName($model->getKey(), $model), $model->fresh(), self::getCacheTime());
69 19
    }
70
71
    /**
72
     * @param $id
73
     * @param string $modelClass
74
     *
75
     * @return bool
76
     */
77
    public static function remove($id, $modelClass)
78
    {
79
        return Cache::forget(self::getCacheName($id, $modelClass));
80
    }
81
82 1
    public static function clearAll()
83
    {
84 1
        $pattern = config('model.cache_prefix');
85 1
        self::deleteWithPrefix($pattern);
86 1
    }
87
88
    /**
89
     * @param $prefix
90
     *
91
     * @throws Exception
92
     */
93 2
    private static function deleteWithPrefix($prefix)
94
    {
95 2
        $redis = self::getCacheConnection();
96 2
        $keyPattern = Cache::getPrefix() . $prefix . '*';
97 2
        $keys = $redis->keys($keyPattern);
98 2
        $redis->delete($keys);
99 2
    }
100
101
    /**
102
     * @throws Exception
103
     *
104
     * @return \Illuminate\Redis\Connections\Connection
105
     */
106 2
    private static function getCacheConnection()
107
    {
108 2
        if (config('cache.default') === 'redis') {
109 2
            return Redis::connection('cache');
110
        }
111
112
        throw new Exception('This action is only possible with redis as cache driver');
113
    }
114
115
    /**
116
     * @param $modelClass
117
     *
118
     * @throws Exception
119
     */
120 1
    public static function clearModel($modelClass)
121
    {
122 1
        $pattern = config('model.cache_prefix') . ':' . strtolower(get_short_class_name($modelClass));
123 1
        self::deleteWithPrefix($pattern);
124 1
    }
125
}
126