diff --git a/.env.example b/.env.example index 6d569e2..cf6050f 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,10 @@ LISTEN_PORT=5000 LANGUAGE_DETECTION_MODEL=langdetect # (Optional. Default: false. Options: true or false) Parameters will be used when LANGUAGE_DETECTION_MODEL=fast_langdetect . Check https://github.com/LlmKira/fast-langdetect for more information -LOW_MEMORY_MODE=false \ No newline at end of file +LOW_MEMORY_MODE=false + +# (Optional. Default: false. Options: true or false) Check whether the cache will be enabled or not +ENABLE_CACHE=false + +# (Optional. Default: 60. Options: integer value) Duration of cache in seconds. Parameter will be used when the cache is enabled +CACHE_DURATION_SECONDS=60 diff --git a/app.py b/app.py index a1de73e..aeb9959 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from fast_langdetect import detect_multilingual from langdetect import detect_langs +from flask_caching import Cache load_dotenv() @@ -18,6 +19,14 @@ LISTEN_PORT = os.getenv("LISTEN_PORT", "5000") LANGUAGE_DETECTION_MODEL = os.getenv("LANGUAGE_DETECTION_MODEL", "langdetect") LOW_MEMORY_MODE = os.getenv("LOW_MEMORY_MODE", "false") == "true" +CACHE_DURATION_SECONDS = ( + None + if int(os.getenv("CACHE_DURATION_SECONDS", 60)) == 0 + else int(os.getenv("CACHE_DURATION_SECONDS", 60)) +) + +CACHE_DURATION_SECONDS = int(os.getenv("CACHE_DURATION_SECONDS", 60)) +ENABLE_CACHE = os.getenv("ENABLE_CACHE", "false") == "true" APP_VERSION = "0.0.1" @@ -42,6 +51,14 @@ app = Flask(__name__) +cache_config = { + "DEBUG": True if APP_ENV != "production" else False, + "CACHE_TYPE": "SimpleCache" if ENABLE_CACHE else "NullCache", + "CACHE_DEFAULT_TIMEOUT": CACHE_DURATION_SECONDS, # Cache duration in seconds +} +cache = Cache(config=cache_config) +cache.init_app(app) + def is_valid_api_key(api_key): if api_key == API_TOKEN: @@ -69,6 +86,16 @@ def decorator(*args, **kwargs): return decorator +def make_key_fn(): + """A function which is called to derive the key for a computed value. + The key in this case is the concat value of all the json request + parameters. Other strategy could to use any hashing function. + :returns: unique string for which the value should be cached. + """ + user_data = request.get_json() + return ",".join([f"{key}={value}" for key, value in user_data.items()]) + + def perform_detect_language(query): result = [] temp_result = [] @@ -115,6 +142,7 @@ def handle_exception(error): @app.route("/detect", methods=["POST"]) @api_required +@cache.cached(make_cache_key=make_key_fn) def predict(): data = request.json q = data["q"] diff --git a/requirements.txt b/requirements.txt index d5e0bdd..a8bf4c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask==3.0.3 +Flask-Caching==2.3.0 python-dotenv==1.0.0 pandas==2.2.2 fast-langdetect==0.1.1