diff --git a/src/c3nav/editor/models/changeset.py b/src/c3nav/editor/models/changeset.py index 5f0353c2..3a1804be 100644 --- a/src/c3nav/editor/models/changeset.py +++ b/src/c3nav/editor/models/changeset.py @@ -6,6 +6,8 @@ from django.apps import apps from django.conf import settings from django.db import models, transaction from django.urls import reverse +from django.utils.http import int_to_base36 +from django.utils.timezone import make_naive from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ungettext_lazy from rest_framework.exceptions import PermissionDenied @@ -410,10 +412,18 @@ class ChangeSet(models.Model): {'num': self.changed_objects_count}) @property - def cache_key(self): - if self.pk is None: - return None - return str(self.pk)+'-'+str(self.last_change) + def updates_cache_key(self): + pk, mapupdate = MapUpdate.last_update() + last_update = self.created if self.last_update_id is None else self.last_update.datetime + return (MapUpdate.cache_key() + '_' + int_to_base36(self.last_update_id or 0) + '_' + + int_to_base36(int(make_naive(last_update).timestamp()))) + + @property + def changes_cache_key(self): + pk, mapupdate = MapUpdate.last_update() + last_change = self.created if self.last_change_id is None else self.last_change.datetime + return (MapUpdate.cache_key() + '_' + int_to_base36(self.last_change_id or 0) + '_' + + int_to_base36(int(make_naive(last_change).timestamp()))) def get_absolute_url(self): if self.pk is None: diff --git a/src/c3nav/mapdata/models/update.py b/src/c3nav/mapdata/models/update.py index 75621c2c..d2f1505a 100644 --- a/src/c3nav/mapdata/models/update.py +++ b/src/c3nav/mapdata/models/update.py @@ -3,6 +3,8 @@ from contextlib import contextmanager from django.conf import settings from django.core.cache import cache from django.db import models, transaction +from django.utils.http import int_to_base36 +from django.utils.timezone import make_naive from django.utils.translation import ugettext_lazy as _ @@ -26,9 +28,14 @@ class MapUpdate(models.Model): 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 + last_update = cls.objects.latest() + cache.set('mapdata:last_update', (last_update.pk, last_update.datetime), 900) + return last_update.pk, last_update.datetime + + @classmethod + def cache_key(cls): + pk, dt = cls.last_update() + return int_to_base36(pk)+'_'+int_to_base36(int(make_naive(dt).timestamp())) @classmethod @contextmanager @@ -40,4 +47,4 @@ class MapUpdate(models.Model): if self.pk is not None: raise TypeError super().save(**kwargs) - cache.set('mapdata:last_update', self.datetime, 900) + cache.set('mapdata:last_update', (self.pk, self.datetime), 900)