From 020c614d9da303790cae043debc71aafe8090bee Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 29 Nov 2019 15:24:35 +0100 Subject: [PATCH 05/13] utils: cache get_warehouses() result based on schema files paths (#38067) --- bijoe/utils.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/bijoe/utils.py b/bijoe/utils.py index 300ba26..4fb18e3 100644 --- a/bijoe/utils.py +++ b/bijoe/utils.py @@ -21,22 +21,39 @@ import json from django.conf import settings from django.db import connection from django.utils.translation import ugettext as _ +try: + from functools import lru_cache +except ImportError: + from django.utils.lru_cache import lru_cache + from .schemas import Warehouse -def get_warehouses(): - warehouses = [] +def get_warehouses_paths(): for pattern in settings.BIJOE_SCHEMAS: for path in glob.glob(pattern): - warehouses.append(Warehouse.from_json(json.load(open(path)))) + yield path if hasattr(connection, 'tenant'): pattern = os.path.join(connection.tenant.get_directory(), 'schemas', '*.model') for path in glob.glob(pattern): - warehouses.append(Warehouse.from_json(json.load(open(path)))) + yield path + + +@lru_cache() +def get_warehouses_by_paths(paths): + warehouses = [] + for path in paths: + d = json.load(open(path)) + warehouses.append(Warehouse.from_json(d)) return warehouses +def get_warehouses(): + paths = frozenset(get_warehouses_paths()) + return get_warehouses_by_paths(paths) + + def human_join(l): if not l: return '' -- 2.23.0