Commit 47076b9

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-08-08 09:40:32
fix(history): separate d1 and turso cache keys
1 parent e3710b3
Changed files (2)
src
src/history/d1.py
@@ -278,9 +278,9 @@ async def save_userinfo_to_d1(client: Client, minfo: dict) -> dict[str, str]:
     if uid == 1:  # default user (user is unknown)
         return {}
     # Get user info from turso and save it to cache
-    if not (cached := cache.get(f"userinfo-{uid}-{cid}")):
+    if not (cached := cache.get(f"d1-user-{uid}-{cid}")):
         cached = await get_d1_userinfo(uid, cid)
-        cache.set(f"userinfo-{uid}-{cid}", cached, ttl=0)
+        cache.set(f"d1-user-{uid}-{cid}", cached, ttl=0)
 
     ctitle = minfo["ctitle"] or minfo["full_name"]
     # if in private chats, we use the opponent's name as chat title
@@ -302,7 +302,7 @@ async def save_userinfo_to_d1(client: Client, minfo: dict) -> dict[str, str]:
     }
     if cached != records:
         logger.info(f"Save user info: {records}")
-        cache.set(f"userinfo-{uid}-{cid}", records, ttl=0)
+        cache.set(f"d1-user-{uid}-{cid}", records, ttl=0)
         await query_d1(**insert_d1("userinfo", records, update_on_conflict="id"), db_name=HISTORY.D1_DATABASE, silent=True)
     return records
 
@@ -332,9 +332,9 @@ async def save_chatinfo_to_d1(client: Client, minfo: dict) -> dict[str, str]:
     if str(cid) == "0":
         return {}
     # Get chat info from turso and save it to cache
-    if not (cached := cache.get(f"chatinfo-{cid}")):
+    if not (cached := cache.get(f"d1-chat-{cid}")):
         cached = await get_d1_chatinfo(cid)
-        cache.set(f"chatinfo-{cid}", cached, ttl=0)
+        cache.set(f"d1-chat-{cid}", cached, ttl=0)
 
     ctitle = minfo["ctitle"] or minfo["full_name"]
     # if in private chats, we use the opponent's name as chat title
@@ -364,6 +364,6 @@ async def save_chatinfo_to_d1(client: Client, minfo: dict) -> dict[str, str]:
     )
     if cached != records:
         logger.info(f"Save chat info: {records}")
-        cache.set(f"chatinfo-{cid}", records, ttl=0)
+        cache.set(f"d1-chat-{cid}", records, ttl=0)
         await query_d1(**insert_d1("chatinfo", records, update_on_conflict="cid"), db_name=HISTORY.D1_DATABASE, silent=True)
     return records
src/history/turso.py
@@ -284,9 +284,9 @@ async def save_chatinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
     if str(cid) == "0":
         return {}
     # Get chat info from turso and save it to cache
-    if not (cached := cache.get(f"chatinfo-{cid}")):
+    if not (cached := cache.get(f"turso-chat-{cid}")):
         cached = await get_turso_chatinfo(cid)
-        cache.set(f"chatinfo-{cid}", cached, ttl=0)
+        cache.set(f"turso-chat-{cid}", cached, ttl=0)
 
     ctitle = minfo["ctitle"] or minfo["full_name"]
     # if in private chats, we use the opponent's name as chat title
@@ -316,7 +316,7 @@ async def save_chatinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
     )
     if cached != records:
         logger.info(f"Save chat info: {records}")
-        cache.set(f"chatinfo-{cid}", records, ttl=0)
+        cache.set(f"turso-chat-{cid}", records, ttl=0)
         await turso_exec([insert_statement("chatinfo", records, update_on_conflict="cid")], retry=2, **TURSO_KWARGS)
     return records
 
@@ -357,9 +357,9 @@ async def save_userinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
     if uid == 1:  # default user (user is unknown)
         return {}
     # Get user info from turso and save it to cache
-    if not (cached := cache.get(f"userinfo-{uid}-{cid}")):
+    if not (cached := cache.get(f"turso-user-{uid}-{cid}")):
         cached = await get_turso_userinfo(uid, cid)
-        cache.set(f"userinfo-{uid}-{cid}", cached, ttl=0)
+        cache.set(f"turso-user-{uid}-{cid}", cached, ttl=0)
 
     ctitle = minfo["ctitle"] or minfo["full_name"]
     # if in private chats, we use the opponent's name as chat title
@@ -381,7 +381,7 @@ async def save_userinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
     }
     if cached != records:
         logger.info(f"Save user info: {records}")
-        cache.set(f"userinfo-{uid}-{cid}", records, ttl=0)
+        cache.set(f"turso-user-{uid}-{cid}", records, ttl=0)
         await turso_exec([insert_statement("userinfo", records, update_on_conflict="id")], retry=2, **TURSO_KWARGS)
     return records