carno-php /
cache
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Native memory keys eviction |
||||||
| 4 | * User: moyo |
||||||
| 5 | * Date: 2018/6/25 |
||||||
| 6 | * Time: 5:51 PM |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace Carno\Cache; |
||||||
| 10 | |||||||
| 11 | use Carno\Timer\Timer; |
||||||
| 12 | use SplPriorityQueue; |
||||||
| 13 | |||||||
| 14 | class Eviction |
||||||
| 15 | { |
||||||
| 16 | /** |
||||||
| 17 | * @var SplPriorityQueue |
||||||
| 18 | */ |
||||||
| 19 | private $heap = null; |
||||||
| 20 | |||||||
| 21 | /** |
||||||
| 22 | * @var string |
||||||
| 23 | */ |
||||||
| 24 | private $daemon = null; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @var int |
||||||
| 28 | */ |
||||||
| 29 | private $nearest = PHP_INT_MAX; |
||||||
| 30 | |||||||
| 31 | /** |
||||||
| 32 | * Eviction constructor. |
||||||
| 33 | */ |
||||||
| 34 | public function __construct() |
||||||
| 35 | { |
||||||
| 36 | $this->heap = new class extends SplPriorityQueue { |
||||||
| 37 | /** |
||||||
| 38 | * heap constructor. |
||||||
| 39 | */ |
||||||
| 40 | public function __construct() |
||||||
| 41 | { |
||||||
| 42 | $this->setExtractFlags(SplPriorityQueue::EXTR_DATA); |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | /** |
||||||
| 46 | * @param mixed $priority1 |
||||||
| 47 | * @param mixed $priority2 |
||||||
| 48 | * @return int |
||||||
| 49 | */ |
||||||
| 50 | public function compare($priority1, $priority2) : int |
||||||
| 51 | { |
||||||
| 52 | return $priority2 <=> $priority1; |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | /** |
||||||
| 56 | * @return array |
||||||
| 57 | */ |
||||||
| 58 | public function nearest() : array |
||||||
| 59 | { |
||||||
| 60 | return $this->top(); |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * @param int $bid |
||||||
| 65 | * @param string $key |
||||||
| 66 | * @param int $ttl |
||||||
| 67 | */ |
||||||
| 68 | public function append(int $bid, string $key, int $ttl) : void |
||||||
| 69 | { |
||||||
| 70 | $this->insert([time() + $ttl, $bid, $key], $ttl); |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | /** |
||||||
| 74 | * extract and forget it |
||||||
| 75 | */ |
||||||
| 76 | public function forget() : void |
||||||
| 77 | { |
||||||
| 78 | $this->extract(); |
||||||
| 79 | } |
||||||
| 80 | }; |
||||||
| 81 | } |
||||||
| 82 | |||||||
| 83 | /** |
||||||
| 84 | */ |
||||||
| 85 | public function startup() : void |
||||||
| 86 | { |
||||||
| 87 | $this->daemon || $this->daemon = Timer::loop(1000, [$this, 'polling']); |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | /** |
||||||
| 91 | */ |
||||||
| 92 | public function shutdown() : void |
||||||
| 93 | { |
||||||
| 94 | $this->daemon && Timer::clear($this->daemon); |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | /** |
||||||
| 98 | */ |
||||||
| 99 | public function polling() : void |
||||||
| 100 | { |
||||||
| 101 | if ($this->nearest > 0 && $this->nearest -- > 0) { |
||||||
| 102 | return; |
||||||
| 103 | } |
||||||
| 104 | |||||||
| 105 | $now = time(); |
||||||
| 106 | |||||||
| 107 | while ($this->heap->valid()) { |
||||||
| 108 | [$expired, $bid, $key] = $this->heap->nearest(); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 109 | |||||||
| 110 | if ($now < $expired) { |
||||||
| 111 | $this->nearest = $expired - $now; |
||||||
| 112 | break; |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | $this->heap->forget(); |
||||||
|
0 ignored issues
–
show
The method
forget() does not exist on SplPriorityQueue. It seems like you code against a sub-type of SplPriorityQueue such as anonymous//src/Eviction.php$0.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 116 | |||||||
| 117 | if (null !== $local = Blocks::get($bid)) { |
||||||
| 118 | $local->remove($key); |
||||||
| 119 | } |
||||||
| 120 | } |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | /** |
||||||
| 124 | * @param int $bid |
||||||
| 125 | * @param string $key |
||||||
| 126 | * @param int $ttl |
||||||
| 127 | */ |
||||||
| 128 | public function watch(int $bid, string $key, int $ttl) : void |
||||||
| 129 | { |
||||||
| 130 | if ($ttl <= 0) { |
||||||
| 131 | return; |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | if ($ttl < $this->nearest) { |
||||||
| 135 | $this->nearest = $ttl; |
||||||
| 136 | } |
||||||
| 137 | |||||||
| 138 | $this->heap->append($bid, $key, $ttl); |
||||||
|
0 ignored issues
–
show
The method
append() does not exist on SplPriorityQueue. It seems like you code against a sub-type of SplPriorityQueue such as anonymous//src/Eviction.php$0.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 139 | } |
||||||
| 140 | } |
||||||
| 141 |