Cache the contents of package.json once loaded, instead of reading the file every time it's required

This commit is contained in:
Danielle McLean 2017-10-23 13:08:11 +11:00
parent 97948b957d
commit f0846d8c73
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5

View file

@ -1,17 +1,21 @@
import json import json
from os.path import join from os.path import join
from types import SimpleNamespace
from django import template from django import template
from django.conf import settings from django.conf import settings
register = template.Library() register = template.Library()
cache = SimpleNamespace(package_json=None)
@register.simple_tag @register.simple_tag
def get_package_json(): def get_package_json():
if cache.package_json:
return cache.package_json
with open(join(settings.BASE_DIR, 'package.json')) as f: with open(join(settings.BASE_DIR, 'package.json')) as f:
package = json.load(f) cache.package_json = json.load(f)
return package return cache.package_json
@register.simple_tag @register.simple_tag