def load_ip_to_redis(filename=IP_TEXT, flush=True): """解析ip, 存储到 redis""" lines = count_lines(filename) if flush: redis_db.delete('ip_location:') redis_db.delete('location:') with open(filename, encoding='utf-8', mode='r+') as fp: ip_mapping = {} location_mapping = {} temp_location = None temp_score = None temp_info = None count = 0 lineno = 0 for lineno, line in enumerate(fp): if not line: continue pieces = line.split(',') score = pieces[1] country, province, city = pieces[4:7] location = city or province or country if temp_location and location != temp_location: ip_mapping['%s_%s' % (temp_location, count)] = temp_score location_mapping[temp_location] = temp_info count += 1 if not count % 10000: redis_db.zadd('ip_location:', ip_mapping) redis_db.hmset('location:', location_mapping) ip_mapping = {} location_mapping = {} print('Loaded ips: %s/%s' % (lineno, lines)) temp_location = location temp_score = score temp_info = '%s:%s:%s' % (country, province, city) ip_mapping['%s_%s' % (temp_location, count)] = temp_score location_mapping[temp_location] = temp_info redis_db.zadd('ip_location:', ip_mapping) redis_db.hmset('location:', location_mapping) print('Loaded ips: %s/%s' % (lineno, lines)) print('All done!')
def clean_sessions(): while not QUIT: size = redis_db.zcard('recent:') if size <= LIMIT: time.sleep(1) continue end_index = min(size - LIMIT, 100) tokens = redis_db.zrange('recent:', 0, end_index - 1) session_keys = [] for token in tokens: session_keys.append('viewed:' + token) session_keys.append('cart:' + token) redis_db.delete(*session_keys) redis_db.hdel('login:'******'recent:', *token)
def cache_rows(): while not QUIT: next = redis_db.zrange('schedule:', 0, 0, withscores=True) now = time.time() if not next or next[0][1] > now: time.sleep(.05) continue row_id = next[0][0] # 延迟为 0 视为删除 delay = redis_db.zscore('delay:', row_id) if delay <= 0: redis_db.zrem('delay:', row_id) redis_db.zrem('schedule:', row_id) redis_db.delete('inv:' + row_id) continue row = Inventory.get(row_id) redis_db.zadd('schedule:', row_id, now + delay) redis_db.set('inv:' + row_id, json.dumps(row.to_dict()))
def end(): player_id = request.cookies['player_id'] redis_db.delete(player_id) return 'Ok'
def test_redis_connection(self): redis_db.set('foo', 'bar') self.assertEqual(redis_db.get('foo'), 'bar') redis_db.delete('foo')