def get_group_articles(conn: redis.client.Redis, group: str, page: int, order=SCORE_NAME) -> list: """ 获取组内文章(排序) """ key = order + group if not conn.exists(key): conn.zinterstore( key, [GROUP_NAME + group, order], aggregate='max', ) conn.expire(key, 60) return get_articles(conn, page, key)
def post_article(conn: redis.client.Redis, user: str, title: str, link: str) -> str: """ 添加文章 """ article_id = str(conn.incr(ARTICLE_NAME)) voted = f'{VOTED_NAME}{article_id}' conn.sadd(voted, user) conn.expire(voted, ONE_WEEK_IN_SECONDS) # 设置过去时间 article = f'{ARTICLE_NAME}{article_id}' now = time.time() conn.hmset(article, dict(title=title, link=link, poster=user, time=now, votes=1)) # before # zadd(name,key1,value1,key2,value2) # now # zadd(name,{key1:value1,key2:value2}) conn.zadd(SCORE_NAME, {article: now + VOTE_SCORE}) conn.zadd(TIME_NAME, {article: now}) return article_id