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 = incr_key(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(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 % (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 % (user_id, p))
def clear_mc(cls, target, amount): """ 清除和target相关的缓存,修改缓存中MC_KEY_STATS_N的值 """ 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) # ceil 舍弃小数部分向前进1位 user_id = target.user_id rdb.delete(MC_KEY_ACTION_ITEM_BY_UESR % (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_UESR_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 range(1, pages + 1): rdb.delete(MC_KEY_ACTION_ITEMS_BY_UESR % ( action_type, user_id, target_kind, p))
def __flush_event__(cls, target): rdb.delete(MC_KEY_ALL_TAGS)
def clear_mc(cls, id, kind): rdb.delete(ITEM_MC_KEY.format(id, kind))