文档章节

Python client for Redis 官翻文档2.10.1(五)

山下狮子
 山下狮子
发布于 2014/06/21 16:12
字数 944
阅读 291
收藏 0

LUA Scripting

LUA脚本

redis-py supports the EVAL, EVALSHA, and SCRIPT commands. However, there are a number of edge cases that make these commands tedious to use in real world scenarios. Therefore, redis-py exposes a Script object that makes scripting much easier to use.

redis-py支持EVAL,EVALSHA和脚本命令。然而,有许多边界情况,使这些命令乏味在真实世界的场景中使用。因此,redis-py暴露一个脚本对象,使脚本更容易使用

To create a Script instance, use the register_script function on a client instance passing the LUA code as the first argument. register_script returns a Script instance that you can use throughout your code.

创建一个脚本实例,使用register_script函数在客户端实例中,LUA代码作为第一个参数传递。register_script返回一个脚本实例,在你的代码中可以继续使用实例操作

The following trivial LUA script accepts two parameters: the name of a key and a multiplier value. The script fetches the value stored in the key, multiplies it with the multiplier value and returns the result.

下面简单LUA脚本接受两个参数:一个关键的名称和一个乘数的值。脚本获取存储在关键的价值,使其与乘数的值并返回结果。

>>> r = redis.StrictRedis()
>>> lua = """
... local value = redis.call('GET', KEYS[1])
... value = tonumber(value)
... return value * ARGV[1]"""
>>> multiply = r.register_script(lua)

multiply is now a Script instance that is invoked by calling it like a function. Script instances accept the following optional arguments:

这个脚本实例现在可以使用,需要传递下面的参数

  • keys: A list of key names that the script will access. This becomes the KEYS list in LUA.

    传递到脚本的关键字列表,在LUA中是KEYS

  • args: A list of argument values. This becomes the ARGV list in LUA.

    参数列表,会是LUA中的ARGV

  • client: A redis-py Client or Pipeline instance that will invoke the script. If client isn't specified, the client that intiially created the Script instance (the one that register_script was invoked from) will be used.

    redis-py客户端或管道实例将调用脚本。如果客户端没有指定,就是默认的。

Continuing the example from above:

继续上面的例子

>>> r.set('foo', 2)
>>> multiply(keys=['foo'], args=[5])
10

The value of key 'foo' is set to 2. When multiply is invoked, the 'foo' key is passed to the script along with the multiplier value of 5. LUA executes the script and returns the result, 10.

Script instances can be executed using a different client instance, even one that points to a completely different Redis server.

>>> r2 = redis.StrictRedis('redis2.example.com')
>>> r2.set('foo', 3)
>>> multiply(keys=['foo'], args=[5], client=r2)
15

The Script object ensures that the LUA script is loaded into Redis's script cache. In the event of a NOSCRIPT error, it will load the script and retry executing it.

Script objects can also be used in pipelines. The pipeline instance should be passed as the client argument when calling the script. Care is taken to ensure that the script is registered in Redis's script cache just prior to pipeline execution.

>>> pipe = r.pipeline()
>>> pipe.set('foo', 5)
>>> multiply(keys=['foo'], args=[5], client=pipe)
>>> pipe.execute()[True, 25]

Sentinel support

redis-py can be used together with Redis Sentinel to discover Redis nodes. You need to have at least one Sentinel daemon running in order to use redis-py's Sentinel support.

Connecting redis-py to the Sentinel instance(s) is easy. You can use a Sentinel connection to discover the master and slaves network addresses:

>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
>>> sentinel.discover_master('mymaster')('127.0.0.1', 6379)
>>> sentinel.discover_slaves('mymaster')[('127.0.0.1', 6380)]

You can also create Redis client connections from a Sentinel instnace. You can connect to either the master (for write operations) or a slave (for read-only operations).

>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
>>> master.set('foo', 'bar')
>>> slave.get('foo')'bar'

The master and slave objects are normal StrictRedis instances with their connection pool bound to the Sentinel instance. When a Sentinel backed client attempts to establish a connection, it first queries the Sentinel servers to determine an appropriate host to connect to. If no server is found, a MasterNotFoundError or SlaveNotFoundError is raised. Both exceptions are subclasses of ConnectionError.

