Skip to content

Commit

Permalink
Make Request.garbage_collect cheaper
Browse files Browse the repository at this point in the history
It would be cheaper to get a single value instead of evaluating
a full queryset.
xrmx authored and avelis committed Feb 15, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 26b1725 commit a47f13c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions silk/models.py
Original file line number Diff line number Diff line change
@@ -150,10 +150,17 @@ def garbage_collect(cls, force=False):
if target_count <= 0:
cls.objects.all().delete()
return
requests = cls.objects.order_by('-start_time')
if not requests or len(requests)-1 < target_count:

try:
time_cutoff = cls.objects.order_by(
'-start_time'
).values_list(
'start_time',
flat=True
)[target_count]
except IndexError:
return
time_cutoff = requests[target_count].start_time

cls.objects.filter(start_time__lte=time_cutoff).delete()

def save(self, *args, **kwargs):

0 comments on commit a47f13c

Please sign in to comment.