Skip to content

Commit

Permalink
Merge pull request #2 from atrifat/feat-basic-cache
Browse files Browse the repository at this point in the history
feat: basic cache support
  • Loading branch information
atrifat authored Jun 7, 2024
2 parents 1d48b4a + 52f8a7c commit 9a326d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
28 changes: 28 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from fast_langdetect import detect_multilingual
from langdetect import detect_langs
from flask_caching import Cache

load_dotenv()

Expand All @@ -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"

Expand All @@ -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:
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9a326d1

Please sign in to comment.