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); |
|
|
|
|
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
|
|
|
|