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,大功告成

  • 相关阅读:
    渚漪Day07——web前端入门【JavaScript02】
    渚漪Day06——web前端入门【HTML补充】【JavaScript01】
    渚漪Day05——注解与反射
    IDEA小知识
    圆覆盖
    数据降维算法——主成分分析
    word Embedding 大杂烩
    fastNLP学习笔记——Vocabulary
    fastNLP学习笔记——Dataset
    Batch Normalization 与 Layer Normalization
  • 原文地址:https://www.cnblogs.com/sky20081816/p/3091125.html
Copyright © 2011-2022 走看看