示例#1
0
 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)
示例#2
0
    def get_random_by_session_id(cls, session_id, start=0, limit=20):
        key = RANDOM_KEY.format(session_id=session_id)
        if not start % SAMPLE_SIZE:
            ids = cls.get_sample_ids(SAMPLE_SIZE)
            cls.cache_by_key(key, ids)

        else:
            ids = cache.lrange(key, start, start + limit)
            if not ids:
                ids = cls.get_sample_ids(SAMPLE_SIZE)
                cls.cache_by_key(key, ids)

        comments = cls.get_multi(ids)
        return comments
示例#3
0
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
示例#4
0
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)