From 8c7519b0dc79d32a216765a1b46e736d53e3d786 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期二, 08 八月 2023 14:17:37 +0800
Subject: [PATCH] Buy1PriceManager单例+缓存改造

---
 l2/l2_data_manager.py |  141 +++++++++++++++++++++--------------------------
 1 files changed, 63 insertions(+), 78 deletions(-)

diff --git a/l2/l2_data_manager.py b/l2/l2_data_manager.py
index 7071b03..2c32e79 100644
--- a/l2/l2_data_manager.py
+++ b/l2/l2_data_manager.py
@@ -6,7 +6,6 @@
 from db import redis_manager
 from db.redis_manager import RedisUtils
 from utils import tool
-from log_module.log import logger_l2_trade_buy
 from utils.tool import CodeDataCacheUtil
 
 _db = 1
@@ -33,45 +32,70 @@
 
 # 浜ゆ槗鐐圭鐞嗗櫒锛岀敤浜庣鐞嗕拱鍏ョ偣锛涗拱鎾ょ偣锛涜窛绂讳拱鍏ョ偣鐨勫噣涔板叆鏁版嵁锛涜窛绂讳拱鎾ょ偣鐨勪拱鎾ゆ暟鎹�
 class TradePointManager:
+    __db = 1
+    __redisManager = redis_manager.RedisManager(1)
     __buy_compute_index_info_cache = {}
+    __buy_cancel_single_pos_cache = {}
+    __instance = None
 
-    @staticmethod
-    def __get_redis():
-        return _redisManager.getRedis()
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(TradePointManager, cls).__new__(cls, *args, **kwargs)
+            cls.load_data()
+
+        return cls.__instance
+
+    @classmethod
+    def __get_redis(cls):
+        return cls.__redisManager.getRedis()
+
+    @classmethod
+    def load_data(cls):
+        redis_ = cls.__get_redis()
+        keys = RedisUtils.keys(redis_, "buy_compute_index_info-*")
+        for k in keys:
+            code = k.split("-")[-1]
+            val = RedisUtils.get(redis_, k)
+            val = json.loads(val)
+            CodeDataCacheUtil.set_cache(cls.__buy_compute_index_info_cache, code, val)
+
+        keys = RedisUtils.keys(redis_, "buy_cancel_single_pos-*")
+        for k in keys:
+            code = k.split("-")[-1]
+            val = RedisUtils.get(redis_, k)
+            CodeDataCacheUtil.set_cache(cls.__buy_cancel_single_pos_cache, code, int(val))
 
     # 鍒犻櫎涔板叆鐐规暟鎹�
-    @staticmethod
-    def delete_buy_point(code):
-        CodeDataCacheUtil.clear_cache(TradePointManager.__buy_compute_index_info_cache, code)
-        RedisUtils.delete_async(_db, "buy_compute_index_info-{}".format(code))
+
+    def delete_buy_point(self, code):
+        CodeDataCacheUtil.clear_cache(self.__buy_compute_index_info_cache, code)
+        RedisUtils.delete_async(self.__db, "buy_compute_index_info-{}".format(code))
 
     # 鑾峰彇涔板叆鐐逛俊鎭�
     # 杩斿洖鏁版嵁涓猴細涔板叆鐐� 绱绾拱棰� 宸茬粡璁$畻鐨勬暟鎹储寮�
-    @staticmethod
-    def get_buy_compute_start_data(code):
+
+    def get_buy_compute_start_data(self, code):
         _key = "buy_compute_index_info-{}".format(code)
-        _data_json = RedisUtils.get(TradePointManager.__get_redis(), _key)
+        _data_json = RedisUtils.get(self.__get_redis(), _key)
         if _data_json is None:
             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)
+    def get_buy_compute_start_data_cache(self, code):
+        cache_result = CodeDataCacheUtil.get_cache(self.__buy_compute_index_info_cache, code)
         if cache_result[0]:
             return cache_result[1]
-        val = TradePointManager.get_buy_compute_start_data(code)
-        CodeDataCacheUtil.set_cache(TradePointManager.__buy_compute_index_info_cache, code, val)
-        return val
+        return None, None, None, 0, 0, [], 0
 
     # 璁剧疆涔板叆鐐圭殑鍊�
     # buy_single_index 涔板叆淇″彿浣�
     # buy_exec_index 涔板叆鎵ц浣�
     # compute_index 璁$畻浣嶇疆
     # nums 绱绾拱棰�
-    @staticmethod
-    def set_buy_compute_start_data(code, buy_single_index, buy_exec_index, compute_index, nums, count, max_num_sets,
+
+    def set_buy_compute_start_data(self, code, buy_single_index, buy_exec_index, compute_index, nums, count,
+                                   max_num_sets,
                                    volume_rate):
         expire = tool.get_expire()
         _key = "buy_compute_index_info-{}".format(code)
@@ -81,80 +105,41 @@
                      volume_rate)
 
         else:
