superxuu commited on
Commit
e4e7477
·
1 Parent(s): d1ec6b7

perf: Add memory cache for eligible stocks to speed up game start

Browse files
Files changed (1) hide show
  1. backend/app/core.py +11 -1
backend/app/core.py CHANGED
@@ -46,12 +46,21 @@ class GameStartResponse(BaseModel):
46
  initial_index: int = 0 # 游戏开始的索引(相对于klines数组)
47
 
48
 
 
 
 
49
  def get_eligible_stocks() -> List[Dict[str, Any]]:
50
  """
51
  获取符合条件的股票列表
52
  条件:上市满3年
 
53
  """
 
 
 
 
54
  db = get_db()
 
55
 
56
  cutoff_date = datetime.now() - timedelta(days=MIN_LISTING_YEARS * 365)
57
 
@@ -68,7 +77,8 @@ def get_eligible_stocks() -> List[Dict[str, Any]]:
68
  for row in result
69
  ]
70
 
71
- logger.info(f"Found {len(stocks)} eligible stocks")
 
72
  return stocks
73
 
74
 
 
46
  initial_index: int = 0 # 游戏开始的索引(相对于klines数组)
47
 
48
 
49
+ # 全局缓存
50
+ _eligible_stocks_cache = None
51
+
52
  def get_eligible_stocks() -> List[Dict[str, Any]]:
53
  """
54
  获取符合条件的股票列表
55
  条件:上市满3年
56
+ 使用内存缓存优化性能
57
  """
58
+ global _eligible_stocks_cache
59
+ if _eligible_stocks_cache is not None:
60
+ return _eligible_stocks_cache
61
+
62
  db = get_db()
63
+ logger.info("Querying eligible stocks from database (first time)...")
64
 
65
  cutoff_date = datetime.now() - timedelta(days=MIN_LISTING_YEARS * 365)
66
 
 
77
  for row in result
78
  ]
79
 
80
+ _eligible_stocks_cache = stocks
81
+ logger.info(f"Found {len(stocks)} eligible stocks and cached them")
82
  return stocks
83
 
84