|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Predis\Client; |
|
6
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
|
7
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
|
8
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\WidgetSlotInterface; |
|
9
|
|
|
use Victoire\Bundle\WidgetBundle\Helper\WidgetHelper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class handle the saving of Widgets. |
|
13
|
|
|
* Widgets are stored for a week, but are invalidated as soon as |
|
14
|
|
|
* the Widget's or BusinessEntity's updatedAt field is changed. |
|
15
|
|
|
*/ |
|
16
|
|
|
class WidgetCache |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Client |
|
20
|
|
|
*/ |
|
21
|
|
|
private $redis; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var AuthorizationChecker |
|
24
|
|
|
*/ |
|
25
|
|
|
private $authorizationChecker; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var WidgetHelper |
|
28
|
|
|
*/ |
|
29
|
|
|
private $widgetHelper; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* WidgetCache constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Client $redis |
|
35
|
|
|
* @param AuthorizationChecker $authorizationChecker |
|
36
|
|
|
* @param WidgetHelper $widgetHelper |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Client $redis, AuthorizationChecker $authorizationChecker, WidgetHelper $widgetHelper) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->redis = $redis; |
|
41
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
42
|
|
|
$this->widgetHelper = $widgetHelper; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Widget $widget |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function fetch(Widget $widget) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->redis->get($this->getHash($widget)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Widget $widget |
|
57
|
|
|
* @param $content |
|
58
|
|
|
*/ |
|
59
|
|
|
public function save(Widget $widget, $content) |
|
60
|
|
|
{ |
|
61
|
|
|
$hash = $this->getHash($widget); |
|
62
|
|
|
if ($hash) { |
|
|
|
|
|
|
63
|
|
|
$this->redis->set($hash, $content); |
|
64
|
|
|
$this->redis->expire($hash, $this->widgetHelper->getCacheTimeout($widget)); // cache for a week |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* clear all redis cache. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function clear() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->redis->executeCommand($this->redis->createCommand('FLUSHALL')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Widget $widget |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getHash(Widget $widget) |
|
82
|
|
|
{ |
|
83
|
|
|
$hash = null; |
|
84
|
|
|
if (!$widget instanceof WidgetSlotInterface) { |
|
85
|
|
|
if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY |
|
86
|
|
|
&& ($entity = $widget->getEntity()) |
|
87
|
|
|
&& method_exists($widget->getEntity(), 'getUpdatedAt')) { |
|
88
|
|
|
$hash = $this->generateBusinessEntityHash($widget); |
|
89
|
|
|
} elseif ($widget->getMode() == Widget::MODE_STATIC) { |
|
90
|
|
|
$hash = $this->generateHash($widget); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $hash; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function generateBusinessEntityHash(Widget $widget) |
|
98
|
|
|
{ |
|
99
|
|
|
return sprintf('%s-%s-%s--%s-%s-%s', |
|
100
|
|
|
$widget->getId(), |
|
101
|
|
|
$widget->getUpdatedAt()->getTimestamp(), |
|
102
|
|
|
$widget->getCurrentView()->getReference()->getId(), |
|
103
|
|
|
$widget->getEntity()->getId(), |
|
|
|
|
|
|
104
|
|
|
$widget->getEntity()->getUpdatedAt()->getTimestamp(), |
|
|
|
|
|
|
105
|
|
|
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE') |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function generateHash($widget) |
|
110
|
|
|
{ |
|
111
|
|
|
return sprintf('%s-%s-%s-%s', |
|
112
|
|
|
$widget->getId(), |
|
113
|
|
|
$widget->getUpdatedAt()->getTimestamp(), |
|
114
|
|
|
$widget->getCurrentView()->getReference()->getId(), |
|
115
|
|
|
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE') |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: