Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Caching] Ensure cache is always created #9020

Merged
merged 3 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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

Expand Down Expand Up @@ -311,8 +312,8 @@ def _try_json_readsha(filepath, length): # pylint: disable=unused-argument
# 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
Expand Down Expand Up @@ -571,10 +572,6 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
SMTP_PASSWORD = "superset"
SMTP_MAIL_FROM = "superset@superset.com"

if not CACHE_DEFAULT_TIMEOUT:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is in the same block as line #313 and always evaluates to True and thus isn't required (hence the deletion).

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
Expand Down
22 changes: 22 additions & 0 deletions superset/typing.py
Original file line number Diff line number Diff line change
@@ -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]]
30 changes: 13 additions & 17 deletions superset/utils/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -27,30 +27,26 @@ def __init__(self) -> None:
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this was confusing logic

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
7 changes: 1 addition & 6 deletions tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,15 +822,10 @@ def test_parse_js_uri_path_items_item_optional(self):
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__)
Expand Down