When trying to connect to a slave client, the Sentinel connection pool will iterate over the list of slaves until it finds one that can be connected to. If no slaves can be connected to, a connection will be established with the master.

See Guidelines for Redis clients with support for Redis Sentinel to learn more about Redis Sentinel.

Scan Iterators

The *SCAN commands introduced in Redis 2.8 can be cumbersome to use. While these commands are fully supported, redis-py also exposes the following methods that return Python iterators for convenience: scan_iter, hscan_iter, sscan_iter and zscan_iter.

>>> for key, value in (('A', '1'), ('B', '2'), ('C', '3')):
...     r.set(key, value)>>> for key in r.scan_iter():
...     print key, r.get(key)A 1B 2C 3


© 著作权归作者所有

山下狮子
粉丝 24
博文 115
码字总数 58167
作品 0
东城
程序员
私信 提问
Python client for Redis 官翻文档2.10.1(一)

Python client for Redis key-value store redis针对python的客户端, github地址:https://github.com/andymccurdy/redis-py/ 安装: 虽然称redis的python客户端称为redis-py,实际安装时的......

山下狮子
2014/06/19
1K
0
Python client for Redis 官翻文档2.10.1(二)

更多的细节 连接池: 在幕后,redis-py 使用连接池管理连接到redis-server的连接.默认, 一旦你创建了一个Redis的实例 ,这个实例相应有自己的连接池。你可以重写此行为,在创建一个Redis实例...

山下狮子
2014/06/20
565
0
Python client for Redis 官翻文档2.10.1(三)

Pipelines 管道 管道是Redis的子类,它支持缓冲多个命令,一次性发送到服务器去执行。可以大大的提高性能,减少服务器到客户端之间的TCP来回数据包。 管道的简单使用: >>> r = redis.Redis(....

山下狮子
2014/06/20
1K
0
jumpserver跳板机安装与配置实战

jumpserver安装与配置 参考jumpserver官网http://docs.jumpserver.org/zh/docs/stepbystep.html 一、准备python3和python虚拟环境 1.1安装依赖包 yum -y install wget sqlite-devel xz gcc ......

jxzhfei
2018/08/15
0
0
seaweedfs文件存储服务器搭建

官方网站: https://github.com/chrislusf/seaweedfs/wiki/Getting-Started 概述 seaweedfs是一个非常优秀的由 golang 开发的分布式存储开源项目。它是用来存储文件的系统,并且与使用的语言无...

其实我很dou
2018/09/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

64.监控平台介绍 安装zabbix 忘记admin密码

19.1 Linux监控平台介绍 19.2 zabbix监控介绍 19.3/19.4/19.6 安装zabbix 19.5 忘记Admin密码如何做 19.1 Linux监控平台介绍: 常见开源监控软件 ~1.cacti、nagios、zabbix、smokeping、ope...

oschina130111
今天
13
0
当餐饮遇上大数据,嗯真香!

之前去开了一场会,主题是「餐饮领袖新零售峰会」。认真听完了餐饮前辈和新秀们的分享,觉得获益匪浅,把脑子里的核心纪要整理了一下,今天和大家做一个简单的分享,欢迎感兴趣的小伙伴一起交...

数澜科技
今天
7
0
DNS-over-HTTPS 的下一代是 DNS ON BLOCKCHAIN

本文作者:PETER LAI ,是 Diode 的区块链工程师。在进入软件开发领域之前,他主要是在做工商管理相关工作。Peter Lai 也是一位活跃的开源贡献者。目前,他正在与 Diode 团队一起开发基于区块...

红薯
今天
12
0
CC攻击带来的危害我们该如何防御?

随着网络的发展带给我们很多的便利,但是同时也带给我们一些网站安全问题,网络攻击就是常见的网站安全问题。其中作为站长最常见的就是CC攻击,CC攻击是网络攻击方式的一种,是一种比较常见的...

云漫网络Ruan
今天
12
0
实验分析性专业硕士提纲撰写要点

为什么您需要研究论文的提纲? 首先当您进行研究时,您需要聚集许多信息和想法,研究论文提纲可以较好地组织你的想法, 了解您研究资料的流畅度和程度。确保你写作时不会错过任何重要资料以此...

论文辅导员
今天
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部