MapUpdate.last_update() and MapUpdate.lock()
This commit is contained in:
parent
375454c3f1
commit
86c476a586
1 changed files with 22 additions and 1 deletions
|
@ -1,5 +1,8 @@
|
|||
from contextlib import contextmanager
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.core.cache import cache
|
||||
from django.db import models, transaction
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
|
@ -17,6 +20,24 @@ class MapUpdate(models.Model):
|
|||
default_related_name = 'mapupdates'
|
||||
get_latest_by = 'datetime'
|
||||
|
||||
@classmethod
|
||||
def last_update(cls):
|
||||
last_update = cache.get('mapdata:last_update', None)
|
||||
if last_update is not None:
|
||||
return last_update
|
||||
with cls.lock():
|
||||
last_update = cls.objects.latest().datetime
|
||||
cache.set('mapdata:last_update', last_update, 900)
|
||||
return last_update
|
||||
|
||||
@classmethod
|
||||
@contextmanager
|
||||
def lock(cls):
|
||||
with transaction.atomic():
|
||||
yield cls.objects.select_for_update().earliest()
|
||||
|
||||
def save(self, **kwargs):
|
||||
if self.pk is not None:
|
||||
raise TypeError
|
||||
super().save(**kwargs)
|
||||
cache.set('mapdata:last_update', self.datetime, 900)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue