def clear_mc(cls, target, amount): tag_id = target.tag_id tag_name = Tag.get(tag_id).name for ident in (tag_id, tag_name): total = incr_key(MC_KEY_GET_COUNT_BY_TAG % ident, amount) pages = math.ceil(max(total, 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(MC_KEY_GET_POSTS_BY_TAG % (ident, p))
def incr_key(stat_key, amount): from corelib.db import rdb from redis.exceptions import ResponseError try: total = rdb.incr(stat_key, amount) except ResponseError: rdb.delete(stat_key) total = rdb.incr(stat_key, amount) return total
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_GET_COUNT_BY_TARGET % (action_type, target_id, target_kind) total = incr_key(stat_key, amount) pages = math.ceil(max(total, 1) / PER_PAGE) user_id = target.user_id rdb.delete(MC_KEY_GET_BY_TARGET % (action_type, user_id, target_id, target_kind)) for p in list(range(1, pages + 1)) + [None]: rdb.delete(MC_KEY_GET_PAGE_BY_TARGET % (action_type, target_id, target_kind, p)) # mc by user stat_key = MC_KEY_GET_COUNT_BY_USER % (action_type, user_id, target_kind) total = incr_key(stat_key, amount) pages = math.ceil(max(total, 1) / PER_PAGE) for p in range(1, pages + 1): rdb.delete(MC_KEY_GET_PAGINATE_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() # 注意,放在sqlalchemy的清理钩子里,会报错 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_GET_FOLLOW_ITEM % (from_id, to_id)) for user_id, total, mc_key in ( (to_id, follower_count, MC_KEY_GET_FOLLOWERS_PAGINATE), (from_id, following_count, MC_KEY_GET_FOLLOWING_PAGINATE)): 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, id, kind): rdb.delete(ITEM_MC_KEY.format(id, kind))
def update(self, **kwargs): rdb.delete(MC_KEY_GET_BY_TITLE % self.title) # 注意,在更新前清除缓存 super().update(**kwargs)
def clear_mc(cls, target): rdb.delete(MC_KEY_GET_BY_TITLE % target.title) rdb.delete(MC_KEY_TAGS % target.id)