-            _buy_single_index, _buy_exec_index, _compute_index, _nums, _count, _max_num_index, _volume_rate = TradePointManager.get_buy_compute_start_data_cache(
+            _buy_single_index, _buy_exec_index, _compute_index, _nums, _count, _max_num_index, _volume_rate = self.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_)
-        RedisUtils.setex_async(
-            _db, _key, expire,
-            json.dumps(data_))
+        CodeDataCacheUtil.set_cache(self.__buy_compute_index_info_cache, code, data_)
+        RedisUtils.setex_async(self.__db, _key, expire, json.dumps(data_))
 
     # 鑾峰彇鎾や拱鍏ュ紑濮嬭绠楃殑淇℃伅
     # 杩斿洖鏁版嵁鐨勫唴瀹逛负锛氭挙閿�鐐圭储寮� 鎾や拱绾拱棰� 璁$畻鐨勬暟鎹储寮�
-    @staticmethod
-    def get_buy_cancel_single_pos(code):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "buy_cancel_single_pos-{}".format(code))
+
+    def get_buy_cancel_single_pos(self, code):
+        info = RedisUtils.get(self.__get_redis(), "buy_cancel_single_pos-{}".format(code))
         if info is None:
             return None
         else:
             return int(info)
 
+    def get_buy_cancel_single_pos_cache(self, code):
+        cache_result = tool.CodeDataCacheUtil.get_cache(self.__buy_cancel_single_pos_cache, code)
+        if cache_result[0]:
+            return cache_result[1]
+        return None
+
     # 璁剧疆涔版挙鐐逛俊鎭�
     # buy_num 绾拱棰�  computed_index璁$畻鍒扮殑涓嬫爣  index鎾や拱淇″彿璧风偣
 
-    @classmethod
-    def set_buy_cancel_single_pos(cls, code, index):
+    def set_buy_cancel_single_pos(self, code, index):
+        tool.CodeDataCacheUtil.set_cache(self.__buy_cancel_single_pos_cache, code, index)
         expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "buy_cancel_single_pos-{}".format(code), expire, index)
+        RedisUtils.setex_async(self.__db, "buy_cancel_single_pos-{}".format(code), expire, index)
 
     # 鍒犻櫎涔版挙鐐规暟鎹�
-    @classmethod
-    def delete_buy_cancel_point(cls, code):
-        RedisUtils.delete_async(_db, "buy_cancel_single_pos-{}".format(code))
-
-    # 璁剧疆涔版挙绾拱棰�
-    @classmethod
-    def set_compute_info_for_cancel_buy(cls, code, index, nums):
-        expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "compute_info_for_cancel_buy-{}".format(code), expire, json.dumps((index, nums)))
-        logger_l2_trade_buy.info("{}淇濆瓨鎾ゅ崟绾拱棰濅俊鎭細{}锛寋}", code, index, nums)
-
-    # 鑾峰彇涔版挙绾拱棰濊绠椾俊鎭�
-    @classmethod
-    def get_compute_info_for_cancel_buy(cls, code):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "compute_info_for_cancel_buy-{}".format(code))
-        if info is None:
-            return None, 0
-        else:
-            info = json.loads(info)
-            return info[0], info[1]
-
-    @classmethod
-    def delete_compute_info_for_cancel_buy(cls, code):
-        RedisUtils.delete_async(_db, "compute_info_for_cancel_buy-{}".format(code))
-
-    # 浠庝拱鍏ヤ俊鍙峰紑濮嬭缃定鍋滀拱涓庢定鍋滄挙鐨勫崟鏁�
-    @classmethod
-    def set_count_info_for_cancel_buy(cls, code, index, buy_count, cancel_count):
-        expire = tool.get_expire()
-        RedisUtils.setex(TradePointManager.__get_redis(), "count_info_for_cancel_buy-{}".format(code), expire,
-                         json.dumps((index, buy_count, cancel_count)))
-        logger_l2_trade_buy.info("{}淇濆瓨鎾ゅ崟绾拱棰濅俊鎭細{}锛寋}", code, index, buy_count, cancel_count)
-
-    # 鑾峰彇涔版挙绾拱棰濊绠椾俊鎭�
-    @classmethod
-    def get_count_info_for_cancel_buy(cls, code):
-        info = RedisUtils.get(TradePointManager.__get_redis(), "count_info_for_cancel_buy-{}".format(code))
-        if info is None:
-            return None, 0, 0
-        else:
-            info = json.loads(info)
-            return info[0], info[1], info[2]
-
-    @classmethod
-    def delete_count_info_for_cancel_buy(cls, code):
-        RedisUtils.delete_async(_db, "count_info_for_cancel_buy-{}".format(code))
+    def delete_buy_cancel_point(self, code):
+        tool.CodeDataCacheUtil.clear_cache(self.__buy_cancel_single_pos_cache, code)
+        RedisUtils.delete_async(self.__db, "buy_cancel_single_pos-{}".format(code))
 
 
 # 娓呴櫎l2鏁版嵁
@@ -203,8 +188,8 @@
 # 鏄惁鍦╨2鍥哄畾鐩戞帶浠g爜涓�
 def is_in_l2_fixed_codes(code):
     key = "l2-fixed-codes"
-    return RedisUtils.sismember( _redisManager.getRedis(), key, code)
+    return RedisUtils.sismember(_redisManager.getRedis(), key, code)
 
 
 if __name__ == "__main__":
-    TradePointManager.get_buy_compute_start_data_cache("603912")
+    TradePointManager().get_buy_compute_start_data_cache("603912")

--
Gitblit v1.8.0