@@ -181,35 +181,115 @@ private function form(): string {
181181 * @return array<int, array<string, string|int>>
182182 */
183183 private function getAllKeys (): array {
184- static $ keys = [];
185184 $ search = Http::get ('s ' , '' );
186-
187185 $ this ->template ->addGlobal ('search_value ' , $ search );
188186
189187 $ info = apcu_cache_info ();
188+ $ keys = [];
189+ $ time = time ();
190190
191191 foreach ($ info ['cache_list ' ] as $ key_data ) {
192192 $ key = $ key_data ['info ' ];
193193
194194 if (stripos ($ key , $ search ) !== false ) {
195195 $ keys [] = [
196- 'key ' => $ key ,
197- 'base64 ' => true ,
198- 'info ' => [
199- 'link_title ' => $ key ,
200- 'bytes_size ' => $ key_data ['mem_size ' ],
201- 'number_hits ' => $ key_data ['num_hits ' ],
202- 'timediff_last_used ' => $ key_data ['access_time ' ],
203- 'time_created ' => $ key_data ['creation_time ' ],
204- 'ttl ' => $ key_data ['ttl ' ] === 0 ? 'Doesn \'t expire ' : $ key_data ['creation_time ' ] + $ key_data ['ttl ' ] - time (),
205- ],
196+ 'key ' => $ key ,
197+ 'mem_size ' => $ key_data ['mem_size ' ],
198+ 'num_hits ' => $ key_data ['num_hits ' ],
199+ 'access_time ' => $ key_data ['access_time ' ],
200+ 'creation_time ' => $ key_data ['creation_time ' ],
201+ 'ttl ' => $ key_data ['ttl ' ] === 0 ? 'Doesn \'t expire ' : $ key_data ['creation_time ' ] + $ key_data ['ttl ' ] - $ time ,
206202 ];
207203 }
208204 }
209205
210- $ keys = Helpers::sortKeys ($ this ->template , $ keys );
206+ if (Http::get ('view ' , 'table ' ) === 'tree ' ) {
207+ return $ this ->keysTreeView ($ keys );
208+ }
209+
210+ return $ this ->keysTableView ($ keys );
211+ }
212+
213+ /**
214+ * @param array<int|string, mixed> $keys
215+ *
216+ * @return array<int, array<string, string|int>>
217+ */
218+ private function keysTableView (array $ keys ): array {
219+ $ formatted_keys = [];
220+
221+ foreach ($ keys as $ key_data ) {
222+ $ formatted_keys [] = [
223+ 'key ' => $ key_data ['key ' ],
224+ 'base64 ' => true ,
225+ 'info ' => [
226+ 'link_title ' => $ key_data ['key ' ],
227+ 'bytes_size ' => $ key_data ['mem_size ' ],
228+ 'number_hits ' => $ key_data ['num_hits ' ],
229+ 'timediff_last_used ' => $ key_data ['access_time ' ],
230+ 'time_created ' => $ key_data ['creation_time ' ],
231+ 'ttl ' => $ key_data ['ttl ' ],
232+ ],
233+ ];
234+ }
235+
236+ return Helpers::sortKeys ($ this ->template , $ formatted_keys );
237+ }
238+
239+ /**
240+ * @param array<int|string, mixed> $keys
241+ *
242+ * @return array<int, array<string, string|int>>
243+ */
244+ private function keysTreeView (array $ keys ): array {
245+ $ separator = Config::get ('apcu-separator ' , ': ' );
246+ $ this ->template ->addGlobal ('separator ' , $ separator );
247+
248+ $ tree = [];
249+
250+ foreach ($ keys as $ key_data ) {
251+ $ key = $ key_data ['key ' ];
252+ $ parts = explode ($ separator , $ key );
253+
254+ /** @var array<int|string, mixed> $current */
255+ $ current = &$ tree ;
256+ $ path = '' ;
257+
258+ foreach ($ parts as $ i => $ part ) {
259+ $ path = $ path ? $ path .$ separator .$ part : $ part ;
260+
261+ if ($ i === count ($ parts ) - 1 ) { // check last part
262+ $ current [] = [
263+ 'type ' => 'key ' ,
264+ 'name ' => $ part ,
265+ 'key ' => $ key ,
266+ 'base64 ' => true ,
267+ 'info ' => [
268+ 'bytes_size ' => $ key_data ['mem_size ' ],
269+ 'number_hits ' => $ key_data ['num_hits ' ],
270+ 'timediff_last_used ' => $ key_data ['access_time ' ],
271+ 'time_created ' => $ key_data ['creation_time ' ],
272+ 'ttl ' => $ key_data ['ttl ' ],
273+ ],
274+ ];
275+ } else {
276+ if (!isset ($ current [$ part ])) {
277+ $ current [$ part ] = [
278+ 'type ' => 'folder ' ,
279+ 'name ' => $ part ,
280+ 'path ' => $ path ,
281+ 'children ' => [],
282+ 'expanded ' => false ,
283+ ];
284+ }
285+ $ current = &$ current [$ part ]['children ' ];
286+ }
287+ }
288+ }
289+
290+ Helpers::countChildren ($ tree );
211291
212- return $ keys ;
292+ return $ tree ;
213293 }
214294
215295 private function mainDashboard (): string {
0 commit comments