@@ -34,7 +34,10 @@ public function init(string $key, int $max_size = -1): bool
34
34
}
35
35
$ this ->status_map [$ key ] = [
36
36
'max ' => $ max_size ,
37
- 'created ' => 0
37
+ 'created ' => 0 ,
38
+ 'in_pool ' => 0 ,
39
+ 'reused ' => 0 ,
40
+ 'destroyed ' => 0
38
41
];
39
42
return true ;
40
43
}
@@ -46,7 +49,7 @@ public function create(array $options, string $key = null)
46
49
if (!$ key ) {
47
50
throw new \InvalidArgumentException ('Argument#2 $key can not be empty! ' );
48
51
}
49
- @ $ this ->status_map [$ key ]['created ' ]++;
52
+ $ this ->status_map [$ key ]['created ' ]++;
50
53
}
51
54
52
55
public function get (string $ key )
@@ -62,6 +65,7 @@ public function get(string $key)
62
65
$ available = $ this ->resource_map [$ key ]->length () > 0 || $ this ->status_map [$ key ]['created ' ] >= $ this ->status_map [$ key ]['max ' ];
63
66
}
64
67
if ($ available ) {
68
+ $ this ->status_map [$ key ]['reused ' ]++;
65
69
return $ this ->resource_map [$ key ]->pop ();
66
70
} else {
67
71
return null ; // need create new one
@@ -76,22 +80,45 @@ public function put($value, string $key = null)
76
80
$ this ->resource_map [$ key ]->push ($ value );
77
81
}
78
82
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
+
79
91
public function getStatus (string $ key ): array
80
92
{
81
93
if (!isset ($ this ->status_map [$ key ])) {
82
94
return [
83
95
'max ' => null ,
84
96
'created ' => null ,
85
- 'in_queue ' => null
97
+ 'in_pool ' => null ,
98
+ 'reused ' => null ,
99
+ 'destroyed ' => null
86
100
];
87
101
} else {
88
102
$ 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 ;
91
105
return $ this ->status_map [$ key ];
92
106
}
93
107
}
94
108
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
+
95
122
public function getMax (string $ key ): ?int
96
123
{
97
124
return $ this ->status_map [$ key ]['max ' ] ?? null ;
0 commit comments