|
1
|
|
|
<?php namespace Arcanedev\LaravelMetrics\Metrics\Concerns; |
|
2
|
|
|
|
|
3
|
|
|
use Closure; |
|
4
|
|
|
use DateInterval; |
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait HasCachedResults |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\LaravelMetrics\Metrics\Concerns |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
trait HasCachedResults |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Getters |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the Http request instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @return \Illuminate\Http\Request |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract public function getRequest(); |
|
27
|
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
|
29
|
|
|
| Main Methods |
|
30
|
|
|
| ----------------------------------------------------------------- |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Determine for how many minutes the metric should be cached. |
|
35
|
|
|
* |
|
36
|
|
|
* @return \DateTimeInterface|\DateInterval|float|int|void |
|
37
|
|
|
*/ |
|
38
|
92 |
|
public function cacheFor() |
|
39
|
|
|
{ |
|
40
|
|
|
// |
|
41
|
92 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Cache the result. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $cacheFor |
|
47
|
|
|
* @param \Closure $callback |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
8 |
|
protected function cacheResult($cacheFor, Closure $callback) |
|
52
|
|
|
{ |
|
53
|
8 |
|
if (is_numeric($cacheFor)) |
|
54
|
4 |
|
$cacheFor = new DateInterval(sprintf('PT%dS', $cacheFor * 60)); |
|
55
|
|
|
|
|
56
|
8 |
|
return Cache::remember($this->getCacheKey(), $cacheFor, $callback); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the cache's key. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
8 |
|
protected function getCacheKey(): string |
|
65
|
|
|
{ |
|
66
|
8 |
|
$request = $this->getRequest(); |
|
67
|
|
|
|
|
68
|
8 |
|
return sprintf( |
|
69
|
8 |
|
'%s.%s.%s.%s.%s', |
|
70
|
8 |
|
Str::slug($this->getCachePrefix(), '.'), |
|
71
|
8 |
|
Str::slug(str_replace('\\', '_', static::class)), |
|
72
|
8 |
|
$request->input('range', 'no-range'), |
|
73
|
8 |
|
$request->input('timezone', 'no-timezone'), |
|
74
|
8 |
|
$request->input('twelveHourTime', '24-hour-time') |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the cache's prefix. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
8 |
|
protected function getCachePrefix(): string |
|
84
|
|
|
{ |
|
85
|
8 |
|
return config('metrics.cache.prefix', 'arcanedev.metrics'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|