1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/twitter/ |
||
4 | * @copyright Copyright (c) Dukt |
||
5 | * @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\twitter\services; |
||
9 | |||
10 | use Craft; |
||
11 | use yii\base\Component; |
||
12 | use dukt\twitter\helpers\TwitterHelper; |
||
13 | use dukt\twitter\Plugin; |
||
14 | |||
15 | /** |
||
16 | * Cache Service |
||
17 | * |
||
18 | * @author Dukt <[email protected]> |
||
19 | * @since 3.0 |
||
20 | */ |
||
21 | class Cache extends Component |
||
22 | { |
||
23 | // Public Methods |
||
24 | // ========================================================================= |
||
25 | |||
26 | /** |
||
27 | * Get cache |
||
28 | * |
||
29 | * @param $id |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function get($id) |
||
34 | { |
||
35 | if (Plugin::getInstance()->getSettings()->enableCache === true) { |
||
36 | $cacheKey = $this->getCacheKey($id); |
||
37 | |||
38 | return Craft::$app->cache->get($cacheKey); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Set cache |
||
44 | * |
||
45 | * @param $id |
||
46 | * @param $value |
||
47 | * @param null $expire |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
48 | * @param null $dependency |
||
0 ignored issues
–
show
|
|||
49 | * |
||
50 | * @return mixed |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | public function set($id, $value, $expire = null, $dependency = null) |
||
54 | { |
||
55 | if (Plugin::getInstance()->getSettings()->enableCache === true) { |
||
56 | $cacheKey = $this->getCacheKey($id); |
||
57 | |||
58 | if (!$expire) { |
||
0 ignored issues
–
show
|
|||
59 | $duration = Plugin::getInstance()->getSettings()->cacheDuration; |
||
60 | $expire = TwitterHelper::durationToSeconds($duration); |
||
61 | } |
||
62 | |||
63 | return Craft::$app->cache->set($cacheKey, $value, $expire, $dependency); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // Private Methods |
||
68 | // ========================================================================= |
||
69 | |||
70 | /** |
||
71 | * Get cache key |
||
72 | * |
||
73 | * @param array $request |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | private function getCacheKey(array $request) |
||
78 | { |
||
79 | $dataSourceClassName = 'Twitter'; |
||
80 | |||
81 | unset($request['CRAFT_CSRF_TOKEN']); |
||
82 | |||
83 | $request[] = $dataSourceClassName; |
||
84 | |||
85 | $hash = md5(serialize($request)); |
||
86 | |||
87 | return substr('twitter'.$hash, 0, 32); |
||
88 | } |
||
89 | } |
||
90 |