Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyjd committed Oct 15, 2018
1 parent 0725e5f commit 488bdff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
7 changes: 3 additions & 4 deletions superset/assets/src/SqlLab/components/SqlEditorLeftBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class SqlEditorLeftBar extends React.PureComponent {
}
onDatabaseChange(db, force) {
const val = db ? db.value : null;
this.setState({ schemaOptions: [] });
this.setState({ tableOptions: [] });
this.setState({ schemaOptions: [], tableOptions: [] });
this.props.actions.queryEditorSetSchema(this.props.queryEditor, null);
this.props.actions.queryEditorSetDb(this.props.queryEditor, val);
if (db) {
Expand Down Expand Up @@ -195,7 +194,7 @@ class SqlEditorLeftBar extends React.PureComponent {
<RefreshLabel
onClick={this.onDatabaseChange.bind(
this, { value: database.id }, true)}
tooltipContent="force refresh table list"
tooltipContent={t('force refresh schema list')}
/>
</div>
</div>
Expand Down Expand Up @@ -244,7 +243,7 @@ class SqlEditorLeftBar extends React.PureComponent {
<RefreshLabel
onClick={this.changeSchema.bind(
this, { value: this.props.queryEditor.schema }, true)}
tooltipContent="force refresh schema list"
tooltipContent={t('force refresh table list')}
/>
</div>
</div>
Expand Down
31 changes: 12 additions & 19 deletions superset/cache_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@ def view_cache_key(*unused_args, **unused_kwargs):
return 'view/{}/{}'.format(request.path, args_hash)


def default_timeout(*unused_args, **unused_kwargs):
return 5 * 60


def default_enable_cache(*unused_args, **unused_kwargs):
return True
def memoized_func(key=view_cache_key, use_tables_cache=False):
"""Use this decorator to cache functions that have predefined first arg.
enable_cache is treated as True by default,
except enable_cache = False is passed to the decorated function.
def memoized_func(timeout=default_timeout,
key=view_cache_key,
enable_cache=default_enable_cache,
use_tables_cache=False):
"""Use this decorator to cache functions that have predefined first arg.
force is treated as False by default,
except force = True is passed to the decorated function.
If enable_cache() is False,
the function will never be cached.
If enable_cache() is True,
cache is adopted and will timeout in timeout() seconds.
If force is True, cache will be refreshed.
timeout of cache is set to 600 seconds by default,
except cache_timeout = {timeout in seconds} is passed to the decorated function.
memoized_func uses simple_cache and stored the data in memory.
Key is a callable function that takes function arguments and
Expand All @@ -42,15 +34,16 @@ def wrap(f):

if selected_cache:
def wrapped_f(cls, *args, **kwargs):
if not enable_cache(*args, **kwargs):
if not kwargs.get('enable_cache', True):
return f(cls, *args, **kwargs)

cache_key = key(*args, **kwargs)
o = selected_cache.get(cache_key)
if not kwargs['force'] and o is not None:
if not kwargs.get('force') and o is not None:
return o
o = f(cls, *args, **kwargs)
selected_cache.set(cache_key, o, timeout=timeout(*args, **kwargs))
selected_cache.set(cache_key, o,
timeout=kwargs.get('cache_timeout', 600))
return o
else:
# noop
Expand Down
16 changes: 0 additions & 16 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ def convert_dttm(cls, target_type, dttm):

@classmethod
@cache_util.memoized_func(
timeout=600,
key=lambda *args, **kwargs: 'db:{}:{}'.format(args[0].id, args[1]),
use_tables_cache=True)
def fetch_result_sets(cls, db, datasource_type, force=False):
Expand Down Expand Up @@ -295,8 +294,6 @@ def patch(cls):

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{}:schema_list'.format(kwargs.get('db_id')))
def get_schema_names(cls, inspector, db_id,
enable_cache, cache_timeout, force=False):
Expand All @@ -313,8 +310,6 @@ def get_schema_names(cls, inspector, db_id,

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{db_id}:schema:{schema}:table_list'.format(
db_id=kwargs.get('db_id'), schema=kwargs.get('schema')))
def get_table_names(cls, inspector, db_id, schema,
Expand All @@ -323,8 +318,6 @@ def get_table_names(cls, inspector, db_id, schema,

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{db_id}:schema:{schema}:view_list'.format(
db_id=kwargs.get('db_id'), schema=kwargs.get('schema')))
def get_view_names(cls, inspector, db_id, schema,
Expand Down Expand Up @@ -455,8 +448,6 @@ class PostgresEngineSpec(PostgresBaseEngineSpec):

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{db_id}:schema:{schema}:table_list'.format(
db_id=kwargs.get('db_id'), schema=kwargs.get('schema')))
def get_table_names(cls, inspector, db_id, schema,
Expand Down Expand Up @@ -592,7 +583,6 @@ def epoch_to_dttm(cls):

@classmethod
@cache_util.memoized_func(
timeout=600,
key=lambda *args, **kwargs: 'db:{}:{}'.format(args[0].id, args[1]),
use_tables_cache=True)
def fetch_result_sets(cls, db, datasource_type, force=False):
Expand All @@ -619,8 +609,6 @@ def convert_dttm(cls, target_type, dttm):

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{db_id}:schema:{schema}:table_list'.format(
db_id=kwargs.get('db_id'), schema=kwargs.get('schema')))
def get_table_names(cls, inspector, db_id, schema,
Expand Down Expand Up @@ -749,7 +737,6 @@ def epoch_to_dttm(cls):

@classmethod
@cache_util.memoized_func(
timeout=600,
key=lambda *args, **kwargs: 'db:{}:{}'.format(args[0].id, args[1]),
use_tables_cache=True)
def fetch_result_sets(cls, db, datasource_type, force=False):
Expand Down Expand Up @@ -1031,7 +1018,6 @@ def patch(cls):

@classmethod
@cache_util.memoized_func(
timeout=600,
key=lambda *args, **kwargs: 'db:{}:{}'.format(args[0].id, args[1]),
use_tables_cache=True)
def fetch_result_sets(cls, db, datasource_type, force=False):
Expand Down Expand Up @@ -1489,8 +1475,6 @@ def convert_dttm(cls, target_type, dttm):

@classmethod
@cache_util.memoized_func(
enable_cache=lambda *args, **kwargs: kwargs.get('enable_cache', False),
timeout=lambda *args, **kwargs: kwargs.get('cache_timeout'),
key=lambda *args, **kwargs: 'db:{}:schema_list'.format(kwargs.get('db_id')))
def get_schema_names(cls, inspector, db_id,
enable_cache, cache_timeout, force=False):
Expand Down

0 comments on commit 488bdff

Please sign in to comment.