Plain is headed towards 1.0! Subscribe for development updates →

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