Skip to content

Commit

Permalink
Merge pull request #128 from Terralego/fix_cache_key
Browse files Browse the repository at this point in the history
Improve cache keys
  • Loading branch information
submarcos authored Jan 12, 2024
2 parents 18fa99b + a3a79b4 commit 62f6599
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

0.8.0+dev (XXXX-XX-XX)
---------------------------

* Fix tilejson cache key with hexdigest


0.8.0 (2023-09-08)
---------------------------

Expand Down
2 changes: 1 addition & 1 deletion geostore/VERSION.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.0
0.8.0+dev
24 changes: 10 additions & 14 deletions geostore/tiles/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import hashlib
from hashlib import sha224
from random import uniform

import mercantile
Expand Down Expand Up @@ -192,23 +192,19 @@ def get_tile_cache_key(self, x, y, z):
else:
cache_key = self.layer.pk

features_filter = ''
if features_filter is not None:
features_filter_hash = \
hashlib.sha224(
str(features_filter).encode('utf-8')
).hexdigest()
features_filter_hash = ''
if self.features_filter is not None:
features_filter_hash = str(self.features_filter)

properties_filter_hash = ''
if self.properties_filter is not None:
properties_filter_hash = \
hashlib.sha224(
','.join(self.properties_filter).encode('utf-8')
).hexdigest()
return (
properties_filter_hash = ','.join(self.properties_filter)

return sha224(
f'tile_cache_{cache_key}_{x}_{y}_{z}'
f'_{self.pixel_buffer}_{features_filter_hash}_{properties_filter_hash}'
f'_{self.features_limit}'
)
f'_{self.features_limit}'.encode()
).hexdigest()


def guess_maxzoom(layer):
Expand Down
5 changes: 4 additions & 1 deletion geostore/tiles/mixins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from hashlib import sha224
from urllib.parse import unquote, urljoin

from django.core.cache import cache
Expand Down Expand Up @@ -53,7 +54,9 @@ def tiles_pattern(self, request, *args, **kwargs):
def tilejson(self, request, *args, **kwargs):
""" MVT layer tilejson """
last_update = self.get_last_update()
cache_key = f'tilejson-{self.get_object().name}' + '-'.join([g.name for g in self.authenticated_groups])
cache_key = sha224(
f"tilejson-{self.get_object().name}{'-'.join([g.name for g in self.authenticated_groups])}".encode()
).hexdigest()
version = int(last_update.timestamp())
tilejson_data = cache.get(cache_key, version=version)

Expand Down

0 comments on commit 62f6599

Please sign in to comment.