Skip to content

Commit

Permalink
Add ansible api hostname to api cache key.
Browse files Browse the repository at this point in the history
fixes #1833
  • Loading branch information
newswangerd committed May 2, 2024
1 parent 3212416 commit ca866f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/1833.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ansible hostname to API v3 cache key
41 changes: 27 additions & 14 deletions pulp_ansible/app/galaxy/v3/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from gettext import gettext as _
import semantic_version
import base64

from django.db import DatabaseError, IntegrityError
from django.db.models import F, OuterRef, Exists, Subquery, Prefetch
Expand Down Expand Up @@ -97,6 +98,15 @@
}


def encode_cache_key(key):
"""
Base64 encode the cache key to avoid any unpleasantness with unsupported
characters.
"""
b64 = base64.b64encode(key.encode())
return b64.decode()


class AnsibleDistributionMixin:
"""
A mixin for ViewSets that use AnsibleDistribution.
Expand Down Expand Up @@ -925,14 +935,17 @@ def list(self, request, *args, **kwargs):
cache_request = False
break

cache_key = "".join(
[
str(self._repository_version.pk),
kwargs["namespace"],
kwargs["name"],
str(request.query_params.get("offset", "0")),
str(request.query_params.get("limit", "0")),
]
cache_key = encode_cache_key(
"-".join(
[
settings.ANSIBLE_API_HOSTNAME,
str(self._repository_version.pk),
kwargs["namespace"],
kwargs["name"],
str(request.query_params.get("offset", "0")),
str(request.query_params.get("limit", "0")),
]
)
)

if cache_key in cache and cache_request:
Expand Down Expand Up @@ -976,9 +989,9 @@ def retrieve(self, request, *args, **kwargs):
repo_version = self._repository_version
# the contents of a repo version can be cached without worry because repo
# versions are immutable
cache_key = (
f"version-details-{repo_version.pk}"
f"{kwargs['namespace']}{kwargs['name']}{kwargs['version']}"
cache_key = encode_cache_key(
f"{settings.ANSIBLE_API_HOSTNAME}-version-details-{repo_version.pk}"
f"-{kwargs['namespace']}-{kwargs['name']}-{kwargs['version']}"
)

if data := cache.get(cache_key):
Expand Down Expand Up @@ -1122,9 +1135,9 @@ def retrieve(self, request, *args, **kwargs):
repo_version = self._repository_version
# the contents of a repo version can be cached without worry because repo
# versions are immutable
cache_key = (
f"version-docs-{repo_version.pk}"
f"{kwargs['namespace']}{kwargs['name']}{kwargs['version']}"
cache_key = encode_cache_key(
f"version-docs-{repo_version.pk}-"
f"{kwargs['namespace']}-{kwargs['name']}-{kwargs['version']}"
)

if data := cache.get(cache_key):
Expand Down

0 comments on commit ca866f8

Please sign in to comment.