redis的高阶类型
hot-redis的Python项目详细描述
简介
热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__方法都将 可选地接受initial和key关键字参数,它们是 用于定义要存储在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)
交易
使用 redisMULTI和EXEC命令:
>>> 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)
在上面的例子中,所有的append和put调用都是 成批处理成一个事务,在 {TT16} $上下文被退出。
数据类型
那个下表是hot提供的类型的完整列表 redis,映射到它们的python对应项和底层redis类型, 以及任何值得注意的特别注意事项。
HOT Redis | Python | Redis | Notes |
---|---|---|---|
List | list | list | |
Set | set | set | |
Dict | dict | hash | |
String | string | string | Mutable - string methods that normally create a new string object in Python will mutate the string stored in Redis |
ImmutableString | string | string | Immutable - behaves like a regular Python string |
Int | int | int | |
Float | float | float | |
Queue | Queue.Queue | list | |
LifoQueue | Queue.LifoQueue | list | |
SetQueue | N/A | list + set | Extension of ^{tt17}$ with unique members |
LifoSetQueue | N/A | list + set | Extension of ^{tt18}$ with unique members |
BoundedSemaphore | threading.BoundedSemaphore | list | Extension of ^{tt17}$ leveraging Redis’ blocking list pop operations with timeouts, while using Queue’s ^{tt20}$ arg to provide BoundedSemaphore’s ^{tt21}$ arg |
Semaphore | threading.Semaphore | list | Extension of ^{tt22}$ without a queue size |
Lock | threading.Lock | list | Extension of ^{tt22}$ with a queue size of 1 |
RLock | threading.RLock | list | Extension of ^{tt24}$ allowing multiple ^{tt25}$ calls |
DefaultDict | collections.DefaultDict | hash | |
MultiSet | collections.Counter | hash |