def exists(gist_id): gist_key = "gist:#"+str(gist_id) last_status = redis.hmget(gist_key, 'status')[0] retry_count = redis.hmget(gist_key, 'retry_count')[0] return last_status == 'OK'
def addRoomCard2Member(redis, transNo): """ 会员增加房卡 """ curTime = datetime.now() orderTable = ORDER_TABLE % (transNo) if not redis.exists(orderTable): log_util.debug('[%s][wechatPay][error] orderNo[%s] is not exists.' % (curTime, params['out_trade_no'])) return False goodid, memberAccount = redis.hmget(orderTable, ('num', 'account')) rType = redis.hget(GOODS_TABLE % goodid, 'type') rType = int(rType) if rType else None if rType == 2: addRoomCard2Member4Type2(redis, curTime, orderTable, memberAccount) return cardNums, present_card = redis.hmget(orderTable, ('roomCards', 'presentCards')) if not present_card: present_card = 0 try: present_card = int(present_card) except: present_card = 0 #chargeNums = TYPE_2_CARS[rType] account2user_table = FORMAT_ACCOUNT2USER_TABLE % (memberAccount ) #从账号获得账号信息,和旧系统一样 userTable = redis.get(account2user_table) groupId = redis.hget(userTable, 'parentAg') #会员ID id = userTable.split(':')[1] pipe = redis.pipeline() pipe.incrby(USER4AGENT_CARD % (groupId, id), (int(cardNums) + present_card)) #记录充值卡总额 if not redis.exists(USER4AGENT_RECHARGE % (groupId, id)): pipe.set(USER4AGENT_RECHARGE % (groupId, id), 0) pipe.incrby(USER4AGENT_RECHARGE % (groupId, id), int(cardNums)) CardMoney = getCardMoney(redis, groupId) log_util.debug('[%s][wechatPay] recharge CardMoney[%s]' % (curTime, CardMoney)) #计算分成金额 countRateOfAgent(redis, groupId, int(cardNums), CardMoney) log_util.debug( '[%s][wechatPay] recharge roomcards[%s] to account[%s] success' % (curTime, cardNums, memberAccount)) roomCards = pipe.execute()[0] pipe = redis.pipeline() ymd = datetime.now().strftime("%Y-%m-%d") useDatas = [int(cardNums), 4, roomCards] useStr = ';'.join(map(str, useDatas)) pipe.lpush(PLAYER_DAY_USE_CARD % (id, ymd), useStr) pipe.expire(PLAYER_DAY_USE_CARD % (id, ymd), SAVE_PLAYER_DAY_USE_CARD_TIME) pipe.execute()
def countRateOfAgent(redis, agentId, roomcardNumber, unitPrice, lowerRate=0): curTime = datetime.now() date = curTime.strftime("%Y-%m-%d") parentTable = AGENT_TABLE % (agentId) parentType, parentrate, parentId = redis.hmget( parentTable, ('type', 'shareRate', 'parent_id')) if not parentrate: AGENT_RATE_TABLE = AGENT_COMPAY_RATE_DATE % (agentId, date) else: AGENT_RATE_TABLE = AGENT_RATE_DATE % (agentId, parentrate, unitPrice, date) redis.hincrby(AGENT_RATE_TABLE, 'number', amount=roomcardNumber) redis.hset(AGENT_RATE_TABLE, 'unitPrice', unitPrice) redis.hset(AGENT_RATE_TABLE, 'rate', parentrate) if parentType == '1': RemainPrice = float(unitPrice) - float(lowerRate) if RemainPrice > 0: redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=RemainPrice * roomcardNumber) else: remainPrice = float(unitPrice) - float(lowerRate) if remainPrice <= 0: countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, lowerRate) else: parent1Type, parent1Rate = redis.hmget(AGENT_TABLE % (parentId), ('type', 'shareRate')) if parent1Type != '1': if parentrate > parent1Rate: parentrate = parent1Rate firstRemainPrice = float(unitPrice) - float(parentrate) if firstRemainPrice <= 0: Rate = float(unitPrice) - float(lowerRate) redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(unitPrice) * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate) else: Rate = float(parentrate) - float(lowerRate) redis.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(parentrate) * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'superRateTotal', amount=firstRemainPrice * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate)
def retry(gist_id, error=None): gist_key = "gist:#"+str(gist_id) last_status = redis.hmget(gist_key, 'status')[0] retry_count = redis.hmget(gist_key, 'retry_count')[0] if retry_count is None or int(retry_count) < 10: redis.hmset(gist_key, dict(status='ERR', message=error)) redis.hincrby(gist_key, 'retry_count') return 'ERR' else: redis.hmset(gist_key, dict(status='DEAD')) return 'DEAD'
def getInfoBySid(redis, sid): """ 通过Sid获取SessionTable, account, uid, verfiySid信息 """ SessionTable = FORMAT_USER_HALL_SESSION % (sid) # 判断SID是否生效 if not SessionTable: client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') redis.hincrby("access:number:ip:hset", client_ip, 1) print(u"远程地址:%s 头部:%s" % (client_ip, request.headers)) if not client_ip or client_ip == "183.60.133.160": return None, None, None, None number = redis.hget("access:number:ip:hset", client_ip) number = int(number) if number else 0 if number >= 3: redis.sadd("remote:disbled:ip:set", client_ip) return None, None, None, None account, uid = redis.hmget(SessionTable, ('account', 'uid')) verfiySid = redis.get(FORMAT_USER_PLATFORM_SESSION % (uid)) curTime = getNowTime() log_util.debug( '[%s][SessionTable][info] account[%s] sessionKey[%s] verfiyKey[%s]' % (curTime, account, sid, verfiySid)) return SessionTable, account, uid, verfiySid
def getInfoBySid(redis, sid): """ 通过Sid获取SessionTable, account, uid, verfiySid信息 """ SessionTable = FORMAT_USER_HALL_SESSION % (sid) account, uid = redis.hmget(SessionTable, ('account', 'uid')) verfiySid = redis.get(FORMAT_USER_PLATFORM_SESSION % (uid)) log_debug('[try getInfoById] account[%s] sessionKey[%s] verfiyKey[%s]' % (account, sid, verfiySid)) return SessionTable, account, uid, verfiySid
def getInfoBySid(redis, sid): """ 通过Sid获取SessionTable, account, uid, verfiySid信息 """ SessionTable = FORMAT_USER_HALL_SESSION % (sid) account, uid = redis.hmget(SessionTable, ('account', 'uid')) verfiySid = redis.get(FORMAT_USER_PLATFORM_SESSION % (uid)) curTime = getNowTime() log_util.debug( '[%s][SessionTable][info] account[%s] sessionKey[%s] verfiyKey[%s]' % (curTime, account, sid, verfiySid)) return SessionTable, account, uid, verfiySid
def getOnlineAIInfos(redis): online_ai_sum = 0 cur_ai_gold_sum = 0 online_ai_room_num_set = set() for key in redis.smembers('users:robot:accounts:set'): online, account, gold= redis.hmget(key, 'isOnline', 'account', 'gold') gold = int(gold) if gold else 0 cur_ai_gold_sum += gold if online == '1': online_ai_sum += 1 if redis.exists(GOLD_ROOM_ACCOUNT_KEY % account): online_ai_room_num_set.add(redis.get(GOLD_ROOM_ACCOUNT_KEY % account)) return online_ai_sum, len(online_ai_room_num_set), cur_ai_gold_sum
def getCardMoney(redis, groupId): """ 会员购卡单价 """ AgentTable = AGENT_TABLE % (groupId) unitPrice, parentId, atype = redis.hmget(AgentTable, 'unitPrice', 'parent_id', 'type') log_util.debug('[HALLFUNC][getCardMoney][info] groupId[%s] price[%s]' % (groupId, unitPrice)) if atype in ['2', '3']: return getCardMoney(redis, parentId) return unitPrice
def addRoomCard2Member(redis, transNo): """ 会员增加房卡 """ curTime = datetime.now() orderTable = ORDER_TABLE % (transNo) if not redis.exists(orderTable): log_util.debug('[%s][wechatPay][error] orderNo[%s] is not exists.' % (curTime, params['out_trade_no'])) return False goodid, memberAccount = redis.hmget(orderTable, ('num', 'account')) rType = redis.hget(GOODS_TABLE % goodid, 'type') rType = int(rType) if rType else None if rType == 2: addRoomCard2Member4Type2(redis, curTime, orderTable, memberAccount) return cardNums, present_card = redis.hmget(orderTable, ('roomCards', 'presentCards')) if not present_card: present_card = 0 try: present_card = int(present_card) except: present_card = 0 #chargeNums = TYPE_2_CARS[rType] account2user_table = FORMAT_ACCOUNT2USER_TABLE % (memberAccount ) #从账号获得账号信息,和旧系统一样 userTable = redis.get(account2user_table) groupId = redis.hget(userTable, 'parentAg') try: redis.hset(orderTable, 'groupId', groupId) except Exception, error: print '[addRoomCard2Member] [ERROR] %s' % (error)
'rateTotal', amount=Rate * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(parentrate) * roomcardNumber) redis.hincrbyfloat(AGENT_RATE_TABLE, 'superRateTotal', amount=firstRemainPrice * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate) agentId = '134253' number = '10' AgentTable = AGENT_TABLE % (agentId) unitPrice, parentId = redis.hmget(AgentTable, 'unitPrice', 'parent_id') if not unitPrice: Agent1Table = AGENT_TABLE % (parentId) unitPrice, parentId = redis.hmget(Agent1Table, 'unitPrice', 'parent_id') print 'unitPrice', unitPrice print 'agentId', agentId if unitPrice: countRateOfAgent(redis, agentId, int(number), unitPrice) print 'success' else: print 'not unitPrice' # AgentTable = AGENT_TABLE%(agentId) # parentId = redis.hget(AgentTable,'parent_id') # AGENT_RATE_TABLE =AGENT_RATE_DATE%('157027',1,price,date)
def get(uid): user = User() attrs = user.__dict__.keys() _user = dict(zip(attrs, redis.hmget('user:%d' % uid, attrs))) user.__dict__.update(_user) return user
import redis redis = redis.StrictRedis(host='localhost', port=6379, db=0, password=8182757) ''' hash value -hset <myhash> <field> <hashvalue> -hget <myhash> <field> -hmset <myhash> <field1> <hashvalue> <field2> <hashvalue2> .. -hmget <myhash> <field1> <field2> .. -hgetall <myhash> ''' #hset print("hset") redis.hset("myhash", "myfield", "hashvalue") #hget print("hget") test = redis.hget("myhash", "myfield") print(test) #hmset print("hmset") redis.hmset("myhash2", {"test1": "test1", "test2": "test2"}) #hmget print("hmget") test2 = redis.hmget("myhash2", {"test1", "test2"}) print(test2)
def addRoomCard2Member4Type2(redis, curTime, orderTable, memberAccount): gold, money = redis.hmget(orderTable, ('roomCards', 'money')) addGold2Merber(redis, memberAccount, money, gold)
def countRateOfAgent(redis, agentId, roomcardNumber, unitPrice, lowerRate=0): log_util.debug( '[HALLFUNC][countRateOfAgent][info] agentId[%s] roomcard[%s] unitPrice[%s]' % (agentId, roomcardNumber, unitPrice)) curTime = datetime.now() date = curTime.strftime("%Y-%m-%d") parentTable = AGENT_TABLE % (agentId) parentType, parentrate, parentId = redis.hmget( parentTable, ('type', 'shareRate', 'parent_id')) if parentType == '0': AGENT_RATE_TABLE = AGENT_COMPAY_RATE_DATE % (agentId, date) else: AGENT_RATE_TABLE = AGENT_RATE_DATE % (agentId, parentrate, unitPrice, date) log_util.debug('[HALLFUNC][countRateOfAgent][info] agentId[%s] parentId[%s] parentrate[%s] parentId[%s] agentRateTable[%s]'\ %(agentId,parentId,parentrate,parentId,AGENT_RATE_TABLE)) pipe = redis.pipeline() pipe.hincrby(AGENT_RATE_TABLE, 'number', amount=roomcardNumber) pipe.hset(AGENT_RATE_TABLE, 'unitPrice', unitPrice) pipe.hset(AGENT_RATE_TABLE, 'rate', parentrate) if parentType == '0': RemainPrice = float(unitPrice) - float(lowerRate) if RemainPrice > 0: pipe.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=RemainPrice * roomcardNumber) else: remainPrice = float(unitPrice) - float(lowerRate) if remainPrice <= 0: countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, lowerRate) else: parent1Type, parent1Rate = redis.hmget(AGENT_TABLE % (parentId), ('type', 'shareRate')) if parent1Type != '0': if parentrate > parent1Rate: parentrate = parent1Rate if not parentrate: parentrate = 0.00 firstRemainPrice = float(unitPrice) - float(parentrate) if firstRemainPrice <= 0: Rate = float(unitPrice) - float(lowerRate) pipe.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) pipe.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(unitPrice) * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate) else: Rate = float(parentrate) pipe.hincrbyfloat(AGENT_RATE_TABLE, 'rateTotal', amount=Rate * roomcardNumber) pipe.hincrbyfloat(AGENT_RATE_TABLE, 'meAndNextTotal', amount=float(parentrate) * roomcardNumber) pipe.hincrbyfloat(AGENT_RATE_TABLE, 'superRateTotal', amount=firstRemainPrice * roomcardNumber) countRateOfAgent(redis, parentId, roomcardNumber, unitPrice, parentrate) pipe.execute()
def hmget(name, ids, sort_field='id'): """docstring for hmget""" if not ids: return [] return [json.loads(r) for r in redis.hmget(name, ids) if r]