utils: cache warehouses (#27412)

Disk reads take a long long time.
This commit is contained in:
Valentin Deniaud 2019-11-21 11:54:21 +01:00
parent 7a5373c91e
commit ba2c0c4fab
2 changed files with 6 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import glob
import json
from django.conf import settings
from django.core.cache import cache
from django.db import connection
from django.utils.translation import ugettext as _
@ -26,6 +27,8 @@ from .schemas import Warehouse
def get_warehouses():
if cache.get('warehouses_cache'):
return cache.get('warehouses_cache')
warehouses = []
for pattern in settings.BIJOE_SCHEMAS:
for path in glob.glob(pattern):
@ -34,6 +37,7 @@ def get_warehouses():
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))))
cache.set('warehouses_cache', warehouses, 30)
return warehouses

View File

@ -15,6 +15,7 @@ import psycopg2
from django.db import connection
from django.contrib.auth.models import User
from django.core.cache import cache
from django.core.management import call_command
@ -52,6 +53,7 @@ SCHEMA_PATHS = os.path.join(os.path.dirname(__file__), 'fixtures/')
def load_schema_db(schema):
import random
cache.delete('warehouses_cache')
database_name = 'db%s' % random.getrandbits(20)
tmpdir = tempfile.mkdtemp()
bijoe_schema_dir = os.path.join(tmpdir, 'schemas')