Beispiel #1
0
 def _(*a, **kw):
     key, args = gen_key(*a, **kw)
     start = int(args.pop("start", 0))
     limit = int(args.pop("limit"))
     if not key or limit is None or start + limit > count:
         return f(*a, **kw)
     force = kw.pop("force", False)
     r = rdb.get(key) if not force else None
     if r is None:
         r = f(limit=count, **args)
         r = dumps(r)
         rdb.set(key, r, expire)
     r = loads(r)
     return r[start : start + limit]
Beispiel #2
0
def get_user_feed(from_id, page):
    """ 获取用户的`FEED_KEY`的文章 """
    feed_key = FEED_KEY.format(from_id)
    update_key = ACTIVITY_UPDATED_KEY.format(from_id)
    if not rdb.get(update_key):
        items = ActivityFeed.get_all()
        if items:
            rdb.zadd(feed_key, dict(items))
        rdb.set(update_key, 1, ex=ONE_MINUTE * 5)
    start = (page - 1) * PER_PAGE
    end = start + PER_PAGE - 1
    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)
Beispiel #3
0
def get_user_feed(user_id, page):
    """某用户feed,由关注者发布的和热门分享的混入,每5分钟更新一次。被点赞数超过10时推入热门分享"""
    feed_key = FEED_KEY.format(user_id)
    update_key = ACTIVITY_UPDATE_KEY.format(user_id)
    if not rdb.get(update_key):
        items = ActivityFeed.get_all()
        if items:
            rdb.zadd(feed_key, {int(item[0]): item[1] for item in items})
        rdb.set(update_key, 1, ex=ONE_MINUTE * 5)
    start = (page - 1) * PER_PAGE
    end = start + PER_PAGE - 1
    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)
Beispiel #4
0
        def _(*a, **kw):
            key, args = gen_key(*a, **kw)
            start = args.pop('start', 0)
            limit = args.pop('limit')
            if not key or limit is None or start + limit > count:
                return f(*a, **kw)

            n = 0
            force = kw.pop('force', False)
            d = rdb.get(key) if not force else None
            if d is None:
                n, r = f(limit=count, **args)
                r = dumps(n, r)
                rdb.set(key, (n, r), expire)
            else:
                n, r = loads(d)
                r = loads(r)
            return (n, r[start:start + limit])
Beispiel #5
0
        def _(*a, **kw):
            key, args = gen_key(*a, **kw)
            start = args.pop('start', 0)
            limit = args.pop('limit')
            start = int(start)
            limit = int(limit)
            if not key or limit is None or start + limit > count:
                return f(*a, **kw)

            force = kw.pop('force', False)
            r = rdb.get(key) if not force else None

            if r is None:
                r = f(limit=count, **args)
                r = dumps(r)
                rdb.set(key, r, expire)
            r = loads(r)
            return r[start:start + limit]
Beispiel #6
0
def get_user_latest_posts(uid, visit_id):
    """获取某用户uid最后发布的帖子,并记录这个订阅者visit_id已经查阅过这个uid的哪篇帖子了"""
    user = User.get(uid)
    if not user:
        return

    query = Post.query.with_entities(Post.id, Post.created_at)
    visit_key = LAST_VISIT_KEY.format(uid, visit_id)
    last_visit_id = rdb.get(visit_key)
    if last_visit_id:
        query = query.filter(Post.id > int(last_visit_id))
    else:
        query = query.filter(Post.created_at >= (datetime.now() - timedelta(DAYS)))
    posts = query.order_by(Post.id.desc()).all()

    if posts:
        last_visit_id = posts[0][0]
        rdb.set(visit_key, last_visit_id)
    return posts
Beispiel #7
0
def get_followed_latest_posts(to_id, from_id):
    user = User.get(to_id)
    if not user:
        return

    query = Post.query.with_entities(Post.id, Post.created_at).filter(
        Post.author_id == to_id)  # noqa
    visit_key = LAST_VISIT_KEY.format(to_id, from_id)
    last_visit_id = rdb.get(visit_key)
    if last_visit_id:
        query = query.filter(Post.id > int(last_visit_id))
    else:
        query = query.filter(Post.created_at >=
                             (datetime.now() - timedelta(days=DAYS)))  # noqa

    posts = query.order_by(Post.id.desc()).all()

    if posts:
        last_visit_id = posts[0][0]
        rdb.set(visit_key, last_visit_id)
    return posts
Beispiel #8
0
        def _(*a, **kw):
            key, args = gen_key(*a, **kw)
            if not key:
                return f(*a, **kw)
            force = kw.pop("force", False)
            r = rdb.get(key) if not force else None
            if r is None:
                r = f(*a, **kw)
                if r is not None:
                    if not isinstance(r, BUILTIN_TYPES):
                        r = dumps(r)
                    rdb.set(key, r, expire)
                else:
                    r = dumps(empty)
                    rdb.set(key, r, expire)

            try:
                r = loads(r)
            except (TypeError, UnpicklingError):
                pass
            if isinstance(r, Empty):
                r = None
            return r