1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Cache; |
4
|
|
|
|
5
|
|
|
use Predis\Client; |
6
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
7
|
|
|
use Symfony\Component\Security\Core\SecurityContext; |
8
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
9
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\WidgetSlotInterface; |
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
|
|
|
public function __construct(Client $redis, AuthorizationChecker $authorizationChecker) |
28
|
|
|
{ |
29
|
|
|
$this->redis = $redis; |
30
|
|
|
$this->authorizationChecker = $authorizationChecker; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Widget $widget |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function fetch(Widget $widget) |
39
|
|
|
{ |
40
|
|
|
return $this->redis->get($this->getHash($widget)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Widget $widget |
45
|
|
|
* @param $content |
46
|
|
|
*/ |
47
|
|
|
public function save(Widget $widget, $content) |
48
|
|
|
{ |
49
|
|
|
$hash = $this->getHash($widget); |
50
|
|
|
if ($hash) { |
|
|
|
|
51
|
|
|
$this->redis->set($hash, $content); |
52
|
|
|
$this->redis->expire($hash, 7 * 24 * 60 * 1000); // cache for a week |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Widget $widget |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function getHash(Widget $widget) |
62
|
|
|
{ |
63
|
|
|
$hash = null; |
64
|
|
|
if (!$widget instanceof WidgetSlotInterface) { |
65
|
|
|
if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY |
66
|
|
|
&& ($entity = $widget->getEntity()) |
67
|
|
|
&& method_exists($widget->getEntity(), 'getUpdatedAt')) { |
68
|
|
|
$hash = $this->generateBusinessEntityHash($widget); |
69
|
|
|
} elseif ($widget->getMode() == Widget::MODE_STATIC) { |
70
|
|
|
$hash = $this->generateHash($widget); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $hash; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function generateBusinessEntityHash(Widget $widget) |
78
|
|
|
{ |
79
|
|
|
return sprintf('%s-%s-%s--%s-%s-%s', |
80
|
|
|
$widget->getId(), |
81
|
|
|
$widget->getUpdatedAt()->getTimestamp(), |
82
|
|
|
$widget->getCurrentView()->getReference()->getId(), |
83
|
|
|
$widget->getEntity()->getId(), |
|
|
|
|
84
|
|
|
$widget->getEntity()->getUpdatedAt()->getTimestamp(), |
|
|
|
|
85
|
|
|
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE') |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function generateHash($widget) |
90
|
|
|
{ |
91
|
|
|
return sprintf('%s-%s-%s-%s', |
92
|
|
|
$widget->getId(), |
93
|
|
|
$widget->getUpdatedAt()->getTimestamp(), |
94
|
|
|
$widget->getCurrentView()->getReference()->getId(), |
95
|
|
|
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: