def update_course_memcached(id): """ 更新特定id课程的缓存 """ course = Courses.query.get_or_404(id) lru.delete(course) lru.set(course.to_json(), [course.name, course.teacher]) lru.save()
def read_data2_memcached(): """ 将postgresql数据库中的课程搜索数据 读入LRU并写入硬盘(数据库读取开销) """ courses = Courses.query.all() for course in courses: lru.set(course.to_json(), [course.name, course.teacher]) lru.save() # 数据存储硬盘
def category_catch(keywords, main_cat_id=0, ts_cat_id=0): """ 类别筛选 """ category = {1: '公共课', 2: '通识课', 3: '专业课', 4: '素质课'}.get(main_cat_id) subcategory = {1: '通识核心课', 2: '通识选修课'}.get(ts_cat_id) if category and not subcategory: gen = (course_json for course_json in lrukeys \ if eval(course_json).get('main_category') == category) elif subcategory: gen = (course_json for course_json in lrukeys \ if eval(course_json).get('sub_category') == subcategory) else: gen = lrukeys results = [] courses = [] sort = {} for course_json in gen: #searchs = lru.get(course_json) #不再从lru中拿,而是直接从course_json中获取 #因为从redis中获取时间太长(每个0.04s, 100个课4秒) searchs = [] course_json = eval(course_json) searchs.append(course_json['title']) searchs.append(course_json['teacher']) searchs.append(course_json['hot_tags']) searchs = str(searchs) course_json = str(course_json) if keywords in eval(searchs)[0]: sort[course_json] = kmp(eval(searchs)[0], keywords) sorted_list = sorted(sort.iteritems(), key=lambda d: d[1]) results = [eval(c_json[0]) for c_json in sorted_list] elif keywords in eval(searchs)[1]: results.append(eval(course_json)) elif len(eval(searchs)) == 3 and \ keywords == eval(searchs)[2]: results.append(eval(course_json)) if len(results) == 0: tag = Tags.query.filter_by(name=keywords).first() if tag: course_tags = tag.courses.all() for course_tag in course_tags: courses.append(Courses.query.get_or_404(course_tag.course_id)) for course in courses: # here I set memcache lru.set(course.to_json(), [course.name, course.teacher, keywords]) results.append(course.to_json()) return results
def category_catch(keywords, main_cat_id=0, ts_cat_id=0): """ 类别筛选 """ category = { 1: '公共课', 2:'通识课', 3: '专业课', 4:'素质课' }.get(main_cat_id) subcategory = { 1: '通识核心课', 2: '通识选修课' }.get(ts_cat_id) if category and not subcategory: gen = (course_json for course_json in lru.keys() \ if eval(course_json).get('main_category') == category) elif subcategory: gen = (course_json for course_json in lru.keys() \ if eval(course_json).get('sub_category') == subcategory) else: gen = lru.keys() results = []; courses = []; sort = {}; for course_json in gen: searchs = lru.get(course_json) if keywords in eval(searchs)[0]: sort[course_json] = kmp(eval(searchs)[0], keywords) sorted_list = sorted(sort.iteritems(), key=lambda d: d[1]) results = [eval(c_json[0]) for c_json in sorted_list] elif keywords in eval(searchs)[1]: results.append(eval(course_json)) elif len(eval(searchs)) == 3 and \ keywords == eval(searchs)[2]: results.append(eval(course_json)) if len(results) == 0: tag = Tags.query.filter_by(name=keywords).first() if tag: course_tags = tag.courses.all() for course_tag in course_tags: courses.append(Courses.query.get_or_404(course_tag.course_id)) for course in courses: # here I set memcache lru.set(course.to_json(), [course.name, course.teacher, keywords]) results.append(course.to_json()) return results