zoukankan      html  css  js  c++  java
  • 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,大功告成

  • 相关阅读:
    忙碌进度条
    WPF异步调用WCF
    Oracle 数据类型及存储方式(二)
    C# 与Oracle Data Type Mappings
    【CEO来信】任正非寄语2010:开放、妥协与灰度
    数据库数据类型
    asccii 表
    SQlServer2008 表结构查询
    Oracle 数据类型及存储方式(六)
    应用特征备份
  • 原文地址:https://www.cnblogs.com/sky20081816/p/3091125.html
Copyright © 2011-2022 走看看