1import click
2from plain.cli import register_cli
3
4from .models import CachedItem
5
6
7@register_cli("cache")
8@click.group()
9def cli() -> None:
10 """Cache management"""
11
12
13@cli.command()
14def clear_expired() -> None:
15 """Clear expired cache entries"""
16 click.echo("Clearing expired cache items...")
17 count = CachedItem.query.expired().delete()
18 click.echo(f"Deleted {count} expired cache items.")
19
20
21@cli.command()
22@click.option("--yes", "-y", is_flag=True, help="Skip confirmation prompt.")
23def clear_all(yes: bool) -> None:
24 """Clear all cache entries"""
25 if not yes and not click.confirm(
26 "Are you sure you want to delete all cache items?"
27 ):
28 return
29 click.echo("Clearing all cache items...")
30 count = CachedItem.query.all().delete()
31 click.echo(f"Deleted {count} cache items.")
32
33
34@cli.command()
35def stats() -> None:
36 """Show cache statistics"""
37 total = CachedItem.query.count()
38 expired = CachedItem.query.expired().count()
39 unexpired = CachedItem.query.unexpired().count()
40 forever = CachedItem.query.forever().count()
41
42 click.echo(f"Total: {click.style(total, bold=True)}")
43 click.echo(f"Expired: {click.style(expired, bold=True)}")
44 click.echo(f"Unexpired: {click.style(unexpired, bold=True)}")
45 click.echo(f"Forever: {click.style(forever, bold=True)}")