Exemple #1
0
def feed_post(post):
    user = User.get(post.author_id)
    for uids in gen_followers(post.author_id):
        for visit_id in uids:
            key = FEED_KEY.format(visit_id)
            rdb.zadd(key, int(post.created_at.strftime('%s')), post.id)
            visit_key = LAST_VISIT_KEY.format(user.id, visit_id)
            rdb.set(visit_key, post.id)
Exemple #2
0
def feed_to_followers(visit_id, uid):
    posts = get_user_latest_posts(uid, visit_id)
    if not posts:
        return
    items = []
    for post_id, created_at in posts:
        items.extend([post_id, int(created_at.strftime('%s'))])
    key = FEED_KEY.format(visit_id)
    rdb.zadd(key, *items)
Exemple #3
0
def get_user_feed(user_id, page):
    feed_key = FEED_KEY.format(user_id)
    update_key = ACTIVITY_UPDATED_KEY.format(user_id)
    if rdb.get(update_key):
        items = ActivityFeed.get_all()
        if items:
            rdb.zadd(feed_key, *sum([(int(time), id) for id, time in items],
                                    ()))  # noqa
        rdb.set(update_key, 1, ex=ONE_MINUTE * 5)
    start = (page - 1) * PER_PAGE
    end = start + PER_PAGE
    post_ids = rdb.zrange(feed_key, start, end)
    items = Post.get_multi([int(id) for id in post_ids])
    total = rdb.zcard(feed_key)
    return Pagination(None, page, PER_PAGE, total, items)
Exemple #4
0
 def add(time, post_id):
     rdb.zadd(ACTIVITY_KEY, time, post_id)
     rdb.zremrangebyrank(ACTIVITY_KEY, MAX, -1)  # 只保留前200个热门文章
Exemple #5
0
        id for id, in Post.query.with_entities(Post.id).filter(
            Post.author_id == uid)
    ]
    key = FEED_KEY.format(visit_id)
    rdb.zrem(key, *post_ids)


def get_user_feed(user_id, page):
    feed_key = FEED_KEY.format(user_id)
    update_key = ACTIVITY_UPDATED_KEY.format(user_id)
    if rdb.get(update_key):
        items = ActivityFeed.get_all()
        if items:
            rdb.zadd(feed_key, *sum([(int(time), id) for id, time in items],
                                    ()))  # noqa
        rdb.set(update_key, 1, ex=ONE_MINUTE * 5)
    start = (page - 1) * PER_PAGE
    end = start + PER_PAGE
    post_ids = rdb.zrange(feed_key, start, end)
    items = Post.get_multi([int(id) for id in post_ids])
    total = rdb.zcard(feed_key)
    return Pagination(None, page, PER_PAGE, total, items)


if __name__ == "__main__":
    my_self = f'liu_zhi-feed_key:1'
    items = {'a1': 6, 'b1': 7, 'gx': 1}
    rdb.zadd(my_self, mapping=items)
    _ = rdb.zrange(my_self, 0, -1)
    print(_)