From f9313c6698281a02ce7a0bac4311b2b3750c4f58 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Fri, 26 Sep 2025 11:45:10 -0700 Subject: [PATCH] try flask caching to get around SQLAlchemy problem with cachetools --- app/job/rest.py | 16 +++++++--------- application.py | 4 ++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/job/rest.py b/app/job/rest.py index 5896cbcb2..813cf666b 100644 --- a/app/job/rest.py +++ b/app/job/rest.py @@ -3,9 +3,7 @@ from zoneinfo import ZoneInfo import dateutil from flask import Blueprint, current_app, jsonify, request -from flask_caching import Cache -import application from app import db from app.aws.s3 import ( extract_personalisation, @@ -43,18 +41,18 @@ from app.schemas import ( notifications_filter_schema, ) from app.utils import check_suspicious_id, hilite, midnight_n_days_ago, pagination_links +from application import rest_cache job_blueprint = Blueprint("job", __name__, url_prefix="/service//job") -cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) -cache.init_app(application) + register_errors(job_blueprint) cache_timeout = os.getenv("REST_CACHE_TIMEOUT", 10) @job_blueprint.route("/", methods=["GET"]) -@cache.cached(timeout=cache_timeout) +@rest_cache.cached(timeout=cache_timeout) def get_job_by_service_and_job_id(service_id, job_id): current_app.logger.info(hilite("ENTER get_job_by_service_and_job_id")) check_suspicious_id(service_id, job_id) @@ -81,7 +79,7 @@ def cancel_job(service_id, job_id): @job_blueprint.route("//notifications", methods=["GET"]) -@cache.cached(timeout=cache_timeout) +@rest_cache.cached(timeout=cache_timeout) def get_all_notifications_for_service_job(service_id, job_id): check_suspicious_id(service_id, job_id) @@ -142,7 +140,7 @@ def get_all_notifications_for_service_job(service_id, job_id): @job_blueprint.route("//recent_notifications", methods=["GET"]) -@cache.cached(timeout=cache_timeout) +@rest_cache.cached(timeout=cache_timeout) def get_recent_notifications_for_service_job(service_id, job_id): current_app.logger.info(hilite("ENTER get_recent_notifications_for_service_job")) @@ -212,7 +210,7 @@ def get_recent_notifications_for_service_job(service_id, job_id): @job_blueprint.route("//notification_count", methods=["GET"]) -@cache.cached(timeout=cache_timeout) +@rest_cache.cached(timeout=cache_timeout) def get_notification_count_for_job_id(service_id, job_id): check_suspicious_id(service_id, job_id) @@ -222,7 +220,7 @@ def get_notification_count_for_job_id(service_id, job_id): @job_blueprint.route("", methods=["GET"]) -@cache.cached(timeout=cache_timeout) +@rest_cache.cached(timeout=cache_timeout) def get_jobs_by_service(service_id): check_suspicious_id(service_id) if request.args.get("limit_days"): diff --git a/application.py b/application.py index c674e7ae6..13bcfef95 100644 --- a/application.py +++ b/application.py @@ -3,6 +3,7 @@ from __future__ import print_function from flask import Flask # noqa +from flask_caching import Cache from werkzeug.serving import WSGIRequestHandler # noqa from app import create_app, socketio # noqa @@ -12,3 +13,6 @@ WSGIRequestHandler.version_string = lambda self: "SecureServer" application = Flask("app") create_app(application) + +rest_cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) +rest_cache.init_app(application)