自动补全与输入联想功能已经是大多数网站的标配,给表单加入自动补全功能大大节省了用户输入时间,而输入联想功能则起到了预测用户喜好的作用,两个功能都是提升用户体验的利器。
本实训,我们通过实现搜索历史、自动补全和搜索预测三大常用功能,带领大家编写实用的程序组件。
搜索历史功能
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import redis
conn = redis.Redis()
# 将最新搜索词记录到搜索记录列表中
def add_search_history(user_id, keyword):
# 请在下面完成要求的功能
#********* Begin *********#
history_list = "recent:search:"+user_id
pipe = conn.pipeline()
pipe.multi()
pipe.lrem(history_list, keyword)
pipe.lpush(history_list, keyword)
pipe.ltrim(history_list, 0, 49)
pipe.execute()
#********* End *********#
# 删除搜索记录列表中的指定搜索词
def remove_search_history(user_id, keyword):
# 请在下面完成要求的功能
#********* Begin *********#
conn.lrem(