def ugc_control_set(user_id,obj_type,obj_id,timeout): """ 设置UGC的时间 可以在执行ugc操作之前先过来set 一下,如果返回True则允许下一步 False则因为太频繁不允许ugc """ assert type(user_id) == types.IntType assert type(timeout) == types.IntType q = { 'user_id':user_id, 'obj_type':obj_type, 'obj_id':obj_id } _key = """UGC::%(user_id)s::%(obj_type)s::%(obj_id)s""" % q ret = redis.get(_key) if ret != None: return False else: redis.pipeline().set(_key,'on').expire(_key,timeout).execute() return True
def inter2(*args,**kwargs): if cachekey is not None: callstr = cachekey else: params = map(lambda xx:repr(xx),args) for (k,v) in kwargs.items(): params.append('%s=%s'%(k,repr(v))) callstr='CALLCACHE::%s(%s)' % (func.func_name,','.join(params)) retobj = redis.get(callstr) if retobj: return json.loads(retobj) retobj = func(*args,**kwargs) rp = redis.pipeline() rp.set(callstr,json.dumps(retobj)).expire(callstr,timeout).execute() return retobj
def get_user_id(ukey): """根据ukey来获取user_id""" if not ukey: return None rp = redis.pipeline() rp.exists('USER-UKEY::%s'%ukey) rp.get('USER-UKEY::%s'%ukey) res = iter(rp.execute()) user_id = None if not res.next(): user = db.session.query(User).filter(User.uid == ukey).first() if user is None: return None g.user_id = user_id = user.id rp.set('USER-UKEY::%s'%ukey,user.id) rp.execute() else: g.user_id = user_id = int(res.next()) return user_id
def decorator(*args,**kwargs): ukey = g.ukey if not ukey: return make_response('need a user',403) rp = redis.pipeline() rp.exists('USER-UKEY::%s'%ukey) rp.get('USER-UKEY::%s'%ukey) res = iter(rp.execute()) if not res.next(): # redis 中不存在 插数据库 user = db.session.query(User).filter(User.uid == ukey).first() if user is None: res = make_response('the user does not exist',403) return res g.user_id = user.id rp.set('USER-UKEY::%s'%ukey,user.id) rp.execute() else: g.user_id = int(res.next()) return f(*args,**kwargs)
def add_comment_count(post_id, step=1): count_key = 'POST::COMMENT_COUNT::%s' % str(post_id) if redis.exists(count_key): rp = redis.pipeline() rp.incr(count_key).expire(count_key, 24 * 5 * 3600).execute()
def add_comment_count(post_id,step=1): count_key = 'POST::COMMENT_COUNT::%s' % str(post_id) if redis.exists(count_key): rp = redis.pipeline() rp.incr(count_key).expire(count_key,24*5*3600).execute()