Skip to content

Commit

Permalink
Remove caching features from PNI
Browse files Browse the repository at this point in the history
  • Loading branch information
robdivincenzo committed Jun 10, 2024
1 parent a052e54 commit 70f0661
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
22 changes: 6 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,11 @@

{% 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 %}
{# 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 %}
</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 @@ -267,7 +267,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 +291,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 +349,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
Expand Up @@ -15,7 +15,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 +38,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

0 comments on commit 70f0661

Please sign in to comment.