Skip to content

Commit

Permalink
Added redis cache and alphabetized imports #47
Browse files Browse the repository at this point in the history
  • Loading branch information
zachale committed Oct 25, 2024
1 parent d3c5c02 commit fc4d0aa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
10 changes: 7 additions & 3 deletions imaginate_api/app.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
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__)
app.config.from_object(Config)
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("/")
Expand Down
2 changes: 2 additions & 0 deletions imaginate_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
8 changes: 5 additions & 3 deletions imaginate_api/date/routes.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -15,6 +16,7 @@

# GET /date/<day>/images: used for viewing images of a specified date
@bp.route("/<day>/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
Expand Down
4 changes: 3 additions & 1 deletion imaginate_api/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}")
db, fs = connect_mongodb(Config.MONGO_TOKEN, f"imaginate_{Config.DB_ENV}")
cache = Cache()

0 comments on commit fc4d0aa

Please sign in to comment.