class KeyCleanTaskTest(object): ''' KeyCleanTask 测试类 ''' def __init__(self): self.__keyCleanTask = KeyClearTask() self.__config = GetConfig() self.__redisClient = RedisClient(self.__config.redis_host(), self.__config.redis_port(), self.__config.redis_password()) def testDoTask(self): dayList = DateUtil.splitIntoDays(DateUtil.addDaysOnCurrent(-3), DateUtil.getCurrentDate()) hourList = DateUtil.splitIntoHours(DateUtil.addHoursOnCurrent(-6), DateUtil.getCurrentDate()) db = 0 print "----test doTask func begin" unlinkKeyList = [] unlinkSetKeyList = [] # 删除以day为单位的key for day in dayList: unlinkSetKeyList.append('launch_date_' + day) # 投放日期(Set),存储campaign Id 的set(白名单) unlinkKeyList.append('launch_date_2_creatives_' + day) # 投放日期到creative的映射关系,存储的是BitSet unlinkKeyList.append('fingerprint_2_creativeId_' + day) # fingerprint与creativeId对应关系 self.__redisClient.sadd(db, 'launch_date_' + day, *['mem1', 'mem2']) self.__redisClient.set(db, 'launch_date_2_creatives_' + day, 'launch_date_2_creatives_value1') self.__redisClient.set(db, 'fingerprint_2_creativeId_' + day, 'fingerprint_2_creativeId_value1') # 删除以hour为单位的key for hour in hourList: unlinkKeyList.append( 'account_unfreeze_time_' + hour) # 广告商账户级别的冻结(预算不足),该小时冻结,存储的是agentId set unlinkKeyList.append( 'campaign_unfreeze_time_' + hour) # 推广计划级别的冻结(预算不足),该小时冻结,存储的是campaign Id set(黑名单) unlinkKeyList.append('account_unfreeze_time_2_creatives_' + hour) # 账户级别的冻结时间到creative的映射关系,存储的是BitSet unlinkKeyList.append('campaign_unfreeze_time_2_creatives_' + hour) # 推广计划级别的冻结时间到creative的映射关系,存储的是BitSet self.__redisClient.set(db, 'account_unfreeze_time_' + hour, 'account_unfreeze_time_') self.__redisClient.set(db, 'campaign_unfreeze_time_' + hour, 'campaign_unfreeze_time_') self.__redisClient.set(db, 'account_unfreeze_time_2_creatives_' + hour, 'account_unfreeze_time_2_creatives_') self.__redisClient.set( db, 'campaign_unfreeze_time_2_creatives_' + hour, 'campaign_unfreeze_time_2_creatives_') print("init data:") print(self.__redisClient.mget(db, *unlinkKeyList)) for unlinkSetKey in unlinkSetKeyList: print(self.__redisClient.sscan(db, unlinkSetKey, 0, None, 3)) self.__keyCleanTask.doTask() print("after clean:") print(self.__redisClient.mget(db, *unlinkKeyList)) for unlinkSetKey in unlinkSetKeyList: print(self.__redisClient.sscan(db, unlinkSetKey, 0, None, 3))
class ExceedDateKeyCleanTaskTest(object): ''' KeyCleanTask 测试类 ''' def __init__(self): self.__exceedDateKeyCleanTask = ExceedDateKeyCleanTask() self.__config = GetConfig() self.__redisClient = RedisClient(self.__config.redis_host(), self.__config.redis_port(), self.__config.redis_password()) def testStart(self): keyPrefix = 'account_cost_' validDays = 2 db = 1 day = DateUtil.format4Y2m2d(DateUtil.addDaysOnCurrent(-validDays)) key = keyPrefix + day print "----test start func begin" self.__redisClient.hset(db, key, 'map1', 'value1') self.__redisClient.hset(db, key, 'map2', 'value2') print("init data:") print(self.__redisClient.hscan(db, key, 0, None, 3)) self.__exceedDateKeyCleanTask.start(keyPrefix, db, validDays) print("after clean:") print(self.__redisClient.hscan(db, key, 0, None, 3)) pass def testStartHour(self): keyPrefix = 'data_cost_' validDays = 2 db = 3 day = DateUtil.format4Y2m2d(DateUtil.addDaysOnCurrent(-validDays)) print "----test startHour func begin" keyList = [] for i in xrange(0, 24): if i < 10: key = keyPrefix + day + '0' + str(i) else: key = keyPrefix + day + str(i) keyList.append(key) self.__redisClient.hset(db, key, 'map1', 'value1') self.__redisClient.hset(db, key, 'map2', 'value2') print("init data:") for key in keyList: print(self.__redisClient.hgetall(db, key)) self.__exceedDateKeyCleanTask.startHour(keyPrefix, db, validDays) print("after clean:") for key in keyList: print(self.__redisClient.hgetall(db, key)) pass def testStartBatch(self): keyPrefix = 'mix_detail_posid_' validDays = 30 db = 1 day = DateUtil.format4Y2m2d(DateUtil.addDaysOnCurrent(-validDays)) key = keyPrefix + day print "----test startBatch func begin" self.__redisClient.sadd(db, key, *['mem1', 'mem2']) print("init data:") print(self.__redisClient.sscan(db, key, 0, None, 3)) self.__exceedDateKeyCleanTask.startBatch(keyPrefix, db, validDays) print("after clean:") print(self.__redisClient.sscan(db, key, 0, None, 3)) pass def testStartHscan(self): keyPrefix = 'view_st_ts_' validDays = 1 db = 1 day = DateUtil.format4Y2m2d(DateUtil.addDaysOnCurrent(-validDays)) key = keyPrefix + day print "----test startHscan func begin" self.__redisClient.hset(db, key, 'map1', 'value1') self.__redisClient.hset(db, key, 'map2', 'value2') print("init data:") print(self.__redisClient.hscan(db, key, 0, None, 3)) self.__exceedDateKeyCleanTask.startHscan(keyPrefix, db, validDays) print("after clean:") print(self.__redisClient.hscan(db, key, 0, None, 3)) pass def testStartSscan(self): keyPrefix = 'click_st_' validDays = 1 db = 1 day = DateUtil.format4Y2m2d(DateUtil.addDaysOnCurrent(-validDays)) key = keyPrefix + day print "----test startSscan func begin" self.__redisClient.sadd(db, key, *['mem1', 'mem2']) print("init data:") print(self.__redisClient.sscan(db, key, 0, None, 3)) self.__exceedDateKeyCleanTask.startSscan(keyPrefix, db, validDays) print("after clean:") print(self.__redisClient.sscan(db, key, 0, None, 3)) pass