使用python怎么批量操作redis數(shù)據(jù)庫-創(chuàng)新互聯(lián)

使用python怎么批量操作redis數(shù)據(jù)庫?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、王益網(wǎng)站維護(hù)、網(wǎng)站推廣。

方法一:使用 pipeline

使用pipelining 發(fā)送命令時(shí),redis server必須部分請(qǐng)求放到隊(duì)列中(使用內(nèi)存)執(zhí)行完畢后一次性發(fā)送結(jié)果,在 pipeline 使用期間,將“獨(dú)占”鏈接,無法進(jìn)行非“管道”類型的其他操作,直至 pipeline 關(guān)閉;如果 pipeline 的指令集很多很龐大,為了不影響其他操作(redis 較大時(shí)間lua-time-limit默認(rèn)是5s),可以使用其他新建新鏈接操作。批量操作如下:

import redis

r = redis.Redis(host='127.0.0.1', port=6379, password='1234567890')
with r.pipeline() as ctx:
  a = time.time()
  ctx.hset('current', "time2", a)
  ctx.hset('current', "time3", a)
  res = ctx.execute()
  print("result: ", res)

使用 pipe line 以樂觀鎖的形式執(zhí)行事務(wù)操作

# -*- coding:utf-8 -*-

import redis
from redis import WatchError
from concurrent.futures import ProcessPoolExecutor

r = redis.Redis(host='127.0.0.1', port=6379)

# 減庫存函數(shù), 循環(huán)直到減庫存完成
# 庫存充足, 減庫存成功, 返回True
# 庫存不足, 減庫存失敗, 返回False
def decr_stock():
  # python中redis事務(wù)是通過pipeline的封裝實(shí)現(xiàn)的
  with r.pipeline() as pipe:
    while True:
      try:
        # watch庫存鍵, multi后如果該key被其他客戶端改變, 事務(wù)操作會(huì)拋出WatchError異常
        pipe.watch('stock:count')
        count = int(pipe.get('stock:count'))
        if count > 0: # 有庫存
          # 事務(wù)開始
          pipe.multi() # multi 判斷 watch 監(jiān)控的 key 是否被其他客戶端改變
          pipe.decr('stock:count')
          # 把命令推送過去
          # execute返回命令執(zhí)行結(jié)果列表, 這里只有一個(gè)decr返回當(dāng)前值
          result = pipe.execute()[0]
          print("result: ", result)
          return True
        else:
          return False
      except WatchError as e:
        # 打印WatchError異常, 觀察被watch鎖住的情況
        print(e.args)
      finally:
        pipe.unwatch()


def worker():
  while True:
    # 沒有庫存就退出
    if not decr_stock():
      break


# 實(shí)驗(yàn)開始
# 設(shè)置庫存為100
r.set("stock:count", 100)

# 多進(jìn)程模擬多個(gè)客戶端提交
with ProcessPoolExecutor(max_workers=2) as pool:
  for _ in range(10):
    pool.submit(worker)

方法二:使用 register_script 

分布執(zhí)行,發(fā)送腳本到redis服務(wù)器,獲取一個(gè)本次連接的一個(gè)調(diào)用句柄,根據(jù)此句柄可以無數(shù)次執(zhí)行不同參數(shù)調(diào)用

import redis
import time

  r = redis.Redis(host='127.0.0.1', port=31320, password='12345678')
  
  lua = """
  local key = KEYS[1]
  local field = ARGV[1]
  local timestamp_new = ARGV[2]
  
  -- get timestamp of the key in redis
  local timestamp_old = redis.call('hget', key, field)
  -- if timestamp_old == nil, it means the key is not exist
  if timestamp_old == nil or timestamp_old == false or timestamp_new > timestamp_old then
    redis.call('hset', key, field .. 1, timestamp_new)
    -- timestamp_new > timestamp_old
    return redis.pcall('hset', key, field, timestamp_new)
  end
  
  """

  cmd = r.register_script(lua)

  cur_time = time.time()
  cmd(keys=['current'], args=["time", cur_time])

register_script 調(diào)用 lua 來實(shí)現(xiàn),需要注意 redis.call(method, key, field) 的返回值(nil,false,1),此處沒有鍵值返回的是false。如果中間有錯(cuò)誤,所有的語句不時(shí)不生效。

方法三:使用 script_load 和 evalsha

簡(jiǎn)而言之,通過 script_load 發(fā)送給redis服務(wù)器,使加載 lua 腳本,并常駐內(nèi)存,返回標(biāo)志,通過 evalsha 按標(biāo)志進(jìn)行執(zhí)行,此連接脫離本次redis 客戶端。

import redis
import time

  r = redis.Redis(host='127.0.0.1', port=31320, password='12345678')
  
  lua = """
  local key = KEYS[1]
  local field = ARGV[1]
  local timestamp_new = ARGV[2]
  
  -- get timestamp of the key in redis
  local timestamp_old = redis.call('hget', key, field)
  -- if timestamp_old == nil, it means the key is not exist
  if timestamp_old == nil or timestamp_old == false or timestamp_new > timestamp_old then
    redis.call('hset', key, field .. 1, timestamp_new)
    -- timestamp_new > timestamp_old
    return redis.pcall('hset', key, field, timestamp_new)
  end
  
  """
  sha = r.script_load(lua)
  print(r.evalsha(sha, 1, 'current', 'time', time.time()))

Redis 管理Lua腳本:(Python下為 script_... )

  • script load

此命令用于將Lua腳本加載到Redis內(nèi)存中

  • script exists

scripts exists sha1 [sha1 …]  
此命令用于判斷sha1是否已經(jīng)加載到Redis內(nèi)存中

  • script flush

此命令用于清除Redis內(nèi)存已經(jīng)加載的所有Lua腳本,在執(zhí)行script flush后,所有 sha 不復(fù)存在。

  • script kill

此命令用于殺掉正在執(zhí)行的Lua腳本。

方法四:eval

使用方法與方法三類似,但是eval是一次性請(qǐng)求,每次請(qǐng)求,必須攜帶 lua 腳本

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

本文標(biāo)題:使用python怎么批量操作redis數(shù)據(jù)庫-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.muchs.cn/article36/ddeosg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、網(wǎng)站制作、網(wǎng)站建設(shè)域名注冊(cè)、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)