From 0a422a74987d424d5261595e0af7b692350ad2f5 Mon Sep 17 00:00:00 2001 From: mike <219478+ilude@users.noreply.github.com> Date: Thu, 18 Apr 2024 18:54:41 -0400 Subject: [PATCH] caching and layout reloading --- app/app.py | 6 +++--- app/models/layout.py | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/app.py b/app/app.py index c7d8ad8..6f2e079 100644 --- a/app/app.py +++ b/app/app.py @@ -4,11 +4,10 @@ import signal import sys from datetime import datetime -from typing import Any from flask import Flask, render_template from flask_assets import Environment, Bundle from flask_caching import Cache - +from typing import Any from utils import copy_default_to_configs copy_default_to_configs() @@ -56,7 +55,8 @@ def inject_current_date(): @cache.cached(timeout=page_timeout, unless=lambda: layout.is_modified) def index(tab_name=None): # Load feeds and bookmarks - layout.reload() + if layout.is_modified(): + layout.reload() return render_template('index.html', layout=layout, tab_name=tab_name, skip_htmx=False) diff --git a/app/models/layout.py b/app/models/layout.py index 0e477af..dbb2ab7 100644 --- a/app/models/layout.py +++ b/app/models/layout.py @@ -7,23 +7,35 @@ import yaml from models.utils import from_list +logger = logging.getLogger(__name__) + class Layout: headers: list['Bookmark'] = [] tabs: list['Tab'] = [] def __init__(self, config_file: str = "configs/layout.yml"): + logger.setLevel(logging.DEBUG) self.config_path = pwd.joinpath(config_file) self.reload() + def stop_scheduler(self): scheduler = SchedulerWidget.getScheduler() if scheduler and scheduler.running: scheduler.shutdown() + def is_modified(self): - return self.mtime > self.last_reload + modified = self.mtime > self.last_reload + logger.debug(f"Layout modified: {modified} mtime: {self.mtime} last_reload: {self.last_reload}") + return modified + @property + def mtime(self): + return os.path.getmtime(self.config_path) + + def reload(self): from models.tab import Tab from models.Bookmark import Bookmark @@ -40,10 +52,6 @@ def reload(self): logging.debug("Layout reloaded!") - @property - def mtime(self): - return os.path.getmtime(self.config_path) - def tab(self, name: str) -> 'Tab': if name is None: @@ -78,5 +86,6 @@ def get_feed(self, feed_id: str) -> 'Feed': self.feed_hash[feed.id] = feed return self.feed_hash[feed_id] + layout = Layout() \ No newline at end of file