def script_load(script):
sha = [None] #A
def call(conn, keys=[], args=[], force_eval=False): #B
if not force_eval:
if not sha[0]: #C
sha[0] = conn.execute_command( #D
"SCRIPT", "LOAD", script, parse="LOAD") #D
try:
return conn.execute_command( #E
"EVALSHA", sha[0], len(keys), *(keys+args)) #E
except redis.exceptions.ResponseError as msg:
if not msg.args[0].startswith("NOSCRIPT"): #F
raise #F
return conn.execute_command( #G
"EVAL", script, len(keys), *(keys+args)) #G
return call #H
# <end id="script-load"/>
#A Store the cached SHA1 hash of the result of SCRIPT LOAD in a list so we can change it later from within the call() function
#B When calling the "loaded script", you must provide the connection, the set of keys that the script will manipulate, and any other arguments to the function
#C We will only try loading the script if we don't already have a cached SHA1 hash
#D Load the script if we don't already have the SHA1 hash cached
#E Execute the command from the cached SHA1
#F If the error was unrelated to a missing script, re-raise the exception
#G If we received a script-related error, or if we need to force-execute the script, directly execute the script, which will automatically cache the script on the server (with the same SHA1 that we've already cached) when done
#H Return the function that automatically loads and executes scripts when called
#END
ch11_listing_source.py 文件源码
python
阅读 18
收藏 0
点赞 0
评论 0
评论列表
文章目录