1 | <?php |
||
10 | class ExpiringMemoizingSupplier |
||
11 | { |
||
12 | private $cachedResult; |
||
13 | private $lastCallTime; |
||
14 | private $function; |
||
15 | private $expireTime; |
||
16 | |||
17 | public function __construct($function, $expireTime = 3600) |
||
18 | { |
||
19 | $this->function = $function; |
||
20 | $this->expireTime = $expireTime; |
||
21 | } |
||
22 | |||
23 | public function get() |
||
24 | { |
||
25 | $function = $this->function; |
||
26 | $now = Clock::now()->getTimestamp(); |
||
27 | if ($this->cachedResult === null || $now - $this->lastCallTime > $this->expireTime) { |
||
28 | $this->cachedResult = $function(); |
||
29 | $this->lastCallTime = $now; |
||
30 | } |
||
31 | return $this->cachedResult; |
||
32 | } |
||
33 | } |