예제 #1
0
 def del_cache(self, name):
     try:
         redis_search = dbfactory.create_db(conf_name=self.conf_name,
                                            db_type="db_pika",
                                            link_type="default")
         return redis_search.delete(name)
     except Exception as e:
         return None
예제 #2
0
파일: BaseMongo.py 프로젝트: wlyehbd/common
 def __init__(self, *args, **kwargs):
     """
     调用父类初始化数据库连接
     :param conf_name: 配置名称
     :return
     """
     self.conn = dbfactory.create_db(conf_name=kwargs.get("conf_name"),
                                     db_type="db_mongo")
     super().__init__(model=kwargs.get("model"))
예제 #3
0
 def del_cache(self, name, keys):
     """
     :param name:
     :param keys:
     :param 删除缓存
     :return:
     """
     try:
         redis_search = dbfactory.create_db(conf_name=self.conf_name,
                                            db_type="db_pika",
                                            link_type="default")
         return redis_search.hdel(name, keys)
     except Exception as e:
         return None
예제 #4
0
    def push_cache(self, name, key, value):
        """
        :param name:
        :param key:
        :param value:
        :desc 添加缓存
        :return:
        """
        try:
            redis_search = dbfactory.create_db(conf_name=self.conf_name,
                                               db_type="db_pika",
                                               link_type="default")
            data = {"data": value, "time": time.time()}
            result = redis_search.hset(name=name,
                                       key=key,
                                       value=json.dumps(data))
            if self.time:  # 默认有失效时间就给大key设置失效时间
                redis_search.expire(name=name, time=self.time)
            return result

        except Exception as e:
            return None
예제 #5
0
 def pull_cache(self, name, key):
     """
     :param name:
     :param key:
     :desc #获取缓存
     :return:
     """
     times = int(time.time())
     timeArray = time.localtime(times)
     otherStyleTime = time.strftime("%Y-%m-%d %H:%M", timeArray)
     day = otherStyleTime.split(" ")[0]
     hm = otherStyleTime.split(" ")[1]
     logkey = "cache_log_y_" + day
     nologkey = "cache_log_n_" + day
     try:
         redis_search = dbfactory.create_db(conf_name=self.conf_name,
                                            db_type="db_pika",
                                            link_type="slave1")
         try:
             rate = float(redis_search.get("cache_rate") or 1)
         except Exception as e1:
             print("pull_cache", e1)
         data = redis_search.hget(name=name, key=key)
         if data:
             data = json.loads(data)
             cache_time = data.get("time", 0)
             if self.time == 0 or cache_time + self.time * rate > times:  # 未失效直接返回
                 # redis_search.hincrby(logkey,hm)
                 return data.get("data", None)
         #  失效直接返回None
         # redis_search.hincrby(nologkey, hm)
         return None
         # return redis_search.hget(name=name, key=key)
     except Exception as e:
         print(name, e)
         return None
예제 #6
0
파일: BaseKafka.py 프로젝트: wlyehbd/common
 def __init__(self, *args, **kwargs):
     self.conf_name = kwargs.get("conf_name")
     self.conn = dbfactory.create_db(conf_name=self.conf_name,
                                     db_type="db_kafka")
예제 #7
0
파일: BaseMysql.py 프로젝트: wlyehbd/common
 def __init__(self, *args, **kwargs):
     self.model = kwargs.get("model")
     self.conn = dbfactory.create_db(conf_name=kwargs.get("conf_name"),
                                     db_type=kwargs.get("db_type")
                                     or "db_mysql")
     super().__init__(model=self.model)
예제 #8
0
        def wrapper(*args, **kwargs):
            st = time.time()  # 开始时间戳
            # 开始拼接缓存key和field
            city = kwargs.get("city", "default")  # 城市
            class_name = list(args).pop().__class__.__name__  # 类名
            func_name = func.__name__  # 函数名
            prefix = ConfigService.get_pika_conf().get(self.conf_name).get(
                'pre')  # 获取pika配置中二级前缀
            # hashkey:【城市】【类名【函数名称】
            name = self.pre + prefix + city + "." + class_name + "." + func_name

            cache_key = kwargs.get(self.key)  # 通过变量名获取缓存field的值

            # 从pika降级缓存开关值。
            pika_search = dbfactory.create_db(conf_name=self.conf_name,
                                              db_type="db_pika",
                                              link_type="slave1")
            cache_type = int(pika_search.get("lower_pcache")
                             or 0)  # 0超时再走缓存,1直接走缓存

            # 开始请求数据 正常情况下插入缓存并返回结果
            if cache_type == 0:  # 降级模式
                result = {}
                try:
                    result = func(*args, **kwargs)  # 查询数据
                    if result:
                        self.push_cache(name=name,
                                        key=cache_key,
                                        value=json.dumps(result))
                    # 降级模式 正常状态
                    logger.info("lowcache",
                                extra={
                                    "fun_name": func_name,
                                    "cache_type": 0,
                                    "fun_time": time.time() - st,
                                    "project": prefix
                                })

                    logger_content.debug({
                        "name": name,
                        "cache_key": cache_key,
                        "result": result
                    })
                    return result
                except Exception as e:
                    print("func执行异常", e)
                    # cache_type == 1

            # 查询到缓存就返回数据
            result = self.pull_cache(name=name, key=cache_key)
            if result:
                result = json.loads(result)
                logger.info("lowcache",
                            extra={
                                "fun_name": func_name,
                                "cache_type": 2,
                                "fun_time": time.time() - st,
                                "project": prefix
                            })

                logger_content.debug({
                    "name": name,
                    "cache_key": cache_key,
                    "result": result
                })
                return result

            # 查询缓存模式 并且没有数据 需要执行func 获取数据
            if cache_type == 1:
                try:
                    result = func(*args, **kwargs)  # 执行函数
                    if result:
                        self.push_cache(name=name,
                                        key=cache_key,
                                        value=json.dumps(result))
                    logger.info("lowcache",
                                extra={
                                    "fun_name": func_name,
                                    "cache_type": 1,
                                    "fun_time": time.time() - st,
                                    "project": prefix
                                })
                    logger_content.debug({
                        "name": name,
                        "cache_key": cache_key,
                        "result": result
                    })
                    return result
                except Exception as e:
                    print("缓存模式下func执行异常", e)
                    logger.info("lowcache",
                                extra={
                                    "fun_name": func_name,
                                    "cache_type": 4,
                                    "fun_time": time.time() - st,
                                    "project": prefix
                                })
            logger_content.debug({
                "name": name,
                "cache_key": cache_key,
                "result": result
            })
            return result or {}