2424from archpkg .command_gen import generate_command
2525from archpkg .logging_config import get_logger , PackageHelperLogger
2626from archpkg .suggest import suggest_apps , list_purposes
27+ from archpkg .cache import get_cache_manager , CacheConfig
2728
2829console = Console ()
2930logger = get_logger (__name__ )
@@ -323,6 +324,7 @@ def main() -> None:
323324 search_parser = subparsers .add_parser ('search' , help = 'Search for packages by name' )
324325 search_parser .add_argument ('query' , type = str , nargs = '*' , help = 'Name of the software to search for' )
325326 search_parser .add_argument ('--aur' , action = 'store_true' , help = 'Prefer AUR packages over Pacman when both are available' )
327+ search_parser .add_argument ('--no-cache' , action = 'store_true' , help = 'Bypass cache and perform fresh search' )
326328
327329 # Suggest command
328330 suggest_parser = subparsers .add_parser ('suggest' , help = 'Get app suggestions based on purpose' )
@@ -332,14 +334,53 @@ def main() -> None:
332334 # Global arguments
333335 parser .add_argument ('--debug' , action = 'store_true' , help = 'Enable debug logging to console' )
334336 parser .add_argument ('--log-info' , action = 'store_true' , help = 'Show logging configuration and exit' )
337+ parser .add_argument ('--no-cache' , action = 'store_true' , help = 'Bypass cache and perform fresh search' )
338+ parser .add_argument ('--cache-stats' , action = 'store_true' , help = 'Show cache statistics and exit' )
339+ parser .add_argument ('--clear-cache' , choices = ['all' , 'aur' , 'pacman' , 'apt' , 'dnf' , 'flatpak' , 'snap' ],
340+ help = 'Clear cache for specified source or all sources' )
335341
336342 args = parser .parse_args ()
337343
344+ # Initialize cache manager
345+ cache_config = CacheConfig (enabled = not args .no_cache )
346+ cache_manager = get_cache_manager (cache_config )
347+
338348 # Enable debug mode if requested
339349 if args .debug :
340350 PackageHelperLogger .set_debug_mode (True )
341351 logger .info ("Debug mode enabled via command line argument" )
342352
353+ # Handle cache-related commands
354+ if args .cache_stats :
355+ stats = cache_manager .get_stats ()
356+ console .print (Panel (
357+ f"[bold cyan]Cache Statistics:[/bold cyan]\n "
358+ f"Enabled: { '[green]Yes[/green]' if stats .get ('enabled' ) else '[red]No[/red]' } \n "
359+ f"Total entries: [yellow]{ stats .get ('total_entries' , 0 )} [/yellow]\n "
360+ f"Valid entries: [green]{ stats .get ('valid_entries' , 0 )} [/green]\n "
361+ f"Total accesses: [blue]{ stats .get ('total_accesses' , 0 )} [/blue]\n "
362+ f"Average access count: [magenta]{ stats .get ('avg_access_count' , 0 )} [/magenta]\n "
363+ f"Database path: [cyan]{ stats .get ('db_path' , 'N/A' )} [/cyan]\n "
364+ f"TTL: [yellow]{ stats .get ('config' , {}).get ('ttl_seconds' , 0 )} s[/yellow]\n "
365+ f"Max entries: [yellow]{ stats .get ('config' , {}).get ('max_entries' , 0 )} [/yellow]\n \n "
366+ f"[bold]Source breakdown:[/bold]\n " +
367+ '\n ' .join ([f" { source } : { count } " for source , count in stats .get ('source_breakdown' , {}).items ()]),
368+ title = "Cache Statistics" ,
369+ border_style = "blue"
370+ ))
371+ return
372+
373+ if args .clear_cache :
374+ source = None if args .clear_cache == 'all' else args .clear_cache
375+ cleared_count = cache_manager .clear (source )
376+ target = args .clear_cache if args .clear_cache != 'all' else 'all sources'
377+ console .print (Panel (
378+ f"[green]Successfully cleared { cleared_count } cache entries for { target } .[/green]" ,
379+ title = "Cache Cleared" ,
380+ border_style = "green"
381+ ))
382+ return
383+
343384 # Show logging info if requested
344385 if args .log_info :
345386 from archpkg .logging_config import get_log_info
@@ -361,7 +402,7 @@ def main() -> None:
361402 return
362403 elif args .command == 'search' or args .command is None :
363404 # Default to search behavior for backward compatibility
364- handle_search_command (args )
405+ handle_search_command (args , cache_manager )
365406 return
366407 else :
367408 console .print (Panel (
@@ -417,7 +458,7 @@ def handle_suggest_command(args) -> None:
417458 suggest_apps (purpose )
418459
419460
420- def handle_search_command (args ) -> None :
461+ def handle_search_command (args , cache_manager ) -> None :
421462 """Handle the search command (original functionality)."""
422463 if not args .query :
423464 console .print (Panel (
@@ -455,14 +496,15 @@ def handle_search_command(args) -> None:
455496
456497 results = []
457498 search_errors = []
499+ use_cache = not args .no_cache
458500
459501 # Search based on detected distribution
460502 if detected == "arch" :
461503 logger .info ("Searching Arch-based repositories (AUR + pacman)" )
462504
463505 try :
464506 logger .debug ("Starting AUR search" )
465- aur_results = search_aur (query )
507+ aur_results = search_aur (query , cache_manager if use_cache else None )
466508 results .extend (aur_results )
467509 logger .info (f"AUR search returned { len (aur_results )} results" )
468510 except Exception as e :
@@ -471,7 +513,7 @@ def handle_search_command(args) -> None:
471513
472514 try :
473515 logger .debug ("Starting pacman search" )
474- pacman_results = search_pacman (query )
516+ pacman_results = search_pacman (query , cache_manager if use_cache else None )
475517 results .extend (pacman_results )
476518 logger .info (f"Pacman search returned { len (pacman_results )} results" )
477519 except Exception as e :
@@ -483,7 +525,7 @@ def handle_search_command(args) -> None:
483525
484526 try :
485527 logger .debug ("Starting APT search" )
486- apt_results = search_apt (query )
528+ apt_results = search_apt (query , cache_manager if use_cache else None )
487529 results .extend (apt_results )
488530 logger .info (f"APT search returned { len (apt_results )} results" )
489531 except Exception as e :
@@ -495,7 +537,7 @@ def handle_search_command(args) -> None:
495537
496538 try :
497539 logger .debug ("Starting DNF search" )
498- dnf_results = search_dnf (query )
540+ dnf_results = search_dnf (query , cache_manager if use_cache else None )
499541 results .extend (dnf_results )
500542 logger .info (f"DNF search returned { len (dnf_results )} results" )
501543 except Exception as e :
@@ -507,7 +549,7 @@ def handle_search_command(args) -> None:
507549
508550 try :
509551 logger .debug ("Starting Flatpak search" )
510- flatpak_results = search_flatpak (query )
552+ flatpak_results = search_flatpak (query , cache_manager if use_cache else None )
511553 results .extend (flatpak_results )
512554 logger .info (f"Flatpak search returned { len (flatpak_results )} results" )
513555 except Exception as e :
@@ -516,7 +558,7 @@ def handle_search_command(args) -> None:
516558
517559 try :
518560 logger .debug ("Starting Snap search" )
519- snap_results = search_snap (query )
561+ snap_results = search_snap (query , cache_manager if use_cache else None )
520562 results .extend (snap_results )
521563 logger .info (f"Snap search returned { len (snap_results )} results" )
522564 except Exception as e :
0 commit comments