1 | <?php |
||||
2 | /** |
||||
3 | * @link https://dukt.net/facebook/ |
||||
4 | * @copyright Copyright (c) Dukt |
||||
5 | * @license https://github.com/dukt/facebook/blob/master/LICENSE.md |
||||
6 | */ |
||||
7 | |||||
8 | namespace dukt\facebook\services; |
||||
9 | |||||
10 | use Craft; |
||||
11 | use yii\base\Component; |
||||
12 | use DateInterval; |
||||
13 | use dukt\facebook\Plugin as Facebook; |
||||
14 | |||||
15 | |||||
16 | /** |
||||
17 | * Class Cache service |
||||
18 | * |
||||
19 | * @author Dukt <[email protected]> |
||||
20 | * @since 2.0 |
||||
21 | */ |
||||
22 | class Cache extends Component |
||||
23 | { |
||||
24 | // Public Methods |
||||
25 | // ========================================================================= |
||||
26 | |||||
27 | /** |
||||
28 | * Retrieves a value from cache with a specified key. |
||||
29 | * |
||||
30 | * @param $id |
||||
31 | * |
||||
32 | * @return mixed |
||||
33 | */ |
||||
34 | public function get($id) |
||||
35 | { |
||||
36 | if (Facebook::$plugin->getSettings()->enableCache == true) { |
||||
37 | $cacheKey = $this->getCacheKey($id); |
||||
38 | |||||
39 | return Craft::$app->cache->get($cacheKey); |
||||
40 | } |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * Stores a value identified by a key into cache. |
||||
45 | * |
||||
46 | * @param $id |
||||
47 | * @param $value |
||||
48 | * @param null $expire |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
49 | * @param null $dependency |
||||
0 ignored issues
–
show
|
|||||
50 | * @param null $enableCache |
||||
0 ignored issues
–
show
|
|||||
51 | * |
||||
52 | * @return bool |
||||
53 | * @throws \Exception |
||||
54 | */ |
||||
55 | public function set($id, $value, $expire = null, $dependency = null, $enableCache = null) |
||||
56 | { |
||||
57 | if (is_null($enableCache)) { |
||||
0 ignored issues
–
show
|
|||||
58 | $enableCache = Facebook::$plugin->getSettings()->enableCache; |
||||
59 | } |
||||
60 | |||||
61 | if ($enableCache) { |
||||
62 | $cacheKey = $this->getCacheKey($id); |
||||
63 | |||||
64 | if (!$expire) { |
||||
0 ignored issues
–
show
|
|||||
65 | $expire = Facebook::$plugin->getSettings()->cacheDuration; |
||||
66 | $expire = $this->_formatDuration($expire); |
||||
67 | } |
||||
68 | |||||
69 | return Craft::$app->cache->set($cacheKey, $value, $expire, $dependency); |
||||
0 ignored issues
–
show
$expire of type string is incompatible with the type integer expected by parameter $duration of yii\caching\CacheInterface::set() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
70 | } |
||||
71 | } |
||||
72 | |||||
73 | // Private Methods |
||||
74 | // ========================================================================= |
||||
75 | /** |
||||
76 | * Formats duration. |
||||
77 | * |
||||
78 | * @param $cacheDuration |
||||
79 | * |
||||
80 | * @return string |
||||
81 | * @throws \Exception |
||||
82 | */ |
||||
83 | private function _formatDuration($cacheDuration, string $format = '%s') |
||||
84 | { |
||||
85 | $cacheDuration = new DateInterval($cacheDuration); |
||||
86 | |||||
87 | return $cacheDuration->format($format); |
||||
88 | } |
||||
89 | |||||
90 | /** |
||||
91 | * Return the cache key. |
||||
92 | * |
||||
93 | * @param array $request |
||||
94 | * |
||||
95 | * @return string |
||||
96 | */ |
||||
97 | private function getCacheKey(array $request) |
||||
98 | { |
||||
99 | $dataSourceClassName = 'Facebook'; |
||||
100 | |||||
101 | unset($request['CRAFT_CSRF_TOKEN']); |
||||
102 | |||||
103 | $request[] = $dataSourceClassName; |
||||
104 | |||||
105 | $hash = md5(serialize($request)); |
||||
106 | |||||
107 | return 'facebook.'.$hash; |
||||
108 | } |
||||
109 | } |
||||
110 |