Count view: async slow counts

when you count, it's usually because there are many, so this
extra complication is probaly going to be required
This commit is contained in:
Klaas van Schelven
2025-04-17 14:08:28 +02:00
parent 90b80cdc32
commit 5895253803
6 changed files with 109 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("bsmain", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="CachedModelCount",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("app_label", models.CharField(max_length=255)),
("model_name", models.CharField(max_length=255)),
("count", models.PositiveIntegerField()),
("last_updated", models.DateTimeField(auto_now=True)),
],
options={
"unique_together": {("app_label", "model_name")},
},
),
]

View File

@@ -18,3 +18,15 @@ class AuthToken(models.Model):
def __str__(self):
return f"AuthToken(token={self.token})"
class CachedModelCount(models.Model):
"""Model to cache the count of a specific model."""
app_label = models.CharField(max_length=255)
model_name = models.CharField(max_length=255)
count = models.PositiveIntegerField(null=False, blank=False)
last_updated = models.DateTimeField(auto_now=True)
class Meta:
unique_together = ('app_label', 'model_name')

26
bsmain/tasks.py Normal file
View File

@@ -0,0 +1,26 @@
from django.apps import apps
from django.utils import timezone
from snappea.decorators import shared_task
from bugsink.transaction import durable_atomic, immediate_atomic
from .models import CachedModelCount
@shared_task
def count_model(app_label, model_name):
ModelClass = apps.get_model(app_label, model_name)
# separate transaction for the expensive counting
with durable_atomic():
count = ModelClass.objects.count()
with immediate_atomic():
CachedModelCount.objects.update_or_create(
app_label=app_label,
model_name=model_name,
defaults={
'count': count,
'last_updated': timezone.now(),
},
)