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