def add(cls, user_id, vote_id, option_id): db = DbManager().db cache = CacheManager().cache create_time = datetime.now() date = create_time.date() vote_cache_key = VOTE_PEOPLE_CACHE_KEY % vote_id option_cache_key = VOTE_OPTION_PEOPLE_CACHE_KEY % option_id vote_daily_count_redis_key = USER_DAILY_VOTE_COUNT % (user_id, date) sql = 'insert into {table} (user_id, vote_id, option_id, create_time) values (%s, %s, %s, %s)'.format( table=cls.table) db.execute(sql, user_id, vote_id, option_id, create_time) cache.zadd(vote_cache_key, get_int_date(create_time), user_id) cache.zadd(option_cache_key, get_int_date(create_time), user_id) cache.incr(vote_daily_count_redis_key, 1)
def add(cls, content_id, content_type, user_id, receiver_id): from starmachine.jobs.user import add_content_like, delete_conetnt_like cache = CacheManager().cache create_time = datetime.now() date = create_time.date() score = get_int_date(create_time) key = CONTENT_LIKED_CACHE_KEY % content_id # CONTENT_TYPE_RANK_KEY = CONTENT_RANK_KEY_MAP.get(content_type) user_daily_redis_key = USER_DAILY_RECEIVE_LIKE_COUNT % (receiver_id, date) if cache.zscore(key, user_id): cache.zrem(key, user_id) cache.zincrby(CONTENT_LIKED_RANK_KEY, content_id, -1) # cache.zincrby(CONTENT_TYPE_RANK_KEY, content_id, -1) cache.incr(user_daily_redis_key, -1) delete_conetnt_like.delay(content_id, content_type, user_id) cache.zrem(USER_LIKED_CONTENT_KEY % user_id, content_id) return cache.zcard(key) cache.zadd(key, score, user_id) cache.zadd(USER_LIKED_CONTENT_KEY % user_id, score, content_id) cache.zincrby(CONTENT_LIKED_RANK_KEY, content_id, 1) # cache.zincrby(CONTENT_TYPE_RANK_KEY, content_id, 1) cache.incr(user_daily_redis_key, 1) add_content_like.delay(content_id, content_type, user_id) return cache.zcard(key)
def add(cls, room_id, user_id, status=ROOM_USER_NORMAL): join_time = datetime.now() cache = CacheManager().cache room_user_key = ROOM_USER_CACHE_KEY % room_id user_room_key = USER_ROOM_CACHE_KEY % user_id score = get_int_date(join_time) try: cache.zadd(room_user_key, score, user_id) cache.zadd(user_room_key, score, room_id) logger.info(u'添加房间用户关系到redis成功。') except Exception as e: logger.error(u'添加房间关系用户到redis失败。Error:[%s]' % e) raise add_room_user.delay(room_id, user_id, join_time, status) cache.zincrby(ROOM_USER_COUNT_KEY, room_id) room_increase_rank_key = ROOM_INCREASE_RANK_CACHE_KEY % (join_time.year, join_time.month) cache.zincrby(room_increase_rank_key, room_id, 1) return
def receive_trade_payment(self): db = DbManager().db cache = CacheManager().cache trades = self.trades now = datetime.now() score = get_int_date(now) all_trades_complete = True for trade in trades: if trade.status != STATUS_COMPLETE: all_trades_complete = False break if all_trades_complete: try: db.execute('begin;') for trade in trades: if trade.pay_method == PAYMETHOD_ACCOUNT: sql = 'update {table} set balance=balance-%s where user_id=%s'.format(table=Account.table) db.execute(sql, float(trade.amount), self.creator_id) sql = 'update {table} set balance=balance+%s where user_id=%s'.format(table=Account.table) db.execute(sql, float(self.amount), self.receiver_id) sql = 'update {table} set status=%s, pay_time=%s where id=%s'.format(table=self.table) db.execute(sql, STATUS_COMPLETE, now, self.id) sql = 'insert into {table} (user_id, source, order_type, order_id, amount, create_time) values ' \ '(%s, %s, %s, %s, %s, %s)'.format(table=WalletRecord.table) db.execute(sql, self.creator_id, WALLET_RECORD_REWARD_SEND, ORDER_REWARD, self.id, -self.amount, now) db.execute(sql, self.receiver_id, WALLET_RECORD_REWARD_RECEIVE, ORDER_REWARD, self.id, self.amount, now) db.execute('commit;') except: db.execute('rollback;') raise notify_content_reward.delay(self.creator, Content.get(self.content_id), self) cache.zincrby(CONTENT_REWARD_AMOUNT_CACHE_KEY, self.content_id, float(self.amount) * 100) cache_key = ROOM_USER_REWARD_CACHE_KEY % self.room_id cache.zincrby(cache_key, self.creator_id, float(self.amount) * 100) cache_key = USER_REWARD_RANK_CACHE_KEY cache.zincrby(cache_key, self.creator_id, float(self.amount) * 100) cache_key = CONTENT_REWARD_CACHE_KEY % self.content_id cache.lpush(cache_key, self.id) cache.zadd(USER_REWARD_CONTENT_KEY % self.creator_id, score, self.content_id) self.flush_reward_cache() handle_daily_send_reward.delay(self)
def add(cls, user_id, proverbs_id, receiver_id): cache = CacheManager().cache create_time = datetime.now() date = create_time.date() score = get_int_date(create_time) key = tag_proverbs_liked_key % proverbs_id user_daily_like_count_redis_key = USER_DAILY_RECEIVE_LIKE_COUNT % ( receiver_id, date) if cache.zscore(key, user_id): cache.zrem(key, user_id) cache.zincrby(tag_proverbs_liked_rank, proverbs_id, -1) cache.incr(user_daily_like_count_redis_key, -1) cls.delete_proverbs_like(user_id, proverbs_id) return cache.zcard(key) cache.zadd(key, score, user_id) cache.zincrby(tag_proverbs_liked_rank, proverbs_id, 1) cache.incr(user_daily_like_count_redis_key, 1) cls.add_proverbs_like(user_id, proverbs_id, create_time) return cache.zcard(key)
def add(cls, user_id, group_id, message_id): db = DbManager().db cache = CacheManager().cache create_time = datetime.now() score = get_int_date(create_time) redis_key = GROUP_MESSAGE_LIKED_KEY % (group_id, message_id) group_message_list_key = GROUP_MESSAGE_LIKED_LIST % group_id if cache.zscore(redis_key, user_id): sql = 'delete from {table} where user_id=%s and message_id=%s and group_id=%s'.format( table=cls.table) db.execute(sql, user_id, message_id, group_id) cache.zrem(redis_key, user_id) cache.zincrby(group_message_list_key, message_id, -1) return cache.zcard(redis_key) sql = 'insert into {table} (user_id, group_id, message_id, create_time) values' \ ' (%s, %s, %s, %s)'.format(table=cls.table) db.execute(sql, user_id, group_id, message_id, create_time) cache.zadd(redis_key, score, user_id) cache.zincrby(group_message_list_key, message_id, 1) return cache.zcard(redis_key)
def follow(cls, user_id, follow_id, follow_type): db = DbManager().db cache = CacheManager().cache create_time = datetime.now() score = get_int_date(create_time) follow_type = int(follow_type) user_follow_key = USER_FOLLOW_KEY % user_id user_fans_key = USER_FANS_KEY % follow_id cache.zadd(user_follow_key, score, follow_id) cache.zadd(user_fans_key, score, user_id) db.execute('begin;') try: sql = 'insert into {table} (user_id, follow_id, follow_type, create_time) values (%s, %s, %s, %s)'.format(table=cls.table) db.execute(sql, user_id, follow_id, follow_type, create_time) sql = 'insert into {table} (follow_id, user_id, create_time) values (%s, %s, %s)'.format(table=UserFans.table) db.execute(sql, follow_id, user_id, create_time) if follow_type == FOLLOW_BOTH_TYPE: sql = 'update {table} set follow_type=%s where user_id=%s and follow_id=%s'.format(table=cls.table) db.execute(sql, FOLLOW_BOTH_TYPE, follow_id, user_id) db.execute('commit;') except: db.execute('rollback;') raise
def save_created_content_to_redis(cls, creator_id, content_id, create_time): cache = CacheManager().cache score = get_int_date(create_time) cache_content_key = USER_CREATED_CONTENT_KEY % creator_id cache.zadd(cache_content_key, score, content_id)