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

PNI and Redis cache optimization #12470

Merged
merged 7 commits into from
Jun 11, 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
21 changes: 5 additions & 16 deletions network-api/networkapi/templates/pages/buyersguide/catalog.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "pages/buyersguide/base.html" %}

{% load static env i18n static wagtailimages_tags wagtailroutablepage_tags wagtailcore_tags cache bg_nav_tags localization %}
{% load static env i18n static wagtailimages_tags wagtailroutablepage_tags wagtailcore_tags bg_nav_tags localization %}

{% block extended_head %}
<noscript>
Expand Down Expand Up @@ -145,21 +145,10 @@

{% block extra_product_box_list_items %}{% endblock extra_product_box_list_items %}

{% if request.user.is_anonymous %}
{# User is not logged in. Return cached results. 24 hour caching applied. #}
{% cache 86400 pni_home_page template_cache_key_fragment %}
{% for product in products %}
{% product_in_category product category as matched %}
{% include "fragments/buyersguide/item.html" with product=product matched=matched %}
{% endfor %}
{% endcache %}
{% else %}
{# User is logged in. Don't cache their results so they can see live and draft products here. #}
{% for product in products %}
{% product_in_category product category as matched %}
{% include "fragments/buyersguide/item.html" with product=product matched=matched %}
{% endfor %}
{% endif %}
{% for product in products %}
{% product_in_category product category as matched %}
{% include "fragments/buyersguide/item.html" with product=product matched=matched %}
{% endfor %}
</div>

<div id="product-filter-no-results-notice" class="d-none text-center my-5 py-5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from django.apps import apps
from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.text import slugify
Expand Down Expand Up @@ -267,7 +266,7 @@ def product_view(self, request, slug):

@route(r"^categories/(?P<slug>[\w\W]+)/", name="category-view")
def categories_page(self, request, slug):
context = self.get_context(request, bypass_products=True)
context = self.get_context(request)
language_code = get_language_from_request(request)
slug = slugify(slug)

Expand All @@ -291,26 +290,20 @@ def categories_page(self, request, slug):
category.parent = category.parent.localized

authenticated = request.user.is_authenticated
key = f"cat_product_dicts_{slug}_auth" if authenticated else f"cat_product_dicts_{slug}_live"
key = f"{language_code}_{key}"
products = cache.get(key)
exclude_cat_ids = [excats.category.id for excats in self.excluded_categories.all()]

ProductPage = apps.get_model(app_label="wagtailpages", model_name="ProductPage")
if products is None:
products = bg_utils.get_product_subset(
self.cutoff_date,
authenticated,
key,
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code,
)
products = bg_utils.get_product_subset(
self.cutoff_date,
authenticated,
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code,
)

context["category"] = slug
context["current_category"] = category
context["products"] = products
context["pageTitle"] = f'{category.name} | {gettext("Privacy & Security Guide")}' f" | Mozilla Foundation"
context["template_cache_key_fragment"] = f"{category.slug}_{request.LANGUAGE_CODE}"

# Checking if category has custom metadata, if so, update the share image and description.
if category.share_image:
Expand Down Expand Up @@ -355,31 +348,24 @@ def get_sitemap_urls(self, request):
return sitemap

def get_context(self, request, *args, **kwargs):
bypass_products = kwargs.pop("bypass_products", False)
context = super().get_context(request, *args, **kwargs)
language_code = get_language_from_request(request)

authenticated = request.user.is_authenticated
key = "home_product_dicts_authed" if authenticated else "home_product_dicts_live"
key = f"{key}_{language_code}"
products = cache.get(key)
exclude_cat_ids = [excats.category.id for excats in self.excluded_categories.all()]

ProductPage = apps.get_model(app_label="wagtailpages", model_name="ProductPage")
if not bypass_products and products is None:
products = bg_utils.get_product_subset(
self.cutoff_date,
authenticated,
key,
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code,
)
products = bg_utils.get_product_subset(
self.cutoff_date,
authenticated,
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code,
)

context["current_category"] = None
context["featured_cta"] = self.call_to_action
context["products"] = products
context["web_monetization_pointer"] = settings.WEB_MONETIZATION_POINTER
context["template_cache_key_fragment"] = f"pni_home_{request.LANGUAGE_CODE}"
return context

def get_editorial_content_index(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.apps import apps
from django.core.cache import cache

from networkapi.wagtailpages.utils import localize_queryset

Expand All @@ -15,7 +14,7 @@ def get_buyersguide_featured_cta(page):
return featured_cta


def get_product_subset(cutoff_date, authenticated, key, products, language_code="en"):
def get_product_subset(cutoff_date, authenticated, products, language_code="en"):
"""
filter a queryset based on our current cutoff date,
as well as based on whether a user is authenticated
Expand All @@ -38,7 +37,6 @@ def get_product_subset(cutoff_date, authenticated, key, products, language_code=

products = annotate_product_categories_local_names(products, language_code)

cache.get_or_set(key, products, 24 * 60 * 60) # Set cache for 24h
return products


Expand Down
Loading