| @@ 18-69 (lines=52) @@ | ||
| 15 | ElapsedPeriod |
|
| 16 | }; |
|
| 17 | ||
| 18 | final class CacheCpu implements Server |
|
| 19 | { |
|
| 20 | private $server; |
|
| 21 | private $clock; |
|
| 22 | private $threshold; |
|
| 23 | private $cachedAt; |
|
| 24 | private $data; |
|
| 25 | ||
| 26 | public function __construct( |
|
| 27 | Server $server, |
|
| 28 | TimeContinuumInterface $clock, |
|
| 29 | ElapsedPeriod $threshold |
|
| 30 | ) { |
|
| 31 | $this->server = $server; |
|
| 32 | $this->clock = $clock; |
|
| 33 | $this->threshold = $threshold; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function cpu(): Cpu |
|
| 37 | { |
|
| 38 | $now = $this->clock->now(); |
|
| 39 | ||
| 40 | if ( |
|
| 41 | $this->cachedAt && |
|
| 42 | $this->threshold->longerThan( |
|
| 43 | $now->elapsedSince($this->cachedAt) |
|
| 44 | ) |
|
| 45 | ) { |
|
| 46 | return $this->data; |
|
| 47 | } |
|
| 48 | ||
| 49 | $this->data = $this->server->cpu(); |
|
| 50 | $this->cachedAt = $now; |
|
| 51 | ||
| 52 | return $this->data; |
|
| 53 | } |
|
| 54 | ||
| 55 | public function memory(): Memory |
|
| 56 | { |
|
| 57 | return $this->server->memory(); |
|
| 58 | } |
|
| 59 | ||
| 60 | public function processes(): Processes |
|
| 61 | { |
|
| 62 | return $this->server->processes(); |
|
| 63 | } |
|
| 64 | ||
| 65 | public function loadAverage(): LoadAverage |
|
| 66 | { |
|
| 67 | return $this->server->loadAverage(); |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 18-69 (lines=52) @@ | ||
| 15 | ElapsedPeriod |
|
| 16 | }; |
|
| 17 | ||
| 18 | final class CacheLoadAverage implements Server |
|
| 19 | { |
|
| 20 | private $server; |
|
| 21 | private $clock; |
|
| 22 | private $threshold; |
|
| 23 | private $cachedAt; |
|
| 24 | private $data; |
|
| 25 | ||
| 26 | public function __construct( |
|
| 27 | Server $server, |
|
| 28 | TimeContinuumInterface $clock, |
|
| 29 | ElapsedPeriod $threshold |
|
| 30 | ) { |
|
| 31 | $this->server = $server; |
|
| 32 | $this->clock = $clock; |
|
| 33 | $this->threshold = $threshold; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function cpu(): Cpu |
|
| 37 | { |
|
| 38 | return $this->server->cpu(); |
|
| 39 | } |
|
| 40 | ||
| 41 | public function memory(): Memory |
|
| 42 | { |
|
| 43 | return $this->server->memory(); |
|
| 44 | } |
|
| 45 | ||
| 46 | public function processes(): Processes |
|
| 47 | { |
|
| 48 | return $this->server->processes(); |
|
| 49 | } |
|
| 50 | ||
| 51 | public function loadAverage(): LoadAverage |
|
| 52 | { |
|
| 53 | $now = $this->clock->now(); |
|
| 54 | ||
| 55 | if ( |
|
| 56 | $this->cachedAt && |
|
| 57 | $this->threshold->longerThan( |
|
| 58 | $now->elapsedSince($this->cachedAt) |
|
| 59 | ) |
|
| 60 | ) { |
|
| 61 | return $this->data; |
|
| 62 | } |
|
| 63 | ||
| 64 | $this->data = $this->server->loadAverage(); |
|
| 65 | $this->cachedAt = $now; |
|
| 66 | ||
| 67 | return $this->data; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 18-69 (lines=52) @@ | ||
| 15 | ElapsedPeriod |
|
| 16 | }; |
|
| 17 | ||
| 18 | final class CacheMemory implements Server |
|
| 19 | { |
|
| 20 | private $server; |
|
| 21 | private $clock; |
|
| 22 | private $threshold; |
|
| 23 | private $cachedAt; |
|
| 24 | private $data; |
|
| 25 | ||
| 26 | public function __construct( |
|
| 27 | Server $server, |
|
| 28 | TimeContinuumInterface $clock, |
|
| 29 | ElapsedPeriod $threshold |
|
| 30 | ) { |
|
| 31 | $this->server = $server; |
|
| 32 | $this->clock = $clock; |
|
| 33 | $this->threshold = $threshold; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function cpu(): Cpu |
|
| 37 | { |
|
| 38 | return $this->server->cpu(); |
|
| 39 | } |
|
| 40 | ||
| 41 | public function memory(): Memory |
|
| 42 | { |
|
| 43 | $now = $this->clock->now(); |
|
| 44 | ||
| 45 | if ( |
|
| 46 | $this->cachedAt && |
|
| 47 | $this->threshold->longerThan( |
|
| 48 | $now->elapsedSince($this->cachedAt) |
|
| 49 | ) |
|
| 50 | ) { |
|
| 51 | return $this->data; |
|
| 52 | } |
|
| 53 | ||
| 54 | $this->data = $this->server->memory(); |
|
| 55 | $this->cachedAt = $now; |
|
| 56 | ||
| 57 | return $this->data; |
|
| 58 | } |
|
| 59 | ||
| 60 | public function processes(): Processes |
|
| 61 | { |
|
| 62 | return $this->server->processes(); |
|
| 63 | } |
|
| 64 | ||
| 65 | public function loadAverage(): LoadAverage |
|
| 66 | { |
|
| 67 | return $this->server->loadAverage(); |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||