5 Commits

3 changed files with 18 additions and 4 deletions

View File

@@ -77,3 +77,6 @@ The source code is similar to the django-rest-framework's generic classes and re
The problem arises in the way cache keys are created, the naive method is to just use the information from the url of the request and just save it to the cache. This creates a problem in that a request such as https://example.com/api/v1/pictures/search?search=mypic&category=mycat and a request https://example.com/api/v1/pictures/search?category=mycat&search=mypic contain the same information in their cache values. So the order of each filter or the order within the filters (such as the facet filter I made for e-commerce APIs) affects the caching behavior and creates more work for our APIs.
The way this module fixes the duplicate cache keys problem is by systematically ordering the filters through each filter's get_unique_dict method that are called in cache_mixins.py and then running the sorted_params_string utility function in the resulting dict.
# Why this instead of webserver caching
Using web server caching, in particular API microcaching, eg https://www.nginx.com/blog/benefits-of-microcaching-nginx/, is recommended to be used along with this library. This way the microcaching at the web server level manages the bulk of the caching while this cache that sits further back manages more flexible caching. This permits among others, runtime cache timeout configuration, handling of the duplicate cache keys problem above, more cache redundancy and more flexible and complicated network topologies for your servers.

View File

@@ -20,3 +20,8 @@ classifiers = [
[project.urls]
"Homepage" = "https://git.vickys-corner.xyz/ace/starfields-drf-generics"
[tool.setuptools.packages.find]
where = ["starfields_drf_generics"]
[tool.setuptools.package-data]
"templates.filters" = ["*.html"]

View File

@@ -2,7 +2,6 @@ from django_filters import rest_framework as filters
from django.contrib.postgres.search import TrigramSimilarity
from django.db.models.functions import Concat
from django.db.models import CharField
from shop.models.product import Product, Facet
from rest_framework.filters import BaseFilterBackend
import operator
from django.template import loader
@@ -216,13 +215,20 @@ class FacetFilter(BaseFilterBackend):
def assign_view_facets(self, request, view):
if not hasattr(view, 'facets'):
if hasattr(view, 'facet_class'):
self.facet_class = self.get_facet_class(view, request)
assert self.facet_class is not None, (
f"{view.__class__.__name__} should include a `facet_class` attribute"
)
if view.category:
if view.category.tn_ancestors_pks:
view.facets = Facet.objects.filter(Q(category__id=view.category.id) | Q(category__id__in=view.category.tn_ancestors_pks.split(','))).prefetch_related('facet_tags')
view.facets = self.facet_class.objects.filter(Q(category__id=view.category.id) | Q(category__id__in=view.category.tn_ancestors_pks.split(','))).prefetch_related('facet_tags')
else:
view.facets = Facet.objects.filter(category__id=view.category.id).prefetch_related('facet_tags')
view.facets = self.facet_class.objects.filter(category__id=view.category.id).prefetch_related('facet_tags')
else:
view.facets = Facet.objects.filter(category__tn_level=1).prefetch_related('facet_tags')
view.facets = self.facet_class.objects.filter(category__tn_level=1).prefetch_related('facet_tags')
def get_filters_dict(self, request, view):
"""