[nominatim-web] Use redis pipeline to extend key TTL on get operation



commit 1d0d1e86d77d383998a341f1db44edb7214e88b5
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Sat Apr 10 14:51:09 2021 +0200

    Use redis pipeline to extend key TTL on get operation

 app/main.py | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
---
diff --git a/app/main.py b/app/main.py
index d2f9636..0627ac9 100644
--- a/app/main.py
+++ b/app/main.py
@@ -8,6 +8,7 @@ import requests
 from fastapi import FastAPI, Request, Response
 from pydantic import BaseSettings
 
+
 class Settings(BaseSettings):
     api_url: str = "https://nominatim.openstreetmap.org";
     redis_host: str = "localhost"
@@ -63,8 +64,10 @@ def search(request: Request):
 
     key = f"search:{lang}:{limit}:{location}:{query}"
 
-    if resp := redis_conn.get(key):
-        return Response(content=resp, media_type="application/json")
+    with redis_conn.pipeline() as redis_pipeline:
+        if resp := redis_pipeline.get(key):
+            redis_pipeline.expire(key, 600)
+            return Response(content=resp, media_type="application/json")
 
     with postgres_ro.cursor() as cur:
         cur.execute("SELECT * FROM search WHERE key = %s", (key,))
@@ -102,8 +105,11 @@ def reverse(request: Request):
     lang = request.query_params.get("accept-language", "none")
 
     key = f"rev:{lang}:{lat}x{lon}"
-    if resp := redis_conn.get(key):
-        return Response(content=resp, media_type="application/json")
+
+    with redis_conn.pipeline() as redis_pipeline:
+        if resp := redis_pipeline.get(key):
+            redis_pipeline.expire(key, 600)
+            return Response(content=resp, media_type="application/json")
 
     with postgres_ro.cursor() as cur:
         cur.execute("SELECT * FROM reverse WHERE key = %s", (key,))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]