From eeda69db8799efe6661f3234a817e6fc3cb45348 Mon Sep 17 00:00:00 2001 From: Zach Legesse Date: Thu, 24 Oct 2024 23:16:53 -0400 Subject: [PATCH] Added redis cache and alphabetized imports #47 --- imaginate_api/app.py | 10 +++++++--- imaginate_api/config.py | 2 ++ imaginate_api/date/routes.py | 8 +++++--- imaginate_api/extensions.py | 4 +++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/imaginate_api/app.py b/imaginate_api/app.py index 404bacf..3dd21af 100644 --- a/imaginate_api/app.py +++ b/imaginate_api/app.py @@ -1,8 +1,9 @@ from flask import Flask, json, render_template -from werkzeug.exceptions import HTTPException +from imaginate_api.config import Config from imaginate_api.date.routes import bp as date_routes +from imaginate_api.extensions import cache from imaginate_api.image.routes import bp as image_routes -from imaginate_api.config import Config +from werkzeug.exceptions import HTTPException def create_app(): app = Flask(__name__) @@ -10,11 +11,14 @@ def create_app(): app.register_blueprint(date_routes, url_prefix="/date") app.register_blueprint(image_routes, url_prefix="/image") return app - + # Create app app = create_app() +# Initialize Redis cache +cache.init_app(app) + # This endpoint is simply for testing purposes @app.route("/") diff --git a/imaginate_api/config.py b/imaginate_api/config.py index b6ae442..9da75b5 100644 --- a/imaginate_api/config.py +++ b/imaginate_api/config.py @@ -22,3 +22,5 @@ class Config: PEXELS_TOKEN = os.getenv("PEXELS_TOKEN") DB_ENV = get_db_env() TESTING = False + CACHE_TYPE = 'RedisCache' + CACHE_REDIS_URL = os.getenv("REDIS_URL") diff --git a/imaginate_api/date/routes.py b/imaginate_api/date/routes.py index f2926dc..6485d0d 100644 --- a/imaginate_api/date/routes.py +++ b/imaginate_api/date/routes.py @@ -1,8 +1,9 @@ +from base64 import b64encode from flask import Blueprint, abort, jsonify -from imaginate_api.extensions import fs -from imaginate_api.utils import build_result, calculate_date from http import HTTPStatus -from base64 import b64encode +from imaginate_api.extensions import fs, cache +from imaginate_api.schemas.date_info import DateInfo +from imaginate_api.utils import build_result, calculate_date bp = Blueprint("date", __name__) @@ -15,6 +16,7 @@ # GET /date//images: used for viewing images of a specified date @bp.route("//images") +@cache.memoize(timeout=DateInfo.SECONDS_PER_DAY.value) def images_by_date(day): try: # This code is from GET /date/latest and is NOT internally called for aws/build_lambda_code.py diff --git a/imaginate_api/extensions.py b/imaginate_api/extensions.py index 6f379be..2f1d45b 100644 --- a/imaginate_api/extensions.py +++ b/imaginate_api/extensions.py @@ -2,6 +2,7 @@ import gridfs from imaginate_api.config import Config import sys +from flask_caching import Cache def connect_mongodb(conn_uri: str, db_name: str): @@ -17,4 +18,5 @@ def connect_mongodb(conn_uri: str, db_name: str): # Setup print(f"Running in \"{Config.DB_ENV}\" environment") -db, fs = connect_mongodb(Config.MONGO_TOKEN, f"imaginate_{Config.DB_ENV}") \ No newline at end of file +db, fs = connect_mongodb(Config.MONGO_TOKEN, f"imaginate_{Config.DB_ENV}") +cache = Cache() \ No newline at end of file