1 | <?php |
||
14 | class CachingDecorator implements Decorator |
||
15 | { |
||
16 | /** |
||
17 | * @var CommandBus |
||
18 | */ |
||
19 | private $innerBus; |
||
20 | /** |
||
21 | * @var CacheItemPoolInterface |
||
22 | */ |
||
23 | private $cache; |
||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $expiresAfter; |
||
28 | |||
29 | /** |
||
30 | * CachingDecorator constructor. |
||
31 | * @param CacheItemPoolInterface $cache |
||
32 | * @param int $expiresAfter |
||
33 | * @param CommandBus $innerCommandBus |
||
34 | */ |
||
35 | public function __construct(CacheItemPoolInterface $cache, $expiresAfter = 3600, CommandBus $innerCommandBus = null) |
||
41 | |||
42 | /** |
||
43 | * @inheritdoc |
||
44 | */ |
||
45 | public function setInnerBus(CommandBus $bus) |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public function execute(Command $command) |
||
70 | |||
71 | /** |
||
72 | * Create a new cache item to be persisted. |
||
73 | * |
||
74 | * @param CacheableCommand $command |
||
75 | * @param mixed $value |
||
76 | * @return CacheItemInterface |
||
77 | */ |
||
78 | private function createCacheItem(CacheableCommand $command, $value) |
||
84 | |||
85 | /** |
||
86 | * Create the key to be used when saving this item to the cache pool. |
||
87 | * |
||
88 | * The cache item key is taken as a the (string) serialized command, to ensure the return value is unique |
||
89 | * depending on the command properties; that serialized string is then md5'd to ensure it doesn't |
||
90 | * overflow any string length limits the implementing CacheItemPoolInterface library has. |
||
91 | * |
||
92 | * @param CacheableCommand $command |
||
93 | * @return string |
||
94 | */ |
||
95 | private function getCacheKey(CacheableCommand $command) |
||
103 | |||
104 | /** |
||
105 | * Determine when this CachableCommand should expire, in terms of seconds from now. |
||
106 | * |
||
107 | * @param CacheableCommand $command |
||
108 | * @return int |
||
109 | */ |
||
110 | private function getCacheExpiry(CacheableCommand $command) |
||
118 | } |
||
119 |
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: