engine: cache EngineDimension.members (#38067)

This commit is contained in:
Benjamin Dauvergne 2019-11-29 15:27:22 +01:00
parent 33a8eabdba
commit a868f11d42
3 changed files with 26 additions and 2 deletions

View File

@ -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,

View File

@ -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]

View File

@ -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