From 44c9070d0927035f5b6cd49eef02925895312278 Mon Sep 17 00:00:00 2001 From: zhenghaoz Date: Wed, 29 Apr 2026 12:28:01 +0800 Subject: [PATCH 1/2] Fix MySQL kv cache value length limitation --- utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/utils.py b/utils.py index 6a35c26..6b58d14 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,7 @@ import os import re import sys +from threading import Lock from typing import List, Optional, Tuple, Dict, Any import pytz @@ -18,6 +19,25 @@ from pydantic import BaseModel MAX_COMMENT_LENGTH = 512 +_kv_cache_schema_checked = False +_kv_cache_schema_lock = Lock() + + +def ensure_kv_cache_schema(engine) -> None: + """Ensure kv_cache.v can store large payloads in MySQL.""" + global _kv_cache_schema_checked + if _kv_cache_schema_checked: + return + + with _kv_cache_schema_lock: + if _kv_cache_schema_checked: + return + if engine.dialect.name == "mysql": + with engine.begin() as conn: + conn.exec_driver_sql( + "ALTER TABLE kv_cache MODIFY COLUMN v LONGTEXT NOT NULL" + ) + _kv_cache_schema_checked = True openai_client = OpenAI( @@ -99,6 +119,7 @@ def get_cached(k: str) -> Optional[Any]: from sqlalchemy.orm import sessionmaker engine = create_engine(os.getenv("SQLALCHEMY_DATABASE_URI")) + ensure_kv_cache_schema(engine) Session = sessionmaker() Session.configure(bind=engine) session = Session() @@ -118,6 +139,7 @@ def save_cache(k: str, v: Any, expiry_hours: int = KvCache.DEFAULT_EXPIRY_HOURS) from sqlalchemy.orm import sessionmaker engine = create_engine(os.getenv("SQLALCHEMY_DATABASE_URI")) + ensure_kv_cache_schema(engine) Session = sessionmaker() Session.configure(bind=engine) session = Session() From 872954047a97d2253b5bb02d7fc15b9d66d96f71 Mon Sep 17 00:00:00 2001 From: zhenghaoz Date: Wed, 29 Apr 2026 12:48:06 +0800 Subject: [PATCH 2/2] Define kv_cache.v as LONGTEXT at DDL for MySQL --- utils.py | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/utils.py b/utils.py index 6b58d14..0abc4c3 100644 --- a/utils.py +++ b/utils.py @@ -4,7 +4,6 @@ import os import re import sys -from threading import Lock from typing import List, Optional, Tuple, Dict, Any import pytz @@ -14,30 +13,12 @@ from github.GithubException import * from gorse import Gorse, GorseException from sqlalchemy import Column, String, Integer, DateTime, JSON, Text +from sqlalchemy.dialects.mysql import LONGTEXT from sqlalchemy.orm import declarative_base from openai import OpenAI from pydantic import BaseModel MAX_COMMENT_LENGTH = 512 -_kv_cache_schema_checked = False -_kv_cache_schema_lock = Lock() - - -def ensure_kv_cache_schema(engine) -> None: - """Ensure kv_cache.v can store large payloads in MySQL.""" - global _kv_cache_schema_checked - if _kv_cache_schema_checked: - return - - with _kv_cache_schema_lock: - if _kv_cache_schema_checked: - return - if engine.dialect.name == "mysql": - with engine.begin() as conn: - conn.exec_driver_sql( - "ALTER TABLE kv_cache MODIFY COLUMN v LONGTEXT NOT NULL" - ) - _kv_cache_schema_checked = True openai_client = OpenAI( @@ -103,7 +84,7 @@ class KvCache(Base): __tablename__ = 'kv_cache' k = Column(String(256), primary_key=True) - v = Column(Text, nullable=False) + v = Column(Text().with_variant(LONGTEXT, "mysql"), nullable=False) expire = Column(DateTime, nullable=False) DEFAULT_EXPIRY_HOURS = 24 @@ -119,7 +100,6 @@ def get_cached(k: str) -> Optional[Any]: from sqlalchemy.orm import sessionmaker engine = create_engine(os.getenv("SQLALCHEMY_DATABASE_URI")) - ensure_kv_cache_schema(engine) Session = sessionmaker() Session.configure(bind=engine) session = Session() @@ -139,7 +119,6 @@ def save_cache(k: str, v: Any, expiry_hours: int = KvCache.DEFAULT_EXPIRY_HOURS) from sqlalchemy.orm import sessionmaker engine = create_engine(os.getenv("SQLALCHEMY_DATABASE_URI")) - ensure_kv_cache_schema(engine) Session = sessionmaker() Session.configure(bind=engine) session = Session()