|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/videos/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\videos\services; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use dukt\videos\Plugin as VideosPlugin; |
|
12
|
|
|
use yii\base\Component; |
|
13
|
|
|
use DateInterval; |
|
14
|
|
|
use craft\helpers\DateTimeHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Cache service. |
|
18
|
|
|
* |
|
19
|
|
|
* An instance of the Cache service is globally accessible via [[Plugin::cache `VideosPlugin::$plugin->getCache()`]]. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Dukt <[email protected]> |
|
22
|
|
|
* @since 2.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Cache extends Component |
|
25
|
|
|
{ |
|
26
|
|
|
// Public Methods |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get cache. |
|
31
|
|
|
* |
|
32
|
|
|
* @param $id |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get(array $id) |
|
37
|
|
|
{ |
|
38
|
|
|
$cacheKey = $this->getCacheKey($id); |
|
39
|
|
|
|
|
40
|
|
|
return Craft::$app->getCache()->get($cacheKey); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Set cache. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $id |
|
47
|
|
|
* @param $value |
|
48
|
|
|
* @param null $expire |
|
|
|
|
|
|
49
|
|
|
* @param null $dependency |
|
|
|
|
|
|
50
|
|
|
* @param null $enableCache |
|
|
|
|
|
|
51
|
|
|
* |
|
52
|
|
|
* @return bool|null |
|
53
|
|
|
* @throws \Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public function set($id, $value, $expire = null, $dependency = null, $enableCache = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if (null === $enableCache) { |
|
|
|
|
|
|
58
|
|
|
$enableCache = VideosPlugin::$plugin->getSettings()->cacheDuration; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($enableCache) { |
|
62
|
|
|
$cacheKey = $this->getCacheKey($id); |
|
63
|
|
|
|
|
64
|
|
|
if (!$expire) { |
|
|
|
|
|
|
65
|
|
|
$duration = VideosPlugin::$plugin->getSettings()->cacheDuration; |
|
66
|
|
|
$interval = new DateInterval($duration); |
|
67
|
|
|
$expire = DateTimeHelper::intervalToSeconds($interval); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return Craft::$app->cache->set($cacheKey, $value, $expire, $dependency); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Private Methods |
|
77
|
|
|
// ========================================================================= |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return the cache key |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $request |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getCacheKey(array $request): string |
|
87
|
|
|
{ |
|
88
|
|
|
unset($request['CRAFT_CSRF_TOKEN']); |
|
89
|
|
|
|
|
90
|
|
|
$hash = md5(serialize($request)); |
|
91
|
|
|
|
|
92
|
|
|
return 'videos.' . $hash; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|