Passed
Push — master ( 9d31e9...b56bec )
by Zahir
04:36
created

refresh_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
use Illuminate\Support\Str;
4
5
if (!function_exists('forget_cache')) {
6
    /**
7
     * @param $key
8
     *
9
     * @throws Exception
10
     */
11
    function forget_cache($key)
12
    {
13
        // delete this query from cache
14
        cache()->forget($key);
15
    }
16
}
17
/*--------------------------------------{</>}----------------------------------------*/
18
19
if (!function_exists('get_cache')) {
20
    /**
21
     * @param $key
22
     *
23
     * @throws Exception
24
     * @return mixed
25
     */
26
    function get_cache($key)
27
    {
28
        return cache()->get($key);
29
    }
30
}
31
/*--------------------------------------{</>}----------------------------------------*/
32
33
if (!function_exists('refresh_cache')) {
34
    /**
35
     * @param $key
36
     * @param $query
37
     *
38
     * @throws Exception
39
     * @return bool
40
     */
41
    function refresh_cache($key, $query)
42
    {
43
        // delete this query from cache
44
        cache()->forget($key);
45
46
        // caching again
47
        cache()->rememberForever($key, function () use ($query) {
48
            return $query;
49
        });
50
51
        return true;
52
    }
53
}
54
/*--------------------------------------{</>}----------------------------------------*/
55
56
if (!function_exists('get_from_cache_by_slug')) {
57
    /**
58
     * @param        $slug
59
     * @param string $model
60
     *
61
     * @throws \Psr\SimpleCache\InvalidArgumentException
62
     * @return mixed
63
     */
64
    function get_from_cache_by_slug($slug, $model = "App\\Page")
65
    {
66
67
        $key = getCacheKey($slug, $model);
0 ignored issues
show
Bug introduced by
The function getCacheKey was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $key = /** @scrutinizer ignore-call */ getCacheKey($slug, $model);
Loading history...
68
        // caching project if not existing
69
        if (!cache()->has($key)) {
70
            cache()->rememberForever($key, function () use ($model, $slug) {
71
                return $model::active()->where('slug', $slug)->firstOrFail();
72
            });
73
        }
74
75
        // get page from cache
76
        return get_cache($key);
77
    }
78
}
79
/*--------------------------------------{</>}----------------------------------------*/
80
81
if (!function_exists('get_cache_key')) {
82
    /**
83
     * @param $slug
84
     * @param $className
85
     *
86
     * @return mixed
87
     */
88
    function get_cache_key($slug, $className = null)
89
    {
90
        $name = get_model_name_by_class($className);
91
92
        return "{$name}_{$slug}"; // page_slug
93
    }
94
}
95
/*--------------------------------------{</>}----------------------------------------*/
96
97
if (!function_exists('get_model_name_by_class')) {
98
    /**
99
     * @param        $className
100
     * @param string $namespace
101
     *
102
     * @return mixed
103
     */
104
    function get_model_name_by_class($className = null, $namespace = 'App\\')
105
    {
106
        $name = str_replace($namespace, '', $className);
107
        $name = Str::lower($name);
108
109
        return $name;
110
    }
111
}
112
/*--------------------------------------{</>}----------------------------------------*/
113