From f0846d8c733855e422a7b67b30e8f1eaa2811ab8 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Mon, 23 Oct 2017 13:08:11 +1100 Subject: [PATCH] Cache the contents of package.json once loaded, instead of reading the file every time it's required --- lemoncurry/templatetags/lemoncurry_tags.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lemoncurry/templatetags/lemoncurry_tags.py b/lemoncurry/templatetags/lemoncurry_tags.py index ed8b6ff..423f330 100644 --- a/lemoncurry/templatetags/lemoncurry_tags.py +++ b/lemoncurry/templatetags/lemoncurry_tags.py @@ -1,17 +1,21 @@ import json from os.path import join +from types import SimpleNamespace from django import template from django.conf import settings register = template.Library() +cache = SimpleNamespace(package_json=None) @register.simple_tag def get_package_json(): + if cache.package_json: + return cache.package_json with open(join(settings.BASE_DIR, 'package.json')) as f: - package = json.load(f) - return package + cache.package_json = json.load(f) + return cache.package_json @register.simple_tag