[Caching] Ensure cache is always created (#9020)

* [Caching] Ensure cache is always created

* Update cache_manager.py

* Refactoring cache typing
This commit is contained in:
Erik Ritter 2020-01-25 14:49:05 -08:00 committed by Daniel Vaz Gaspar
parent c552c852cd
commit 68e85ab1b6
4 changed files with 39 additions and 29 deletions

View File

@ -35,6 +35,7 @@ from dateutil import tz
from flask_appbuilder.security.manager import AUTH_DB
from superset.stats_logger import DummyStatsLogger
from superset.typing import CacheConfig
from superset.utils.log import DBEventLogger
from superset.utils.logging_configurator import DefaultLoggingConfigurator
@ -311,8 +312,8 @@ IMG_UPLOAD_URL = "/static/uploads/"
# IMG_SIZE = (300, 200, True)
CACHE_DEFAULT_TIMEOUT = 60 * 60 * 24
CACHE_CONFIG: Dict[str, Any] = {"CACHE_TYPE": "null"}
TABLE_NAMES_CACHE_CONFIG = {"CACHE_TYPE": "null"}
CACHE_CONFIG: CacheConfig = {"CACHE_TYPE": "null"}
TABLE_NAMES_CACHE_CONFIG: CacheConfig = {"CACHE_TYPE": "null"}
# CORS Options
ENABLE_CORS = False
@ -571,10 +572,6 @@ SMTP_PORT = 25
SMTP_PASSWORD = "superset"
SMTP_MAIL_FROM = "superset@superset.com"
if not CACHE_DEFAULT_TIMEOUT:
CACHE_DEFAULT_TIMEOUT = CACHE_CONFIG["CACHE_DEFAULT_TIMEOUT"]
ENABLE_CHUNK_ENCODING = False
# Whether to bump the logging level to ERROR on the flask_appbuilder package

22
superset/typing.py Normal file
View File

@ -0,0 +1,22 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any, Callable, Dict, Union
from flask import Flask
from flask_caching import Cache
CacheConfig = Union[Callable[[Flask], Cache], Dict[str, Any]]

View File

@ -14,11 +14,11 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Optional
from flask import Flask
from flask_caching import Cache
from superset.typing import CacheConfig
class CacheManager:
def __init__(self) -> None:
@ -27,30 +27,26 @@ class CacheManager:
self._tables_cache = None
self._cache = None
def init_app(self, app):
self._cache = self._setup_cache(app, app.config.get("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.get("TABLE_NAMES_CACHE_CONFIG")
app, app.config["TABLE_NAMES_CACHE_CONFIG"]
)
@staticmethod
def _setup_cache(app: Flask, cache_config) -> Optional[Cache]:
def _setup_cache(app: Flask, cache_config: CacheConfig) -> Cache:
"""Setup the flask-cache on a flask app"""
if cache_config:
if isinstance(cache_config, dict):
if cache_config.get("CACHE_TYPE") != "null":
return Cache(app, config=cache_config)
else:
# Accepts a custom cache initialization function,
# returning an object compatible with Flask-Caching API
return cache_config(app)
if isinstance(cache_config, dict):
return Cache(app, config=cache_config)
return None
# Accepts a custom cache initialization function, returning an object compatible
# with Flask-Caching API.
return cache_config(app)
@property
def tables_cache(self):
def tables_cache(self) -> Cache:
return self._tables_cache
@property
def cache(self):
def cache(self) -> Cache:
return self._cache

View File

@ -822,15 +822,10 @@ class UtilsTestCase(SupersetTestCase):
self.assertIsNone(parse_js_uri_path_item(None))
self.assertIsNotNone(parse_js_uri_path_item("item"))
def test_setup_cache_no_config(self):
app = Flask(__name__)
cache_config = None
self.assertIsNone(CacheManager._setup_cache(app, cache_config))
def test_setup_cache_null_config(self):
app = Flask(__name__)
cache_config = {"CACHE_TYPE": "null"}
self.assertIsNone(CacheManager._setup_cache(app, cache_config))
assert isinstance(CacheManager._setup_cache(app, cache_config), Cache)
def test_setup_cache_standard_config(self):
app = Flask(__name__)