Skip to content

Commit

Permalink
chore: Using cache factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
John Bodley committed Sep 15, 2020
1 parent 38edb69 commit f4db831
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion superset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# then initialize it in app.create_app(). These fields will be removed
# in subsequent PRs as things are migrated towards the factory pattern
app: Flask = current_app
cache = LocalProxy(lambda: cache_manager.cache)
cache = cache_manager.cache
conf = LocalProxy(lambda: current_app.config)
get_feature_flags = feature_flag_manager.get_feature_flags
get_manifest_files = manifest_processor.get_manifest_files
Expand Down
22 changes: 9 additions & 13 deletions superset/utils/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,24 @@ class CacheManager:
def __init__(self) -> None:
super().__init__()

self._tables_cache = None
self._cache = None
self._thumbnail_cache = None
self._cache = self._setup_cache(app.config["CACHE_CONFIG"])
self._tables_cache = self._setup_cache(app.config["TABLE_NAMES_CACHE_CONFIG"])
self._thumbnail_cache = self._setup_cache(app.config["THUMBNAIL_CACHE_CONFIG"])

def init_app(self, app: Flask) -> None:
self._cache = self._setup_cache(app, app.config["CACHE_CONFIG"])
self._tables_cache = self._setup_cache(
app, app.config["TABLE_NAMES_CACHE_CONFIG"]
)
self._thumbnail_cache = self._setup_cache(
app, app.config["THUMBNAIL_CACHE_CONFIG"]
)
self._cache.init_app(app)
self._tables_cache.init_app(app)
self._thumbnail_cache.init_app(app)

@staticmethod
def _setup_cache(app: Flask, cache_config: CacheConfig) -> Cache:
def _setup_cache(cache_config: CacheConfig) -> Cache:
"""Setup the flask-cache on a flask app"""
if isinstance(cache_config, dict):
return Cache(app, config=cache_config)
return Cache(config=cache_config)

# Accepts a custom cache initialization function, returning an object compatible
# with Flask-Caching API.
return cache_config(app)
return cache_config()

@property
def tables_cache(self) -> Cache:
Expand Down

0 comments on commit f4db831

Please sign in to comment.