Skip to content

Commit

Permalink
drop original data when caching
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyielia committed Oct 24, 2023
1 parent 22bbbeb commit b7ddeaa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
40 changes: 20 additions & 20 deletions vizro-core/examples/default/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,30 @@ def delete_memoized_cache(delete_button_id_n_clicks):
)
dashboard = vm.Dashboard(pages=[page])

### when launching with gunicorn ###
Vizro._user_assets_folder = os.path.abspath("../assets")
Vizro._cache_config = {
"CACHE_TYPE": "FileSystemCache",
"CACHE_DIR": "cache",
"CACHE_THRESHOLD": 20, # The maximum number of items the cache can hold
"CACHE_DEFAULT_TIMEOUT": 3000, # Unit of time is seconds
}
# Vizro._cache_config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 120}
app = Vizro()
app.build(dashboard)
server = app.dash.server
### when launching with gunicorn ###
# ### when launching with gunicorn ###
# Vizro._user_assets_folder = os.path.abspath("../assets")
# data_manager._cache.config = {
# "CACHE_TYPE": "FileSystemCache",
# "CACHE_DIR": "cache",
# "CACHE_THRESHOLD": 20, # The maximum number of items the cache can hold
# "CACHE_DEFAULT_TIMEOUT": 3000, # Unit of time is seconds
# }
# # data_manager._cache.config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 120}
# app = Vizro()
# app.build(dashboard)
# server = app.dash.server
# ### when launching with gunicorn ###


if __name__ == "__main__":
Vizro._user_assets_folder = os.path.abspath("../assets")
Vizro._cache_config = {
"CACHE_TYPE": "FileSystemCache",
"CACHE_DIR": "cache",
"CACHE_THRESHOLD": 20, # The maximum number of items the cache can hold
"CACHE_DEFAULT_TIMEOUT": 3000, # Unit of time is seconds
}
# Vizro._cache_config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 3000,}
# data_manager._cache.config = {
# "CACHE_TYPE": "FileSystemCache",
# "CACHE_DIR": "cache",
# "CACHE_THRESHOLD": 20, # The maximum number of items the cache can hold
# "CACHE_DEFAULT_TIMEOUT": 3000, # Unit of time is seconds
# }
data_manager._cache.config = {"CACHE_TYPE": "RedisCache", "CACHE_REDIS_URL": "redis://localhost:6379/0", "CACHE_DEFAULT_TIMEOUT": 3000,}
Vizro().build(dashboard).run()
# Vizro().build(dashboard).run(
# threaded=False,
Expand Down
5 changes: 3 additions & 2 deletions vizro-core/src/vizro/_vizro.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Vizro:

_user_assets_folder = Path.cwd() / "assets"
_lib_assets_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
_cache_config = {"CACHE_TYPE": "NullCache"}

def __init__(self):
"""Initializes Dash."""
Expand All @@ -45,9 +44,11 @@ def build(self, dashboard: Dashboard):
Returns:
Vizro: App object
"""
data_manager._cache.init_app(self.dash.server, config=self._cache_config)
data_manager._cache.init_app(self.dash.server, config=data_manager._cache.config)
# Note that model instantiation and pre_build are independent of Dash.
self._pre_build()
# to clear original data if the cache type is not NullCache
data_manager._drop_original_data()

self.dash.layout = dashboard.build()

Expand Down
9 changes: 9 additions & 0 deletions vizro-core/src/vizro/managers/_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def _has_registered_data(self, component_id: ComponentID) -> bool:
def _clear(self):
self.__init__() # type: ignore[misc]

# to clear original data if the cache type is not NullCache
def _drop_original_data(self):
logger.debug(f"__original_data: {self.__original_data}")
logger.debug(f"config: {self._cache.config}")
logger.debug("drop original data")
if self._cache.config["CACHE_TYPE"] != "NullCache":
self.__original_data = {}
logger.debug(f"__original_data: {self.__original_data}")


data_manager = DataManager()

Expand Down

0 comments on commit b7ddeaa

Please sign in to comment.