redis的高阶类型

hot-redis的Python项目详细描述


https://secure.travis-ci.org/stephenmcd/hot-redis.png?branch=master

Stephen McDonald

创建

简介

热redis是redis-py客户端的包装库。而不是 直接从客户端库hot redis调用Redis命令 提供多种数据类型,模拟许多内置的 python提供的数据类型,如list、dict、set等 以及标准库中的许多类,例如 如队列、线程和集合模块中所示。

然后这些类型由redis支持,允许对象 通过网络进行原子操作的 在热redis中对对象实现的方法是其核心之一。 特性,其中许多特性都由在中执行的Lua代码支持 redis,在适用的情况下确保原子操作。

hot redis最初代表“redis的高阶类型”, 但由于实现并不严格符合定义, 递归首字母缩略词“Hot Object Toolkit for Redis”应该满足 最甜美的胡子脖子。

热redis是从 Kouio RSS reader,google阅读器的流行替代品。

安装

安装hot-redis的最简单方法是直接安装 从pypi使用pip运行以下命令:

$ pip install -U hot-redis

否则,您可以直接从源代码下载并安装它:

$ python setup.py install

用法

热redis提供的每种类型都力求实现相同的 方法签名和返回值作为其python内置和 标准库副本。主要的区别是每种类型的 __init__方法。每个热redis类型的__init__方法都将 可选地接受initialkey关键字参数,它们是 用于定义要存储在redis中的 对象和应分别使用的键。如果没有钥匙 如果生成密钥,则可以通过 key属性:

>>> from hot_redis import List
>>> my_list = List()
>>> my_list.key
'93366bdb-90b2-4226-a52a-556f678af40e'
>>> my_list_with_key = List(key="foo")
>>> my_list_with_key.key
'foo'

一旦确定了命名密钥的策略,就可以创建 热redis对象并通过网络与它们交互,例如 这是在计算机上创建的List,我们称之为计算机a:

>>> list_on_computer_a = List(key="foo", initial=["a", "b", "c"])

然后在另一台计算机上,我们将创造性地称为计算机B:

>>> list_on_computer_b = List(key="foo")
>>> list_on_computer_b[:]  # Performs: LRANGE foo 0 -1
['a', 'b', 'c']
>>> list_on_computer_b += ['d', 'e', 'f']  # Performs: RPUSH foo d e f

然后回到计算机A:

>>> list_on_computer_a[:]  # Performs: LRANGE foo 0 -1
['a', 'b', 'c', 'd', 'e', 'f']
>>> 'c' in list_on_computer_a  # Works like Python lists where expected
True
>>> list_on_computer_a.reverse()
>>> list_on_computer_a[:]
['f', 'e', 'd', 'c', 'b', 'a']

最后一个互动是有趣的。Python的 list.reverse()是列表的就地反转,即 修改现有列表,而不是返回反向副本。如果 我们很天真地实现了这个,我们首先从 redis,在本地反转,然后将反转的列表存储回redis 再一次。但是如果另一个客户在 大致相同的时间?一台计算机对列表的修改 当然会覆盖另一个的。在这个场景中,many 另外,热redis提供了自己的lua例程,专门用于 在redis中原子性地反转列表。我写了更多 在博客文章Bitwise Lua Operations in Redis中详细介绍了这一点。

配置

默认情况下,热redis尝试连接到运行 在默认端口6379上本地。您可以配置默认客户端 通过在实例化之前调用hot_redis.configure函数 任何热的redis对象。传递给configure的参数 在底层的redis-py客户机上:

>>> from hot_redis import configure
configure(host='myremotehost', port=6380)

或者,如果希望为每个对象使用不同的客户端,则 可以显式创建一个HotClient实例,并将其传递给每个 对象:

>>> from hot_redis import HotClient, Queue
>>> client = HotClient(host="myremotehost", port=6380)
>>> my_queue = Queue(client=client)

交易

使用 redisMULTIEXEC命令:

>>> from hot_redis import List, Queue, transaction
>>> my_list = List(key="foo")
>>> my_queue = Queue(key="bar")
>>> with transaction():
...     for i in range(20):
...         my_list.append(i)
...         my_queue.put(i)

在上面的例子中,所有的appendput调用都是 成批处理成一个事务,在 {TT16} $上下文被退出。

数据类型

那个下表是hot提供的类型的完整列表 redis,映射到它们的python对应项和底层redis类型, 以及任何值得注意的特别注意事项。

HOT RedisPythonRedisNotes
Listlistlist
Setsetset
Dictdicthash
StringstringstringMutable - string methods that normally create a new string object in Python will mutate the string stored in Redis
ImmutableStringstringstringImmutable - behaves like a regular Python string
Intintint
Floatfloatfloat
QueueQueue.Queuelist
LifoQueueQueue.LifoQueuelist
SetQueueN/Alist + setExtension of ^{tt17}$ with unique members
LifoSetQueueN/Alist + setExtension of ^{tt18}$ with unique members
BoundedSemaphorethreading.BoundedSemaphorelistExtension of ^{tt17}$ leveraging Redis’ blocking list pop operations with timeouts, while using Queue’s ^{tt20}$ arg to provide BoundedSemaphore’s ^{tt21}$ arg
Semaphorethreading.SemaphorelistExtension of ^{tt22}$ without a queue size
Lockthreading.LocklistExtension of ^{tt22}$ with a queue size of 1
RLockthreading.RLocklistExtension of ^{tt24}$ allowing multiple ^{tt25}$ calls
DefaultDictcollections.DefaultDicthash
MultiSetcollections.Counterhash

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
不同窗口中的java视图   java创建SQL插入语句到CSV文件   java效率检查:Opengl动画代码   在clojure中处理Java可选<T>   java理解camel中的输入/输出交换模式行为   对于使用jpackage构建的应用程序,java LSOpenURLsWithRole()失败,错误为10810   多线程Java同步:多重倒计时闩锁   java哪个类应该做这项工作?   java在运行时出现问题。getRuntime()。执行官   java我们不能在GAE中使用集合或集合作为返回类型吗?   amazon web服务返回类型与RequestHandler<Object,String>不兼容。JAVA中的HandlerRequest(对象、上下文)   如何在Java中使用ExecutorService设置任务的超时时间