From 920829965b4f8aad8271febafd1d6a2a155d8b9e Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Thu, 24 Oct 2013 10:48:42 -0200 Subject: [PATCH] Incrementing hits directly in DB --- src/hitcount/models.py | 12 ++++++++---- src/super_archives/views.py | 3 +-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/hitcount/models.py b/src/hitcount/models.py index 5e52457..afeeae4 100644 --- a/src/hitcount/models.py +++ b/src/hitcount/models.py @@ -45,7 +45,11 @@ class HitCountModelMixin(object): cache.set(cache_key, True) # Everything ok, so just increment the page count - hit_ = Hit.objects.get_or_create(content_type=content_type, - object_pk=self.pk)[0] - hit_.hits += 1 - hit_.save() + hit_pk = Hit.objects.get_or_create(content_type=content_type, + object_pk=self.pk)[0].pk + + # Using this way instead of hits += 1 forces django to + # call the UPDATE directly in the database avoiding + # cuncurrency problems + Hit.objects.filter(pk=hit_pk).update(hits=models.F("hits") + 1) + diff --git a/src/super_archives/views.py b/src/super_archives/views.py index c329965..6d4db98 100644 --- a/src/super_archives/views.py +++ b/src/super_archives/views.py @@ -30,8 +30,7 @@ def thread(request, mailinglist, thread_token): thread = Thread.objects.get(subject_token=thread_token, mailinglist__name=mailinglist) - - thread.hit() + thread.hit(request) order_by = request.GET.get('order') if order_by == 'voted': -- libgit2 0.21.2