Skip to content

Commit 8c8fdd2

Browse files
committed
Better MapPool.
1 parent da031f9 commit 8c8fdd2

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/MapPool.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function init(string $key, int $max_size = -1): bool
3434
}
3535
$this->status_map[$key] = [
3636
'max' => $max_size,
37-
'created' => 0
37+
'created' => 0,
38+
'in_pool' => 0,
39+
'reused' => 0,
40+
'destroyed' => 0
3841
];
3942
return true;
4043
}
@@ -46,7 +49,7 @@ public function create(array $options, string $key = null)
4649
if (!$key) {
4750
throw new \InvalidArgumentException('Argument#2 $key can not be empty!');
4851
}
49-
@$this->status_map[$key]['created']++;
52+
$this->status_map[$key]['created']++;
5053
}
5154

5255
public function get(string $key)
@@ -62,6 +65,7 @@ public function get(string $key)
6265
$available = $this->resource_map[$key]->length() > 0 || $this->status_map[$key]['created'] >= $this->status_map[$key]['max'];
6366
}
6467
if ($available) {
68+
$this->status_map[$key]['reused']++;
6569
return $this->resource_map[$key]->pop();
6670
} else {
6771
return null; // need create new one
@@ -76,22 +80,45 @@ public function put($value, string $key = null)
7680
$this->resource_map[$key]->push($value);
7781
}
7882

83+
public function destroy($value, string $key = null)
84+
{
85+
if (!$key) {
86+
throw new \InvalidArgumentException('Argument#2 $key can not be empty!');
87+
}
88+
$this->status_map[$key]['destroyed']++;
89+
}
90+
7991
public function getStatus(string $key): array
8092
{
8193
if (!isset($this->status_map[$key])) {
8294
return [
8395
'max' => null,
8496
'created' => null,
85-
'in_queue' => null
97+
'in_pool' => null,
98+
'reused' => null,
99+
'destroyed' => null
86100
];
87101
} else {
88102
$pool = $this->resource_map[$key];
89-
$in_queue = $pool instanceof \SplQueue ? $pool->count() : $pool->length();
90-
$this->status_map[$key]['in_queue'] = $in_queue;
103+
$in_pool = $pool instanceof \SplQueue ? $pool->count() : $pool->length();
104+
$this->status_map[$key]['in_pool'] = $in_pool;
91105
return $this->status_map[$key];
92106
}
93107
}
94108

109+
public function getAllStatus(bool $full = false): array
110+
{
111+
if ($full) {
112+
$ret = [];
113+
foreach ($this->status_map as $key => $value) {
114+
$ret[$key] = $this->getStatus($key);
115+
}
116+
return $ret;
117+
} else {
118+
return $this->status_map;
119+
}
120+
}
121+
95122
public function getMax(string $key): ?int
96123
{
97124
return $this->status_map[$key]['max'] ?? null;

0 commit comments

Comments
 (0)