Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cache keys #128

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 197 in geostore/tiles/helpers.py

View check run for this annotation

Codecov / codecov/patch

geostore/tiles/helpers.py#L197

Added line #L197 was not covered by tests

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)

Check warning on line 201 in geostore/tiles/helpers.py

View check run for this annotation

Codecov / codecov/patch

geostore/tiles/helpers.py#L201

Added line #L201 was not covered by tests

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