def incr_key(stat_key, amount): try: total = rdb.incr(stat_key, amount) except redis.exceptions.ResponseError: rdb.delete(stat_key) total = rdb.incr(stat_key, amount) return total
def clear_mc(target, amount): post_id = target.post_id tag_name = Tag.get(target.tag_id).name for ident in (post_id, tag_name): total = int(PostTag.get_count_by_tag(ident)) rdb.incr(MC_KEY_POST_STATS_BY_TAG % ident, amount) pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(MC_KEY_POSTS_BY_TAG % (ident, p))
def clear_mc(target, amount): tag_name = Tag.get(target.tag_id).name stat_key = MC_KEY_POST_STATS_BY_TAG % tag_name total = int(PostTag.get_count_by_tag(tag_name)) try: rdb.incr(stat_key, amount) except redis.exceptions.ResponseError: rdb.delete(stat_key) rdb.incr(stat_key, amount) pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(MC_KEY_POSTS_BY_TAG % (tag_name, p))
def clear_mc(target, amount): post_id = target.post_id tag_name = Tag.get(target.tag_id).name for ident in (post_id, tag_name): total = int(PostTag.get_count_by_tag(ident)) try: rdb.incr(MC_KEY_POST_COUNT_BY_TAG.format(ident), amount) except redis.exceptions.ResponseError: rdb.delete(MC_KEY_POST_COUNT_BY_TAG.format(ident)) rdb.incr(MC_KEY_POST_COUNT_BY_TAG.format(ident), amount) pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(MC_KEY_POSTS_BY_TAG.format(ident, p))
def clear_mc(cls, target, amount): action_type = cls.action_type assert action_type target_id = target.target_id target_kind = target.target_kind stat_key = MC_KEY_STATS_N % (action_type, target_id, target_kind) total = incr_key(stat_key, amount) pages = math.ceil((max(total, 0) or 1) / PER_PAGE) user_id = target.user_id rdb.delete(MC_KEY_ACTION_ITEM_BY_USER % (action_type, user_id, target_id, target_kind)) for p in list(range(1, pages + 1)) + [None]: rdb.delete(MC_KEY_ACTION_ITEMS % (action_type, target_id, target_kind, p)) # mc by user stat_key = MC_KEY_BY_USER_STATS_N % (action_type, user_id, target_kind) total = incr_key(stat_key, amount) pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in list(range(1, pages + 1)) + [None]: rdb.delete(MC_KEY_ACTION_ITEMS_BY_USER % (action_type, user_id, target_kind, p))
def clear_mc(cls, target, amount): to_id = target.to_id from_id = target.from_id st = userFollowStats.get_or_create(to_id) follower_count = st.follower_count or 0 st.follower_count = follower_count + amount st.save() st = userFollowStats.get_or_create(from_id) following_count = st.following_count or 0 st.following_count = following_count + amount st.save() rdb.delete(MC_KEY_FOLLOW_ITEM.format(from_id, to_id)) for user_id, total, mc_key in ( (to_id, follower_count, MC_KEY_FOLLOWERS), (from_id, following_count, MC_KEY_FOLLOWING), ): pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(mc_key.format(user_id, p))
def clear_mc(cls, target, amount): to_id = target.to_id from_id = target.from_id st = userFollowStats.get_or_create(to_id) follower_count = st.follower_count or 0 st.follower_count = follower_count + amount st.save() st = userFollowStats.get_or_create(from_id) following_count = st.following_count or 0 st.following_count = following_count + amount st.save() # `class:BaseMixin:save` rdb.delete(MC_KEY_FOLLOW_ITEM % (from_id, to_id)) # should be `follower_count + amount`, `following_count + amount`? for user_id, total, mc_key in ((to_id, follower_count, MC_KEY_FOLLOWERS), (from_id, following_count, MC_KEY_FOLLOWING)): pages = math.ceil((max(total, 0) or 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(mc_key % (user_id, p))
def __flush_event__(cls, target): rdb.delete(MC_KEY_ALL_TAGS)
def clear_mc(cls, id, kind): rdb.delete(MC_KEY_ITEM.format(id, kind))
def __flush_event__(cls, target): """BaseModel里面配置了event, 会在增删改的时候把缓存清空""" rdb.delete(MC_KEY_LIKE_N % (target.target_id, target.target_kind))
def clear_mc(cls, id, kind): rdb.delete(ITEM_MC_KEY.format(id, kind))
def __flush_event__(cls, target): rdb.delete(MC_KEY_CONTACT_N % (target.target_id, target.target_kind))
def __flush_event__(cls, target): """BaseModel里面配置了event, 会在增删改的时候把缓存清空""" for key in (MC_KEY_COMMENT_LIST, MC_KEY_COMMENT_N): rdb.delete(key % (target.id, target.kind))