From 3999f42e5a2ca91dc86fe4cc7c03f923fa8849cd Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 29 Nov 2019 15:27:22 +0100 Subject: [PATCH 06/13] engine: cache EngineDimension.members (#38067) --- bijoe/engine.py | 22 +++++++++++++++++++++- bijoe/schemas.py | 5 ++++- bijoe/utils.py | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bijoe/engine.py b/bijoe/engine.py index 765f5ee..a8a7b89 100644 --- a/bijoe/engine.py +++ b/bijoe/engine.py @@ -17,10 +17,12 @@ import contextlib import logging import itertools +import hashlib import collections import psycopg2 +from django.core.cache import cache from . import schemas @@ -61,9 +63,20 @@ class EngineDimension(object): def __getattr__(self, name): return getattr(self.dimension, name) + @property + def cache_key(self): + return hashlib.md5(self.engine.path + self.engine_cube.name + self.name).hexdigest() + @property def members(self): assert self.type != 'date' + + members = cache.get(self.cache_key) + if members is not None: + return members + + members = [] + value = self.value value_label = self.value_label or value order_by = self.order_by @@ -96,7 +109,9 @@ class EngineDimension(object): for row in cursor.fetchall(): if row[0] is None: continue - yield Member(*row) + members.append(Member(*row)) + cache.set(self.cache_key, members, 600) + return members class SchemaJSONDimension(schemas.Dimension): @@ -132,6 +147,11 @@ class EngineJSONDimension(EngineDimension): self.engine_cube = engine_cube self.dimension = SchemaJSONDimension(self.engine_cube.json_field, name) + @property + def cache_key(self): + return hashlib.md5(self.engine.path + self.engine_cube.json_field + + self.engine_cube.name + self.name).hexdigest() + def to_json(self): return { 'name': self.name, diff --git a/bijoe/schemas.py b/bijoe/schemas.py index 856f0ab..1705384 100644 --- a/bijoe/schemas.py +++ b/bijoe/schemas.py @@ -379,7 +379,7 @@ class Cube(Base): class Warehouse(Base): - __slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes'] + __slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes', 'path'] __types__ = { 'name': str, 'label': unicode, @@ -387,8 +387,11 @@ class Warehouse(Base): 'search_path': [str], 'cubes': [Cube], 'search_path': [str], + 'path': str, } + path = None + def check(self): names = collections.Counter(cube.name for cube in self.cubes) duplicates = [k for k, v in names.iteritems() if v > 1] diff --git a/bijoe/utils.py b/bijoe/utils.py index 4fb18e3..a4afa53 100644 --- a/bijoe/utils.py +++ b/bijoe/utils.py @@ -45,6 +45,7 @@ def get_warehouses_by_paths(paths): warehouses = [] for path in paths: d = json.load(open(path)) + d['path'] = path warehouses.append(Warehouse.from_json(d)) return warehouses -- 2.23.0