python pipeline 支持get和delete操作

前言:pipeline缓存一堆的command,最后一次性执行。但是如果操作过程中需要先到redis中get一次,对数据进行相关处理,最后在进行set/delete等操作,怎么办?

 

看官方文档:https://github.com/andymccurdy/redis-py 

找到pipeline,阅读下面代码

with r.pipeline() as pipe:
     while 1:
         try:
            # put a WATCH on the key that holds our sequence value
            pipe.watch('OUR-SEQUENCE-KEY')
             # after WATCHing, the pipeline is put into immediate execution
             # mode until we tell it to start buffering commands again.
             # this allows us to get the current value of our sequence
             current_value = pipe.get('OUR-SEQUENCE-KEY')
             next_value = int(current_value) + 1
             # now we can put the pipeline back into buffered mode with MULTI
             pipe.multi()
             pipe.set('OUR-SEQUENCE-KEY', next_value)
             # and finally, execute the pipeline (the set command)
             pipe.execute()
             # if a WatchError wasn't raised during execution, everything
             # we just did happened atomically.
             break
        except WatchError:
             # another client must have changed 'OUR-SEQUENCE-KEY' between
             # the time we started WATCHing it and the pipeline's execution.
             # our best bet is to just retry.
             continue


注意到 这句解释# after WATCHing, the pipeline is put into immediate execution mode until we tell it to start buffering commands again.this allows us to get the current value of our sequence

下面我们就知道怎么做了

看代码:

 

          pipe = self._initRedis()
           pipe.watch(session_id) #立即获取到数据而不是等pipeline

          d = pipe.get(session_id) if d is None: pass else: pipe.multi() #重新进入pipeline模式 pipe.delete(session_id) u = Json.loads(d) if u.has_key("userinfo"): pipe.set(session_id,rd) pipe.execute()

 


ok,大功告成

 

 

转载于:https://www.cnblogs.com/sky20081816/archive/2013/05/21/3091125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值