Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
[Caching] Ensure cache is always created (apache#9020)
Browse files Browse the repository at this point in the history
* [Caching] Ensure cache is always created

* Update cache_manager.py

* Refactoring cache typing

(cherry picked from commit 68e85ab)
  • Loading branch information
Erik Ritter authored and John Bodley committed Jan 26, 2020
1 parent c732690 commit e7bad6a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
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 = "[email protected]"

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
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):
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 @@ -821,15 +821,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

0 comments on commit e7bad6a

Please sign in to comment.