Administrator
8 天以前 fbac2e1e6ae16a4fff90a1ec8e7ac680c8bd459b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 涨停时间管理器
"""
今日最高价管理
"""
import json
 
from db import redis_manager_delegate as redis_manager
from db.redis_manager_delegate import RedisUtils
from log_module import async_log_util
from log_module.log import logger_debug
from utils import global_util, tool
 
 
class MaxPriceInfoManager:
    __max_price_info_cache = {}
    __db = 4
    _redisManager = redis_manager.RedisManager(4)
    __instance = None
 
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(MaxPriceInfoManager, cls).__new__(cls, *args, **kwargs)
            cls.load_max_price_info()
        return cls.__instance
 
    @classmethod
    def load_max_price_info(cls):
        redis = cls._redisManager.getRedis()
        keys = RedisUtils.keys(redis, "max_price_info-*", auto_free=False)
        for key in keys:
            code = key.replace("max_price_info-", "")
            val = RedisUtils.get(redis, key, auto_free=False)
            if val:
                val = json.loads(val)
                cls.__max_price_info_cache[code] = val
 
    def set_price_info(self, code, price, time):
        """
        设置价格信息
        @param code:
        @param price:
        @param time:
        @return:
        """
        old_price_info = self.__max_price_info_cache.get(code)
        if old_price_info and old_price_info[0] >= price:
            return
        price_info = (price, time)
        tool.CodeDataCacheUtil.set_cache(self.__max_price_info_cache, code, price_info)
        RedisUtils.setex_async(
            self.__db, "max_price_info-{}".format(code), tool.get_expire(), json.dumps(price_info))
        async_log_util.info(logger_debug, f"最大现价:{code}-{price_info}")
 
    def get_price_info_cache(self, code):
        return self.__max_price_info_cache.get(code)
 
 
if __name__ == "__main__":
    list = [("1234578", "09:00:03", None), ("12345", "09:00:01", True), ("123456", "09:00:00", True),
            ("123457", "09:00:04", False)]