def order_by_star(cls, start=0, limit=20): ids = cache.lrange(STAR_KEY, start, start + limit) if not ids: ids = [ c.id for c in cls.objects.order_by('-like_count')[:TOTAL_SIZE] ] # noqa cache.delete(STAR_KEY) cache.rpush(STAR_KEY, *ids) ids = ids[start:start + limit] return cls.get_multi(ids)
def search(subject_id, type): key = SEARCH_KEY.format(id=subject_id, type=type) if not subject_id: return [] comment_ids = cache.lrange(key, 0, -1) if comment_ids: return Comment.get_multi(comment_ids) songs = [] if type == 'artist': artist = Artist.get(id=subject_id) songs.extend(Song.objects(artist=artist)) else: songs.extend(Song.get(id=subject_id)) comments = sum([list(Comment.objects(song=song)) for song in songs], []) comment_ids = [comment.id for comment in comments] if comment_ids: cache.rpush(key, *comment_ids) return comments
def cache_by_key(cls, key, ids): """给内部用""" cache.delete(key) cache.rpush(key, *ids) cache.expire(key, TIMEOUT)