本文整理汇总了Python中redis.client.Redis.register_script方法的典型用法代码示例。如果您正苦于以下问题:Python Redis.register_script方法的具体用法?Python Redis.register_script怎么用?Python Redis.register_script使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.client.Redis
的用法示例。
在下文中一共展示了Redis.register_script方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HotClient
# 需要导入模块: from redis.client import Redis [as 别名]
# 或者: from redis.client.Redis import register_script [as 别名]
class HotClient(object):
"""
A Redis client wrapper that loads Lua functions and creates
client methods for calling them.
"""
_ATOMS_FILE_NAME = "atoms.lua"
_BIT_FILE_NAME = "bit.lua"
_MULTI_FILE_NAME = "multi.lua"
def __init__(self, client=None, *args, **kwargs):
self._client = client
if not self._client:
kwargs.setdefault('decode_responses', True)
self._client = Redis(*args, **kwargs)
self._bind_atoms()
self._bind_multi()
def _bind_atoms(self):
with open(self._get_lua_path(self._BIT_FILE_NAME)) as f:
luabit = f.read()
requires_luabit = (
"number_and",
"number_or",
"number_xor",
"number_lshift",
"number_rshift"
)
for name, snippet in self._split_lua_file_into_funcs(
self._ATOMS_FILE_NAME):
if name in requires_luabit:
snippet = luabit + snippet
self._bind_lua_method(name, snippet)
def _bind_multi(self):
for name, snippet in self._split_lua_file_into_funcs("multi.lua"):
self._bind_private_lua_script(name, snippet)
@staticmethod
def _get_lua_path(name):
"""
Joins the given name with the relative path of the module.
"""
parts = (os.path.dirname(os.path.abspath(__file__)), "lua", name)
return os.path.join(*parts)
def _split_lua_file_into_funcs(self, file_name):
"""
Returns the name / code snippet pair for each Lua function
in the file under file_name.
"""
with open(self._get_lua_path(file_name)) as f:
for func in f.read().strip().split("function "):
if func:
bits = func.split("\n", 1)
name = bits[0].split("(")[0].strip()
snippet = bits[1].rsplit("end", 1)[0].strip()
yield name, snippet
def _bind_lua_method(self, name, code):
"""
Registers the code snippet as a Lua script, and binds the
script to the client as a method that can be called with
the same signature as regular client methods, eg with a
single key arg.
"""
script = self._client.register_script(code)
method = lambda key, *a, **k: script(keys=[key], args=a, **k)
setattr(self, name, method)
def _bind_private_lua_script(self, name, code):
"""
Registers the code snippet as a Lua script, and binds the
script to the client as a private method (eg. some_lua_func becomes
a _some_lua_func method of HotClient) that can be latter wrapped in
public methods with better argument and error handling.
"""
script = self._client.register_script(code)
setattr(self, '_' + name, script)
def rank_lists_by_length(self, *keys):
"""
Creates a temporary ZSET with LIST keys as entries and their
*LLEN* as scores. Uses ZREVRANGE .. WITHSCORES, to return keys and
lengths sorted from longest to shortests.
:param keys: keys of the lists you want rank
:return: :rtype: Ranking :raise ValueError:
:raise ValueError: when not enough keys are provided
"""
return Ranking(self._rank_lists_by_length, keys)
def rank_sets_by_cardinality(self, *keys):
"""
Creates a temporary ZSET with SET keys as entries and their
*CARD* as scores. Uses ZREVRANGE .. WITHSCORES, to return keys and
cardinalities sorted from largest to smallest.
:param keys: keys of the sets you want to rank
:return: :rtype: Ranking
#.........这里部分代码省略.........