Пример #1
0
class deal_cache_attends(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        dbi = MySQL_Interface()
        self.dbi = dbi
        self.bf = BloomFilter()

    def run(self):
        bag = []
        uid_bag = []  #与bag类似,只不过存储uid
        bag_size = 1000  #100次插入一次
        ready_to_get_col = self.dbi.get_col_name('ready_to_get')
        cache_attends_col = self.dbi.get_col_name('cache_attends')
        while True:
            query = 'select * from cache_attends limit 5000'
            res = self.dbi.select_asQuery(query)
            if res.__len__() == 0:
                if bag.__len__() > 0:
                    self.dbi.insert_asList('ready_to_get', bag, unique=True)
                    bag = []
                    # self.bf.insert_asList(uid_bag,'ready_to_get')
                    uid_bag = []
                time.sleep(1)
                self.dbi = MySQL_Interface()  #更新dbi
                continue

            print('thread cache attends is working')

            for line in res:
                raw_id = line[cache_attends_col.index('uid')]
                in_user_info = self.bf.isContains(raw_id,
                                                  'user_info_table')  #此处可优化
                if not in_user_info:
                    data = [
                        line[cache_attends_col.index(col)]
                        if col in cache_attends_col else None
                        for col in ready_to_get_col
                    ]
                    bag.append(data)
                    uid_bag.append(raw_id)
                    if bag.__len__() > bag_size:
                        self.dbi.insert_asList('ready_to_get',
                                               bag,
                                               unique=True)
                        # self.bf.insert_asList(uid_bag,'ready_to_get')
                        print('insert once')
                        bag = []
                        uid_bag = []
                self.dbi.delete_line('cache_attends', 'uid', raw_id)  # 此处可优化

    def isInUserInfo(self, in_uid):
        col_user_info = self.dbi.get_col_name('user_info_table')
        query = 'select * from user_info_table where uid={uid}'.format(
            uid=in_uid)
        res = self.dbi.select_asQuery(query)
        if res.__len__() == 0:
            return False
        else:
            return True
class deal_cache_attends(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        dbi=MySQL_Interface()
        self.dbi=dbi
        self.bf=BloomFilter()

    def  run(self):
        bag=[]
        uid_bag=[]              #与bag类似,只不过存储uid
        bag_size=1000             #100次插入一次
        ready_to_get_col=self.dbi.get_col_name('ready_to_get')
        cache_attends_col=self.dbi.get_col_name('cache_attends')
        while True:
            query='select * from cache_attends limit 5000'
            res=self.dbi.select_asQuery(query)
            if res.__len__()==0:
                if bag.__len__()>0:
                    self.dbi.insert_asList('ready_to_get',bag,unique=True)
                    bag=[]
                    # self.bf.insert_asList(uid_bag,'ready_to_get')
                    uid_bag=[]
                time.sleep(1)
                self.dbi=MySQL_Interface()  #更新dbi
                continue

            print('thread cache attends is working')

            for line in res:
                raw_id=line[cache_attends_col.index('uid')]
                in_user_info=self.bf.isContains(raw_id,'user_info_table')   #此处可优化
                if not in_user_info:
                    data=[line[cache_attends_col.index(col)] if col in cache_attends_col else None for col in ready_to_get_col]
                    bag.append(data)
                    uid_bag.append(raw_id)
                    if bag.__len__()>bag_size:
                        self.dbi.insert_asList('ready_to_get',bag,unique=True)
                        # self.bf.insert_asList(uid_bag,'ready_to_get')
                        print('insert once')
                        bag=[]
                        uid_bag=[]
                self.dbi.delete_line('cache_attends','uid',raw_id) # 此处可优化

    def isInUserInfo(self,in_uid):
        col_user_info=self.dbi.get_col_name('user_info_table')
        query='select * from user_info_table where uid={uid}'.format(uid=in_uid)
        res=self.dbi.select_asQuery(query)
        if res.__len__()==0:
            return False
        else:
            return True