Commit a4104b5a authored by 段英荣's avatar 段英荣

modify

parent f3602d5c
......@@ -28,43 +28,42 @@ def home_recommend(session_id="",user_id=-1,offset=0,size=10):
session_id = ""
redis_key = "physical:home_recommend" + ":user_id:" + str(user_id) + ":session_id:" + session_id
redis_field_list = [b'last_offset_num', b'unread_topic_id', b'have_read_topic_id']
redis_field_list = [b'last_offset_num', b'unread_topic_id']
for page_id in range(0,offset,size):
redis_field_list.append(str(page_id))
redis_field_val_list = redis_client.hmget(redis_key,redis_field_list)
logging.info("duan add,redis_field_list:%s,redis_field_val_list:%s" % (str(redis_field_list),str(redis_field_val_list)))
redis_val_dict = redis_client.hgetall(redis_key)
last_offset_num = int(redis_val_dict[b"last_offset_num"]) if b"last_offset_num" in redis_val_dict else -1
#redis_val_dict = redis_client.hgetall(redis_key)
last_offset_num = int(redis_field_val_list[0]) if redis_field_val_list[0] else -1
recommend_topic_ids = []
topic_id_list = TopicUtils.get_recommend_topic_ids(user_id, offset, size*size)
have_read_group_id_tuple = set()
have_read_group_id_set = set()
unread_topic_id_list = list()
have_read_topic_id_set = set()
# 未登录用户已读帖子
unregister_user_have_redis_topic_id_dict = dict()
ori_unread_topic_id_list = list()
if len(redis_val_dict)>0 and offset>0:
if user_id>0 and offset==last_offset_num:
topic_id_list = json.loads(redis_val_dict[b"unread_topic_id"]) + topic_id_list
have_read_topic_id_set = set(json.loads(redis_val_dict[b"have_read_topic_id"]))
elif user_id==-1:
unregister_user_have_redis_topic_id_dict = json.loads(redis_val_dict[b"have_read_topic_id"])
for page_id in unregister_user_have_redis_topic_id_dict:
if int(page_id)<offset:
have_read_topic_id_set.union(set(unregister_user_have_redis_topic_id_dict[page_id]))
if redis_field_val_list[1] and offset>0:
if (user_id>0 and offset==last_offset_num) or user_id==-1:
ori_unread_topic_id_list = json.loads(redis_field_val_list[1])
topic_id_list = ori_unread_topic_id_list + topic_id_list
for have_read_item in redis_field_val_list[2:]:
if have_read_item:
have_read_topic_id_set.union(json.loads(have_read_item))
if user_id==-1:
topic_id_list = list(set(topic_id_list).difference(have_read_topic_id_set))
for item in topic_id_list:
if item["group_id"] in have_read_group_id_tuple:
if item["group_id"] in have_read_group_id_set:
unread_topic_id_list.append(item)
else:
if item["id"] not in have_read_topic_id_set:
recommend_topic_ids.append(item["id"])
if isinstance(item["group_id"],int) and item["group_id"]>0:
have_read_group_id_tuple.add(item["group_id"])
have_read_group_id_set.add(item["group_id"])
have_read_topic_id_set.add(item["id"])
if len(recommend_topic_ids) >= size:
......@@ -76,18 +75,12 @@ def home_recommend(session_id="",user_id=-1,offset=0,size=10):
unread_topic_id_list = unread_topic_id_list[(size-recommend_len):]
redis_have_redis_data = ""
if user_id>0:
redis_have_redis_data = json.dumps(list(have_read_topic_id_set))
else:
unregister_user_have_redis_topic_id_dict[offset] = recommend_topic_ids
redis_have_redis_data = json.dumps(unregister_user_have_redis_topic_id_dict)
if user_id==-1:
unread_topic_id_list = ori_unread_topic_id_list + unread_topic_id_list
redis_dict = {
"unread_topic_id":json.dumps(unread_topic_id_list),
"have_read_topic_id":redis_have_redis_data,
"last_offset_num":offset+size,
offset:recommend_topic_ids
offset: json.dumps(recommend_topic_ids)
}
redis_client.hmset(redis_key,redis_dict)
# 每个session key保存15分钟
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment