def get_top_n(n): ''' :param n: 前十 :return: ''' origin_data = rds.zrevrange('ReadCounter', 0, n - 1, withscores=True) cleaned_data = [[int(post_id), int(count)] for post_id, count in origin_data] # for item in cleaned_data: # post_id = item[0] # post = Post.objects.get(pk=post_id) # item[0] = post post_id_list = [post_id for post_id, _ in cleaned_data] post = Post.objects.filter(pk__in=post_id_list) post = sorted(post, key=lambda post:post_id_list.index(post.id)) for item, post in zip(cleaned_data, post): item[0] = post # post_id_list = [post_id for post_id,_ in cleaned_data] # post = Post.objects.in_bulk(post_id_list) # # print(dir(Post.objects)) # for item in cleaned_data: # post_id = item[0] # item[0] = post[post_id] return cleaned_data
def get_top_n(count): ori_data = rds.zrevrange(READ_COUNT_KEY, 0, count - 1, withscores=True) # [(b'34', 17.0), [[34, 17], # (b'12', 10.0), [12, 10], # (b'2', 9.0), [ 2, 9], # (b'1', 9.0), => [ 1, 9], # (b'20', 8.0), [20, 8], # (b'38', 6.0), [38, 6], # (b'35', 3.0)] [35, 3]] rank_data = [[int(post_id), int(count)] for post_id, count in ori_data] post_id_list = [post_id for post_id, _ in rank_data] # 取出 post_id 列表 # 方法 1 # # post_dict = {1: <Post: Post object>, # # 2: <Post: Post object>, # # 12: <Post: Post object>, # # ...} # post_dict = Post.objects.in_bulk(post_id_list) # post_rank = [[post_dict[pid], count] for pid, count in rank_data] # 方法 2 posts = Post.objects.filter(id__in=post_id_list) posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) post_rank = [[post, rank[1]] for post, rank in zip(posts, rank_data)] return post_rank
def top10(request): print('5678987656789') post_id = int(request.GET.get('post_id', 1)) print(post_id) lists = rds.zrevrange(READ_COUNT, 0, -1, withscores=True) print(lists) return render(request, 'top10.html', {'lists': lists})
def top_n(num): # 从redis中获取原始的数据 ori_data = rds.zrevrange(b'readrank', 0, num - 1, withscores=True) # 清洗数据 将数据装换成整形 cleaned_rank = [[int(post_id), int(count)] for post_id, count in ori_data] # 思路一:直接替换 # for item in cleaned_rank: # item[0] = Post.objects.get(id=item[0]) # rank_data = cleaned_rank # 思路二:批量获取 Post # post_id_list = [post_id for post_id, _ in cleaned_rank] # posts = Post.objects.filter(id__in=post_id_list) # 批量取出 posts # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 调整为正确的顺序 # # 组装 rank_data # rank_data = [] # 遍历获取帖子和点击量 # for post, (_, count) in zip(posts, cleaned_rank): # rank_data.append([post, count]) # 思路三 post_id_list = [post_id for post_id, _ in cleaned_rank] # post_dict = { # 1: <Post: Post object>, # 3: <Post: Post object>, # 29: <Post: Post object>, # } post_dict = Post.objects.in_bulk(post_id_list) # 批量获取 post 字典 for item in cleaned_rank: post_id = item[0] item[0] = post_dict[post_id] rank_data = cleaned_rank return rank_data
def get_top_n(num): ''' 获取阅读计数前n的文章数据 Args: num: 排行前 N Return: rank_data: [ [Post(9),100], [Post(5),91], [Post(7),79], ] ''' # origin_data = [ # (b'1',37.0), # (b'517',16.8), # (b'510',12.0), # ] origin_data = rds.zrevrange('ReadCounter', 0, num - 1, withscores=True) # cleaned_data = [ # [1,37], # [517,16], # [510,12], # ] cleaned_data = [[int(post_id), int(count)] for post_id, count in origin_data] #方法一: # for item in cleaned_data: # post_id = item[0] # post = Post.objects.get(pk=post_id) # item[0] = post #方法二:批量获取,减少对数据库的操作 # post_id_list = [post_id for post_id, _ in cleaned_data] #提取所有的post_id # posts = Post.objects.filter(id__in = post_id_list) # posts = sorted(posts,key=lambda post:post_id_list.index(post.id)) #按照post_id_list 顺序进行排序 # # for item,post in zip(cleaned_data,posts): #zip 起迭代器的作用 # item[0] = post #方法三: post_id_list = [post_id for post_id, _ in cleaned_data] # 提取所有的POST ID posts = Post.objects.in_bulk(post_id_list) for item in cleaned_data: post_id = item[0] item[0] = posts[post_id] return cleaned_data
def get_top_n(num): ori_data = rds.zrevrange(keys.READ_COUNTER, 0, num - 1, withscores=True) rank_data = [[int(post_id), int(count)] for post_id, count in ori_data] post_id_list = [post_id for post_id, _ in rank_data] posts = Post.objects.in_bulk(post_id_list) for item in rank_data: post_id = item[0] item[0] = posts[post_id] return rank_data
def get_top_n(num): ori_data = rds.zrevrange('ReadRank', 0, num - 1, withscores=True) cleaned = [[int(post_id), int(count)] for post_id, count in ori_data] # 方法二 post_id_list = [post_id for post_id, _ in cleaned] posts = Post.objects.filter(id__in=post_id_list) posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) for post, item in zip(posts, cleaned): item[0] = post return cleaned
def get_top_n(num): # 从redis中取出原始数据 ori_data = rds.zrevrange("ReadRank", 0, num - 1, withscores=True) # ori_data = [ # (b'36', 1183.0), # (b'3', 233.0), # (b'37', 164.0), # ... # ] # 数据清洗 cleaned_rank = [[int(post_id), int(count)] for post_id, count in ori_data] # cleaned_rand = [ # [36, 1183], # [ 3, 233], # [37, 164], # ] # 思路一:直接替换 # for item in cleaned_rank: # item[0] = Post.objects.get(id=item[0]) # rank_data = cleaned_rank # 思路二:批量获取 Post post_id_list = [post_id for post_id, _ in cleaned_rank] # 批量取出posts posts = Post.objects.filter(id__in=post_id_list) # 调整为正确的顺序v posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 组装 rank_data rank_data = [] for post, (_, count) in zip(posts, cleaned_rank): rank_data.append([post, count]) # # 思路三 # post_id_list = [post_id for post_id, _ in cleaned_rank] # # post_dict = { # # 1: <Post: Post object>, # # 3: <Post: Post object>, # # 29: <Post: Post object>, # # } # post_dict = Post.objects.in_bulk(post_id_list) # 批量获取 post 字典 # for item in cleaned_rank: # post_id = item[0] # item[0] = post_dict[post_id] # rank_data = cleaned_rank return rank_data
def get_top_n(num): '''取出 Top N 的帖子及其阅读计数''' # ori_data = [ # (b'36', 1183.0), # (b'3', 233.0), # (b'37', 164.0), # ... # ] ori_data = rds.zrevrange(b'ReadRank', 0, num - 1, withscores=True) # 从 redis 取出原始的阅读数据 # cleaned_rank = [ # [36, 1183], # [ 3, 233], # [37, 164], # ] cleaned_rank = [[int(post_id), int(count)] for post_id, count in ori_data] # 数据清洗 # 思路一:直接替换 # for item in cleaned_rank: # item[0] = Post.objects.get(id=item[0]) # rank_data = cleaned_rank # 思路二:批量获取 Post # post_id_list = [post_id for post_id, _ in cleaned_rank] # posts = Post.objects.filter(id__in=post_id_list) # 批量取出 posts # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 调整为正确的顺序 # # 组装 rank_data # rank_data = [] # for post, (_, count) in zip(posts, cleaned_rank): # rank_data.append([post, count]) # 思路三 post_id_list = [post_id for post_id, _ in cleaned_rank] # post_dict = { # 1: <Post: Post object>, # 3: <Post: Post object>, # 29: <Post: Post object>, # } post_dict = Post.objects.in_bulk(post_id_list) # 批量获取 post 字典 for item in cleaned_rank: post_id = item[0] item[0] = post_dict[post_id] rank_data = cleaned_rank return rank_data
def get_top_n(num): #获取排名前n的数据 ''' ori_data = [ (b'33', 344.0), (b'31', 34.0), (b'29', 32.0), ] ''' ori_data = rds.zrevrange('ReadRank', 0, num - 1, withscores=True) #取出原始排名数据 # cleaned_data = [ # [33, 344], # [31, 34], # [29, 32], # ] cleaned_data = [[int(post_id), int(count)] for post_id, count in ori_data] #数据清洗 #方法一 跟数据库交互多 性能不好 # for item in cleaned_data: # item[0] = Post.objects.get(id=item[0]) # rank_data = cleaned_data #方法二 # post_id_list = [post_id for post_id, _ in cleaned_data] # posts = Post.objects.filter(id__in = post_id_list) #批量取出posts # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) #根据id的索引进行排序 # # 拼接rank_data # rank_data = [] # for post, (_, count) in zip(posts, cleaned_data): # rank_data.append([post, count]) #方法三 post_id_list = [post_id for post_id, _ in cleaned_data] # post_dict = { # 1: <Post: Post object>, # 2: <Post: Post object>, # 3: <Post: Post object>, # } post_dict = Post.objects.in_bulk(post_id_list) for item in cleaned_data: item[0] = post_dict[item[0]] rank_data = cleaned_data return rank_data
def get_top_n(num): '''获取排名数据''' # ori_data = [ # (b'33', 843.0), # ( b'1', 384.0), # (b'29', 261.0), # ] ori_data = rds.zrevrange('ReadRank', 0, num - 1, withscores=True) # 取出原始排名数据 # cleaned_data = [ # [33, 1779], # [ 1, 384], # [29, 261], # ] cleaned_data = [[int(post_id), int(count)] for post_id, count in ori_data] # 数据清洗 # 方法一 # for item in cleaned_data: # item[0] = Post.objects.get(id=item[0]) # rank_data = cleaned_data # 方法二 # post_id_list = [post_id for post_id, _ in cleaned_data] # posts = Post.objects.filter(id__in=post_id_list) # 批量取出 posts # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 按照id的索引进行排序 # # 拼装 rank_data # rank_data = [] # for post, (_, count) in zip(posts, cleaned_data): # rank_data.append([post, count]) # 方法三 post_id_list = [post_id for post_id, _ in cleaned_data] # post_dict = { # 1: <Post: Post object>, # 2: <Post: Post object>, # 3: <Post: Post object>, # } post_dict = Post.objects.in_bulk(post_id_list) for item in cleaned_data: item[0] = post_dict[item[0]] rank_data = cleaned_data return rank_data
def get_top_n(num): '''获取阅读排行前 N 的数据''' # ori_data = [ # (b'31', 507.0), # (b'2', 88.0), # (b'34', 24.0), # ] ori_data = rds.zrevrange('ReadRank', 0, num - 1, withscores=True) # 取出原始数据 # rank_data = [ # [31, 507], # [ 2, 88], # [34, 24], # ] rank_data = [[int(post_id), int(count)] for post_id, count in ori_data] # 清洗原始数据 # 方法一:思路简单,效率低,性能差 # for item in rank_data: # post = Post.objects.get(pk=item[0]) # 替换每一项的第一个元素 # item[0] = post # 方法二 # post_id_list = [post_id for post_id, _ in rank_data] # 批量取出 id 列表 # posts = Post.objects.filter(id__in=post_id_list) # 批量获取 post # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 重新按阅读量排序 # # 按位置逐一替换 # for post, item in zip(posts, rank_data): # item[0] = post # 方法三 post_id_list = [post_id for post_id, _ in rank_data] # 批量取出 id 列表 # posts = { # 2: <Post: Post object>, # 3: <Post: Post object>, # 25: <Post: Post object>, # } posts = Post.objects.in_bulk(post_id_list) # 批量获取 post # 按位置逐一替换 for item in rank_data: item[0] = posts[item[0]] return rank_data
def get_top_n(num): '''获取阅读排行前 N 的文章''' # ori_data = [ # (b'16', 53.0), # (b'27', 39.0), # (b'31', 37.0), # ] ori_data = rds.zrevrange('ReadCounter', 0, num - 1, withscores=True) # cleaned_data = [ # [16, 53], # [27, 39], # [31, 37], # ] cleaned_data = [[int(post_id), int(count)] for post_id, count in ori_data] # 方法一 for item in cleaned_data: key = 'Post-%s' % item[0] post = cache.get(key) if post is None: post = Post.objects.get(pk=item[0]) item[0] = post # 方法二 # post_id_list = [post_id for post_id, _ in cleaned_data] # posts = Post.objects.filter(id__in=post_id_list) # posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 按照post_id_list的顺序进行排序 # for index, item in enumerate(cleaned_data): # item[0] = posts[index] # for post, item in zip(posts, cleaned_data): # item[0] = post # 方法三 post_id_list = [post_id for post_id, _ in cleaned_data] posts = Post.objects.in_bulk(post_id_list) for item in cleaned_data: post_id = item[0] item[0] = posts[post_id] return cleaned_data
def get_top_n(num): '''获取排行前 N 的数据''' # ori_data = [ # (b'38', 369.0), # (b'37', 216.0), # (b'40', 52.0), # ] ori_data = rds.zrevrange('ReadRank', 0, num - 1, withscores=True) # 数据清洗 # cleaned = [ # [38, 369], # [37, 216], # [40, 52], # ] cleaned = [[int(post_id), int(count)] for post_id, count in ori_data] # 方法一:循环操作数据库,性能差 # for item in cleaned: # post = Post.objects.get(pk=item[0]) # item[0] = post # 方法二 post_id_list = [post_id for post_id, _ in cleaned] # 取出 post id 列表 posts = Post.objects.filter(id__in=post_id_list) # 根据 id 批量获取 post posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) # 根据 id 位置排序 for post, item in zip(posts, cleaned): item[0] = post # 逐个替换 post # 方法三 # post_id_list = [post_id for post_id, _ in cleaned] # 取出 post id 列表 # # posts = { # # 1: <Post: Post object>, # # 4: <Post: Post object>, # # 6: <Post: Post object>, # # } # posts = Post.objects.in_bulk(post_id_list) # 批量获取 post # for item in cleaned: # item[0] = posts[item[0]] return cleaned
def get_top_n(count): ori_data = rds.zrevrange(READ_COUNT_KEY, 0, count - 1, withscores=True) # [(b'3', 24.0), (b'2', 18.0), (b'5', 15.0), (b'38', 10.0), (b'1', 6.0)] # [[3, 24], [2, 18], [5, 15], [38, 10], [1, 6]] rank_data = [[int(post_id), int(count)] for post_id, count in ori_data] post_id_list = [post_id for post_id, _ in rank_data] # 方法 1 # # post_dict = {1: <Post: Post object>, # # 2: <Post: Post object>, # # 3: <Post: Post object>, # # 5: <Post: Post object>, # # 38: <Post: Post object>, # # ...} # post_dict = Post.objects.in_bulk(post_id_list) # post_rank = [[post_dict[pid], count] for pid, count in rank_data] # 方法 2 posts = Post.objects.filter(id__in=post_id_list) posts = sorted(posts, key=lambda post: post_id_list.index(post.id)) post_rank = [[post, rank[1]] for post, rank in zip(posts, rank_data)] return post_rank
def get_top_n(n): # 获取排名原始数据 # [ # (b'28', 3.0), # (b'26', 3.0), # (b'31', 2.0), # (b'35', 1.0), # (b'30', 1.0) # ] ori_data = rds.zrevrange(keys.READ_RANK, 0, n - 1, withscores=True) # rank_data =[] # for data in ori_data: # post_id = int(data[0]) # count = int(data[1]) # rank_data.append([post_id, count]) # ====================================== # rank_data = [ # (28, 3), # (26, 3), # (31, 2), # (35, 1), # (30, 1), # ] rank_data = [[int(data[0]), int(data[1])] for data in ori_data] # 获取对应的 Post # for data in rank_data: # post = Post.objects.get(id=data[0]) # data[0] = post id_list = [post_id for post_id, _ in rank_data] posts = Post.objects.filter(id__in=id_list) posts = sorted(posts, key=lambda post: id_list.index(post.id)) # 整理数据格式 rank_data = [(post, data[1]) for post, data in zip(posts, rank_data)] return rank_data
def get_top_n(num): '''获取阅读量前 N 的文章''' # ori_data = [ # (b'38', 321.0), # (b'1', 239.0), # (b'39', 111.0) # ... # ] ori_data = rds.zrevrange(b'ReadRank', 0, num - 1, withscores=True) # 取出原始排名数据 # rank_data = [ # [1, 1035], # [38, 687], # [39, 214], # ] rank_data = [[int(post_id), int(count)] for post_id, count in ori_data] # 数据清洗 # 批量获取 post 对象 post_id_list = [post_id for post_id, _ in rank_data] # 取出每一个 post_id posts = Post.objects.filter(id__in=post_id_list) # filter 的结果会按 id 生序排列 posts = sorted( posts, key=lambda p: post_id_list.index(p.id)) # 重新整理成按 post_id_list 的顺序排列 # 修改 rank_data 每一项的第一个元素为 post 对象 # rank_data = [ # [<Post(1)>, 1035], # [<Post(38)>, 687], # [<Post(39)>, 214], # ... # ] for item, post in zip(rank_data, posts): item[0] = post return rank_data