Passed
Push — master ( 7bc374...cf39f3 )
by Arthur
05:05
created

ModelCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 108
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 3 1
A clearAll() 0 4 1
A findOrRequery() 0 4 1
A store() 0 3 1
A remove() 0 3 1
A getCacheTime() 0 3 1
A clearModel() 0 4 1
A getCacheConnection() 0 7 2
A getCacheName() 0 3 1
A findWithoutRequery() 0 3 1
A deleteWithPrefix() 0 6 1
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 30
    public static function findOrRequery($id, $modelClass)
29
    {
30
        return Cache::remember(self::getCacheName($id, $modelClass), self::getCacheTime(), function () use ($id, $modelClass) {
31 30
            return $modelClass::findWithoutCache($id);
32 30
        });
33
    }
34
35
    /**
36
     * @param string
37
     */
38 30
    public static function getCacheName($id, $modelClass)
39
    {
40 30
        return config('model.cache_prefix').':'.strtolower(get_short_class_name($modelClass)).':'.$id;
41
    }
42
43
    /**
44
     * @return \Illuminate\Config\Repository|mixed
45
     */
46 30
    private static function getCacheTime()
47
    {
48 30
        return config('model.cache_time');
49
    }
50
51
    /**
52
     * @param $id
53
     * @param string $modelClass
54
     *
55
     * @return mixed
56
     */
57 2
    public static function findWithoutRequery($id, $modelClass)
58
    {
59 2
        return Cache::get(self::getCacheName($id, $modelClass));
60
    }
61
62
    /**
63
     * @param \Eloquent $model
64
     */
65 30
    public static function store($model)
66
    {
67 30
        Cache::put(self::getCacheName($model->getKey(), $model), $model->fresh(), self::getCacheTime());
68 30
    }
69
70
    /**
71
     * @param $id
72
     * @param string $modelClass
73
     *
74
     * @return bool
75
     */
76 1
    public static function remove($id, $modelClass)
77
    {
78 1
        return Cache::forget(self::getCacheName($id, $modelClass));
79
    }
80
81 30
    public static function clearAll()
82
    {
83 30
        $pattern = config('model.cache_prefix');
84 30
        self::deleteWithPrefix($pattern);
85 30
    }
86
87
    /**
88
     * @param $prefix
89
     *
90
     * @throws Exception
91
     */
92 30
    private static function deleteWithPrefix($prefix)
93
    {
94 30
        $redis = self::getCacheConnection();
95 30
        $keyPattern = Cache::getPrefix().$prefix.'*';
96 30
        $keys = $redis->keys($keyPattern);
97 30
        $redis->delete($keys);
98 30
    }
99
100
    /**
101
     * @throws Exception
102
     *
103
     * @return \Illuminate\Redis\Connections\Connection
104
     */
105 30
    private static function getCacheConnection()
106
    {
107 30
        if (config('cache.default') === 'redis') {
108 30
            return Redis::connection('cache');
109
        }
110
111
        throw new Exception('This action is only possible with redis as cache driver');
112
    }
113
114
    /**
115
     * @param $modelClass
116
     *
117
     * @throws Exception
118
     */
119 1
    public static function clearModel($modelClass)
120
    {
121 1
        $pattern = config('model.cache_prefix').':'.strtolower(get_short_class_name($modelClass));
122 1
        self::deleteWithPrefix($pattern);
123 1
    }
124
}
125