@@ -61,13 +61,8 @@ public function handle(): int
6161 return self ::SUCCESS ;
6262 }
6363
64- // Get the key value to show information
65- $ value = $ this ->store ->get ($ selectedKey );
66- $ valuePreview = $ this ->getValuePreview ($ value );
67-
6864 $ this ->newLine ();
6965 $ this ->line ("📝 <fg=cyan>Key:</> {$ selectedKey }" );
70- $ this ->line ("💾 <fg=cyan>Value:</> {$ valuePreview }" );
7166 $ this ->newLine ();
7267
7368 $ confirmed = confirm (
@@ -81,7 +76,23 @@ public function handle(): int
8176 return self ::SUCCESS ;
8277 }
8378
84- if ($ this ->store ->forget ($ selectedKey )) {
79+ // Try to delete the key
80+ // For Redis, we need to add the prefix back since we removed it when listing keys
81+ if ($ this ->driver === 'redis ' ) {
82+ $ prefix = config ('database.redis.options.prefix ' , '' );
83+ $ fullKey = $ prefix ? $ prefix .$ selectedKey : $ selectedKey ;
84+ $ deleted = $ this ->store ->forget ($ fullKey );
85+ } else {
86+ $ deleted = $ this ->store ->forget ($ selectedKey );
87+ }
88+
89+ // If not deleted, try different approaches based on driver
90+ if (! $ deleted && $ this ->driver === 'file ' ) {
91+ // For file driver, try to delete using the actual key
92+ $ deleted = $ this ->deleteFileKeyByKey ($ selectedKey );
93+ }
94+
95+ if ($ deleted ) {
8596 info ("🗑️ The key ' {$ selectedKey }' has been successfully deleted " );
8697
8798 return self ::SUCCESS ;
@@ -119,7 +130,7 @@ private function getRedisKeys(): array
119130 return $ key ;
120131 }, $ keys );
121132 } catch (Exception $ e ) {
122- error ('Error al obtener claves de Redis: ' .$ e ->getMessage ());
133+ error ('Error getting Redis keys : ' .$ e ->getMessage ());
123134
124135 return [];
125136 }
@@ -138,17 +149,39 @@ private function getFileKeys(): array
138149 $ keys = [];
139150
140151 foreach ($ files as $ file ) {
141- // El nombre del archivo en Laravel es un hash, pero podemos leer el contenido
142- $ content = File::get ($ file ->getPathname ());
143-
144- // Formato del archivo de caché de Laravel: expiration_time + serialized_value
145- // Intentar extraer el nombre de la clave del contenido serializado
146- $ keys [] = $ file ->getFilename ();
152+ try {
153+ $ content = File::get ($ file ->getPathname ());
154+
155+ // Laravel file cache format: expiration_time + serialized_value
156+ if (mb_strlen ($ content ) < 10 ) {
157+ continue ;
158+ }
159+
160+ $ expiration = mb_substr ($ content , 0 , 10 );
161+ $ serialized = mb_substr ($ content , 10 );
162+
163+ // Check if expired
164+ if (time () > $ expiration ) {
165+ continue ;
166+ }
167+
168+ // Try to unserialize to get the actual key
169+ $ data = unserialize ($ serialized );
170+ if (is_array ($ data ) && isset ($ data ['key ' ])) {
171+ $ keys [] = $ data ['key ' ];
172+ } else {
173+ // Fallback to filename if we can't extract the key
174+ $ keys [] = $ file ->getFilename ();
175+ }
176+ } catch (Exception ) {
177+ // If we can't read this file, skip it
178+ continue ;
179+ }
147180 }
148181
149182 return $ keys ;
150183 } catch (Exception $ e ) {
151- error ('Error al obtener claves del sistema de archivos : ' .$ e ->getMessage ());
184+ error ('Error getting file system keys : ' .$ e ->getMessage ());
152185
153186 return [];
154187 }
@@ -161,7 +194,7 @@ private function getDatabaseKeys(): array
161194
162195 return DB ::table ($ table )->pluck ('key ' )->toArray ();
163196 } catch (Exception $ e ) {
164- error ('Error al obtener claves de la base de datos : ' .$ e ->getMessage ());
197+ error ('Error getting database keys : ' .$ e ->getMessage ());
165198
166199 return [];
167200 }
@@ -184,32 +217,95 @@ private function handleUnsupportedDriver(): array
184217 return [];
185218 }
186219
187- private function getValuePreview ( mixed $ value ): string
220+ private function getFileKeyValue ( string $ filename ): mixed
188221 {
189- $ previewLimit = config ('cache-ui-laravel.preview_limit ' , 100 );
222+ try {
223+ $ cachePath = config ('cache.stores.file.path ' , storage_path ('framework/cache/data ' ));
224+ $ filePath = $ cachePath .'/ ' .$ filename ;
190225
191- if (is_null ( $ value )) {
192- return ' <fg=gray>( null)</> ' ;
193- }
226+ if (! File:: exists ( $ filePath )) {
227+ return null ;
228+ }
194229
195- if (is_bool ($ value )) {
196- return $ value ? '<fg=green>true</> ' : '<fg=red>false</> ' ;
197- }
230+ $ content = File::get ($ filePath );
198231
199- if (is_array ($ value ) || is_object ($ value )) {
200- $ json = json_encode ($ value , JSON_UNESCAPED_UNICODE );
201- if (mb_strlen ($ json ) > $ previewLimit ) {
202- return mb_substr ($ json , 0 , $ previewLimit ).'<fg=gray>...</> ' ;
232+ // Laravel file cache format: expiration_time + serialized_value
233+ if (mb_strlen ($ content ) < 10 ) {
234+ return null ;
203235 }
204236
205- return $ json ;
237+ $ expiration = mb_substr ($ content , 0 , 10 );
238+ $ serialized = mb_substr ($ content , 10 );
239+
240+ // Check if expired
241+ if (time () > $ expiration ) {
242+ return null ;
243+ }
244+
245+ return unserialize ($ serialized );
246+ } catch (Exception ) {
247+ return null ;
206248 }
249+ }
250+
251+ private function deleteFileKeyByKey (string $ key ): bool
252+ {
253+ try {
254+ $ cachePath = config ('cache.stores.file.path ' , storage_path ('framework/cache/data ' ));
255+
256+ if (! File::exists ($ cachePath )) {
257+ return false ;
258+ }
207259
208- $ stringValue = (string ) $ value ;
209- if (mb_strlen ($ stringValue ) > $ previewLimit ) {
210- return mb_substr ($ stringValue , 0 , $ previewLimit ).'<fg=gray>...</> ' ;
260+ $ files = File::allFiles ($ cachePath );
261+
262+ foreach ($ files as $ file ) {
263+ try {
264+ $ content = File::get ($ file ->getPathname ());
265+
266+ // Laravel file cache format: expiration_time + serialized_value
267+ if (mb_strlen ($ content ) < 10 ) {
268+ continue ;
269+ }
270+
271+ $ expiration = mb_substr ($ content , 0 , 10 );
272+ $ serialized = mb_substr ($ content , 10 );
273+
274+ // Check if expired
275+ if (time () > $ expiration ) {
276+ continue ;
277+ }
278+
279+ // Try to unserialize to get the data
280+ $ data = unserialize ($ serialized );
281+ if (is_array ($ data ) && isset ($ data ['key ' ]) && $ data ['key ' ] === $ key ) {
282+ return File::delete ($ file ->getPathname ());
283+ }
284+ } catch (Exception ) {
285+ // If we can't read this file, skip it
286+ continue ;
287+ }
288+ }
289+
290+ return false ;
291+ } catch (Exception ) {
292+ return false ;
211293 }
294+ }
212295
213- return $ stringValue ;
296+ private function deleteFileKey (string $ filename ): bool
297+ {
298+ try {
299+ $ cachePath = config ('cache.stores.file.path ' , storage_path ('framework/cache/data ' ));
300+ $ filePath = $ cachePath .'/ ' .$ filename ;
301+
302+ if (File::exists ($ filePath )) {
303+ return File::delete ($ filePath );
304+ }
305+
306+ return false ;
307+ } catch (Exception ) {
308+ return false ;
309+ }
214310 }
215311}
0 commit comments