def add_remove_groups(conn: redis.client.Redis, article: str, to_add=[], to_remove=[]) -> None: """ 组别设置 """ for group in to_add: conn.sadd(GROUP_NAME + group, article) for group in to_remove: conn.srem(GROUP_NAME + group, article)
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
def article_vote(conn: redis.client.Redis, user: str, article: str) -> None: """ 用户投票 """ cutoff = time.time() - ONE_WEEK_IN_SECONDS a = conn.zscore(TIME_NAME, article) if conn.zscore(TIME_NAME, article) < cutoff: return article_id = article.partition(SEP)[: -1] # diff between split and partition if conn.sadd(f'{VOTED_NAME}{article_id}', user): # before # zincrby(self, name, value, amount) # now # zincrby(self, name, amount, value) conn.zincrby(SCORE_NAME, VOTE_SCORE, article) # zset score increase conn.hincrby(article, 'votes', 1) # hash value increase