From 079946d7bedd10151b6b6ea7ba25fd8ad1019777 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 02 八月 2023 00:20:13 +0800
Subject: [PATCH] redis缓存加入内存缓存

---
 l2/l2_data_manager.py |   38 ++++++++++++++++++++++++++++----------
 1 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/l2/l2_data_manager.py b/l2/l2_data_manager.py
index b99c0fa..339e9f2 100644
--- a/l2/l2_data_manager.py
+++ b/l2/l2_data_manager.py
@@ -6,6 +6,7 @@
 from db import redis_manager
 from utils import tool
 from log_module.log import logger_l2_trade_buy
+from utils.tool import CodeDataCacheUtil
 
 _redisManager = redis_manager.RedisManager(1)
 
@@ -30,6 +31,8 @@
 
 # 浜ゆ槗鐐圭鐞嗗櫒锛岀敤浜庣鐞嗕拱鍏ョ偣锛涗拱鎾ょ偣锛涜窛绂讳拱鍏ョ偣鐨勫噣涔板叆鏁版嵁锛涜窛绂讳拱鎾ょ偣鐨勪拱鎾ゆ暟鎹�
 class TradePointManager:
+    __buy_compute_index_info_cache = {}
+
     @staticmethod
     def __get_redis():
         return _redisManager.getRedis()
@@ -37,6 +40,7 @@
     # 鍒犻櫎涔板叆鐐规暟鎹�
     @staticmethod
     def delete_buy_point(code):
+        CodeDataCacheUtil.clear_cache(TradePointManager.__buy_compute_index_info_cache, code)
         redis = TradePointManager.__get_redis()
         redis.delete("buy_compute_index_info-{}".format(code))
 
@@ -48,9 +52,18 @@
         _key = "buy_compute_index_info-{}".format(code)
         _data_json = redis.get(_key)
         if _data_json is None:
-            return None, None, None, 0, 0, [],0
+            return None, None, None, 0, 0, [], 0
         _data = json.loads(_data_json)
         return _data[0], _data[1], _data[2], _data[3], _data[4], _data[5], _data[6]
+
+    @staticmethod
+    def get_buy_compute_start_data_cache(code):
+        cache_result = CodeDataCacheUtil.get_cache(TradePointManager.__buy_compute_index_info_cache, code)
+        if cache_result[0]:
+            return cache_result[1]
+        val = TradePointManager.get_buy_compute_start_data_cache(code)
+        CodeDataCacheUtil.set_cache(TradePointManager.__buy_compute_index_info_cache, code, val)
+        return val
 
     # 璁剧疆涔板叆鐐圭殑鍊�
     # buy_single_index 涔板叆淇″彿浣�
@@ -58,17 +71,24 @@
     # compute_index 璁$畻浣嶇疆
     # nums 绱绾拱棰�
     @staticmethod
-    def set_buy_compute_start_data(code, buy_single_index, buy_exec_index, compute_index, nums, count, max_num_sets, volume_rate):
+    def set_buy_compute_start_data(code, buy_single_index, buy_exec_index, compute_index, nums, count, max_num_sets,
+                                   volume_rate):
         redis = TradePointManager.__get_redis()
         expire = tool.get_expire()
         _key = "buy_compute_index_info-{}".format(code)
+        data_ = None
         if buy_single_index is not None:
-            redis.setex(_key, expire,
-                        json.dumps((buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),volume_rate)))
+            data_ = (buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),
+                     volume_rate)
+
         else:
-            _buy_single_index, _buy_exec_index, _compute_index, _nums, _count, _max_num_index,_volume_rate = TradePointManager.get_buy_compute_start_data(code)
-            redis.setex(_key, expire,
-                        json.dumps((_buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),volume_rate)))
+            _buy_single_index, _buy_exec_index, _compute_index, _nums, _count, _max_num_index, _volume_rate = TradePointManager.get_buy_compute_start_data_cache(
+                code)
+            data_ = (_buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets),
+                     volume_rate)
+        CodeDataCacheUtil.set_cache(TradePointManager.__buy_compute_index_info_cache, code, data_)
+        redis.setex(_key, expire,
+                    json.dumps(data_))
 
     # 鑾峰彇鎾や拱鍏ュ紑濮嬭绠楃殑淇℃伅
     # 杩斿洖鏁版嵁鐨勫唴瀹逛负锛氭挙閿�鐐圭储寮� 鎾や拱绾拱棰� 璁$畻鐨勬暟鎹储寮�
@@ -145,7 +165,6 @@
         redis.delete("count_info_for_cancel_buy-{}".format(code))
 
 
-
 # 娓呴櫎l2鏁版嵁
 def clear_l2_data(code):
     redis_l2 = redis_manager.RedisManager(1).getRedis()
@@ -156,10 +175,9 @@
     redis_l2.delete("l2-data-latest-{}".format(code))
 
 
-
-
 second_930 = 9 * 3600 + 30 * 60 + 0
 
+
 #  鍒濆鍖杔2鍥哄畾浠g爜搴�
 def init_l2_fixed_codes():
     key = "l2-fixed-codes"

--
Gitblit v1.8.0