replace shelve with redis

This commit is contained in:
Lea 2024-11-07 17:03:19 +01:00
parent 4de584e59d
commit dbff4005fc
3 changed files with 18 additions and 7 deletions

12
app.py
View File

@ -1,7 +1,7 @@
import locale import locale
import redis
from flask import Flask, render_template, request, redirect from flask import Flask, render_template, request, redirect
from flask_caching import Cache
from forms.SearchForm import SearchForm from forms.SearchForm import SearchForm
from arztapi.APIHandler import APIHandler from arztapi.APIHandler import APIHandler
@ -9,6 +9,14 @@ app = Flask(__name__)
app.config.from_prefixed_env() app.config.from_prefixed_env()
app.secret_key = app.config["THERAPY_SECRET"] app.secret_key = app.config["THERAPY_SECRET"]
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
app.config["CACHE_TYPE"] = "redis"
app.config["CACHE_REDIS_HOST"] = "localhost"
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)
@app.route('/') @app.route('/')

View File

@ -1,10 +1,12 @@
import json
from typing import List from typing import List
import requests import requests
from requests import JSONDecodeError from requests import JSONDecodeError
import base64 import base64
import shelve
from datetime import datetime, timedelta from datetime import datetime, timedelta
from app import redis_client
from arztapi.ArztPraxisDatas import ArztPraxisDatas from arztapi.ArztPraxisDatas import ArztPraxisDatas
from arztapi.DoctorInformation import DoctorInformation, PhoneTime from arztapi.DoctorInformation import DoctorInformation, PhoneTime
from arztapi.DoctorPhoneTime import DoctorPhoneTime from arztapi.DoctorPhoneTime import DoctorPhoneTime
@ -23,7 +25,6 @@ class APIHandler:
self.phone_times = [] self.phone_times = []
self.general_information = [] self.general_information = []
self.processed_doctor_phone_times = [] self.processed_doctor_phone_times = []
self._cache = shelve.open("_therapydb.cache")
def get_lat_lon_location_list(self, location): def get_lat_lon_location_list(self, location):
""" """
@ -160,8 +161,9 @@ class APIHandler:
return self.phone_times return self.phone_times
def get_current_doctor_information_data_in_cache_with_time_check(self, amount_of_days): def get_current_doctor_information_data_in_cache_with_time_check(self, amount_of_days):
cached_data = self._cache.get(str(self.json_data)) cached_data = redis_client.get(str(self.json_data))
if cached_data: if cached_data:
cached_data = json.loads(cached_data)
cache_timestamp = cached_data["timestamp"] cache_timestamp = cached_data["timestamp"]
current_date = datetime.now() current_date = datetime.now()
time_difference = current_date - cache_timestamp time_difference = current_date - cache_timestamp
@ -170,7 +172,7 @@ class APIHandler:
def set_current_doctor_information_data_in_cache(self): def set_current_doctor_information_data_in_cache(self):
current_date = datetime.now() current_date = datetime.now()
self._cache[str(self.json_data)] = {"timestamp": current_date, "data": self.phone_times} redis_client.set(str(self.json_data), json.dumps({"timestamp": current_date, "data": self.phone_times}))
def filter_for_therapy_types(self, therapy_types): def filter_for_therapy_types(self, therapy_types):
""" """

View File

@ -14,6 +14,7 @@ MarkupSafe==2.1.5
packaging==24.1 packaging==24.1
pydantic==2.8.2 pydantic==2.8.2
pydantic_core==2.20.1 pydantic_core==2.20.1
redis==5.2.0
requests==2.32.3 requests==2.32.3
typing_extensions==4.12.2 typing_extensions==4.12.2
urllib3==2.2.2 urllib3==2.2.2