|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chief\Decorators; |
|
4
|
|
|
|
|
5
|
|
|
use Chief\Busses\SynchronousCommandBus; |
|
6
|
|
|
use Chief\CacheableCommand; |
|
7
|
|
|
use Chief\Command; |
|
8
|
|
|
use Chief\CommandBus; |
|
9
|
|
|
use Chief\Decorator; |
|
10
|
|
|
use Chief\HasCacheOptions; |
|
11
|
|
|
use Psr\Cache\CacheItemInterface; |
|
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
13
|
|
|
|
|
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) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cache = $cache; |
|
38
|
|
|
$this->expiresAfter = $expiresAfter; |
|
39
|
|
|
$this->setInnerBus($innerCommandBus ?: new SynchronousCommandBus); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setInnerBus(CommandBus $bus) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->innerBus = $bus; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function execute(Command $command) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$command instanceof CacheableCommand) { |
|
56
|
|
|
return $this->innerBus->execute($command); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$cached = $this->cache->getItem($this->getCacheKey($command)); |
|
60
|
|
|
if ($cached->isHit()) { |
|
61
|
|
|
return $cached->get(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$value = $this->innerBus->execute($command); |
|
65
|
|
|
|
|
66
|
|
|
$this->cache->save($this->createCacheItem($command, $value)); |
|
67
|
|
|
|
|
68
|
|
|
return $value; |
|
69
|
|
|
} |
|
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) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->cache->getItem($this->getCacheKey($command)) |
|
81
|
|
|
->expiresAfter($this->getCacheExpiry($command)) |
|
82
|
|
|
->set($value); |
|
83
|
|
|
} |
|
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) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($command instanceof HasCacheOptions && $command->getCacheKey()) { |
|
|
|
|
|
|
98
|
|
|
return $command->getCacheKey(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return md5(serialize($command)); |
|
102
|
|
|
} |
|
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) |
|
111
|
|
|
{ |
|
112
|
|
|
if ($command instanceof HasCacheOptions && $command->getCacheExpiry() > 0) { |
|
113
|
|
|
return $command->getCacheExpiry(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->expiresAfter; |
|
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: