0006-engine-cache-EngineDimension.members-38067.patch
bijoe/engine.py | ||
---|---|---|
17 | 17 |
import contextlib |
18 | 18 |
import logging |
19 | 19 |
import itertools |
20 |
import hashlib |
|
20 | 21 | |
21 | 22 |
import collections |
22 | 23 |
import psycopg2 |
23 | 24 | |
25 |
from django.core.cache import cache |
|
24 | 26 | |
25 | 27 |
from . import schemas |
26 | 28 | |
... | ... | |
61 | 63 |
def __getattr__(self, name): |
62 | 64 |
return getattr(self.dimension, name) |
63 | 65 | |
66 |
@property |
|
67 |
def cache_key(self): |
|
68 |
return hashlib.md5(self.engine.path + self.engine_cube.name + self.name).hexdigest() |
|
69 | ||
64 | 70 |
@property |
65 | 71 |
def members(self): |
66 | 72 |
assert self.type != 'date' |
73 | ||
74 |
members = cache.get(self.cache_key) |
|
75 |
if members is not None: |
|
76 |
return members |
|
77 | ||
78 |
members = [] |
|
79 | ||
67 | 80 |
value = self.value |
68 | 81 |
value_label = self.value_label or value |
69 | 82 |
order_by = self.order_by |
... | ... | |
96 | 109 |
for row in cursor.fetchall(): |
97 | 110 |
if row[0] is None: |
98 | 111 |
continue |
99 |
yield Member(*row) |
|
112 |
members.append(Member(*row)) |
|
113 |
cache.set(self.cache_key, members, 600) |
|
114 |
return members |
|
100 | 115 | |
101 | 116 | |
102 | 117 |
class SchemaJSONDimension(schemas.Dimension): |
... | ... | |
132 | 147 |
self.engine_cube = engine_cube |
133 | 148 |
self.dimension = SchemaJSONDimension(self.engine_cube.json_field, name) |
134 | 149 | |
150 |
@property |
|
151 |
def cache_key(self): |
|
152 |
return hashlib.md5(self.engine.path + self.engine_cube.json_field |
|
153 |
+ self.engine_cube.name + self.name).hexdigest() |
|
154 | ||
135 | 155 |
def to_json(self): |
136 | 156 |
return { |
137 | 157 |
'name': self.name, |
bijoe/schemas.py | ||
---|---|---|
379 | 379 | |
380 | 380 | |
381 | 381 |
class Warehouse(Base): |
382 |
__slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes'] |
|
382 |
__slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes', 'path']
|
|
383 | 383 |
__types__ = { |
384 | 384 |
'name': str, |
385 | 385 |
'label': unicode, |
... | ... | |
387 | 387 |
'search_path': [str], |
388 | 388 |
'cubes': [Cube], |
389 | 389 |
'search_path': [str], |
390 |
'path': str, |
|
390 | 391 |
} |
391 | 392 | |
393 |
path = None |
|
394 | ||
392 | 395 |
def check(self): |
393 | 396 |
names = collections.Counter(cube.name for cube in self.cubes) |
394 | 397 |
duplicates = [k for k, v in names.iteritems() if v > 1] |
bijoe/utils.py | ||
---|---|---|
45 | 45 |
warehouses = [] |
46 | 46 |
for path in paths: |
47 | 47 |
d = json.load(open(path)) |
48 |
d['path'] = path |
|
48 | 49 |
warehouses.append(Warehouse.from_json(d)) |
49 | 50 |
return warehouses |
50 | 51 | |
51 |
- |