fix Redis JSON cache issues

This commit is contained in:
Lea 2024-11-08 09:49:19 +01:00
parent dbff4005fc
commit b44c4a7597
3 changed files with 23 additions and 11 deletions

10
app.py
View File

@ -10,13 +10,17 @@ app.config.from_prefixed_env()
app.secret_key = app.config["THERAPY_SECRET"]
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
app.config["CACHE_TYPE"] = "redis"
app.config["CACHE_REDIS_HOST"] = "localhost"
app.config["CACHE_REDIS_HOST"] = "redis"
app.config["CACHE_REDIS_PORT"] = 6379
app.config["CACHE_REDIS_DB"] = 0
cache = Cache(app=app)
cache.init_app(app)
redis_client = redis.Redis(host='localhost', port=6379, db=0)
redis_client = redis.Redis(
host=app.config["CACHE_REDIS_HOST"],
port=app.config["CACHE_REDIS_PORT"],
db=app.config["CACHE_REDIS_DB"]
)
@app.route('/')
@ -50,7 +54,7 @@ def search_for_doctors_with_params():
return redirect("/")
# Create an APIHandler to process the given data by gathering the desired phone times
api_handler = APIHandler()
api_handler = APIHandler(redis_client)
# Get a list of locations
locations = api_handler.get_lat_lon_location_list(location)
# Location not found at Arztsuche API

View File

@ -1,4 +1,5 @@
import json
import sys
from typing import List
import requests
@ -6,8 +7,7 @@ from requests import JSONDecodeError
import base64
from datetime import datetime, timedelta
from app import redis_client
from arztapi.ArztPraxisDatas import ArztPraxisDatas
from arztapi.ArztPraxisDatas import ArztPraxisDatas, ArztPraxisData
from arztapi.DoctorInformation import DoctorInformation, PhoneTime
from arztapi.DoctorPhoneTime import DoctorPhoneTime
@ -17,7 +17,7 @@ class APIHandler:
Class for accessing and handling the Arztsuche API -> Get necessary data and filter it for theraPy
"""
def __init__(self):
def __init__(self, redis_client):
# Base URL as given by the base website
self.base_api_url = "https://arztsuche.116117.de/api/"
self.json_data = {}
@ -25,6 +25,7 @@ class APIHandler:
self.phone_times = []
self.general_information = []
self.processed_doctor_phone_times = []
self.redis_client = redis_client
def get_lat_lon_location_list(self, location):
"""
@ -161,18 +162,22 @@ class APIHandler:
return self.phone_times
def get_current_doctor_information_data_in_cache_with_time_check(self, amount_of_days):
cached_data = redis_client.get(str(self.json_data))
cached_data = self.redis_client.get(str(self.json_data))
if cached_data:
cached_data = json.loads(cached_data)
cache_timestamp = cached_data["timestamp"]
cache_timestamp = datetime.fromisoformat(cached_data["timestamp"])
current_date = datetime.now()
time_difference = current_date - cache_timestamp
if time_difference.days <= amount_of_days:
return cached_data["data"]
data = [ArztPraxisData.model_validate(json.loads(item)) for item in cached_data["data"]]
data = ArztPraxisDatas(arztPraxisDatas=data)
return data
def set_current_doctor_information_data_in_cache(self):
current_date = datetime.now()
redis_client.set(str(self.json_data), json.dumps({"timestamp": current_date, "data": self.phone_times}))
serialized_data = [item.json() for item in self.phone_times.arztPraxisDatas]
self.redis_client.set(str(self.json_data), json.dumps({"timestamp": current_date.isoformat(),
"data": serialized_data}))
def filter_for_therapy_types(self, therapy_types):
"""

View File

@ -1,11 +1,13 @@
annotated-types==0.7.0
blinker==1.8.2
cachelib==0.9.0
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
dnspython==2.6.1
email_validator==2.2.0
Flask==3.0.3
Flask-Caching==2.3.0
gunicorn==23.0.0
idna==3.7
itsdangerous==2.2.0
@ -18,5 +20,6 @@ redis==5.2.0
requests==2.32.3
typing_extensions==4.12.2
urllib3==2.2.2
waitress==3.0.0
Werkzeug==3.0.3
WTForms==3.1.2