简介
HOT Redis
是redis-py
的封装库. 它不需要使用Redis
_指令直接操作客户端. 它模拟了python
内置的如lists, dicts, sets
等数据类型, 以及Queue, threading和collections等模块
提供的标准库类
复杂结构类型, 只需要操作这些类型便可以进行redis操作这些类型都由 Redis 支持, 可以在网络上进行
原子操作
– 关于对象的原子操作
是HOT redis
一个核心功能, 而这些都是由在 Redis 内部运行的Lua
_脚本来提供支持, 用于确保原子操作正常工作.
安装
pip install -U hot-redis
使用
入门: 单机新建
HOT Redis 提供的任何一个类型, 都被尽量按照 python 内置类型来设计
最大的不同在于
__init__
方法, HOT Redis 的__init__
方法可以接受一个初始值, 用来定义并初始化存储于Redis中的对象, 你需要指定一个key
, 当然也可以使用自动生成的 key, key 可以通过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'`
提高: 多机交互
步骤1: 电脑A
>>> list_on_computer_a = List(key="foo", initial=["a", "b", "c"])
步骤2: 电脑B
`>>> list_on_computer_b = List(key="foo")>>> list_on_computer_b[:] # 命令等同于: LRANGE foo 0 -1['a', 'b', 'c']>>> list_on_computer_b += ['d', 'e', 'f'] # 命令等同于: RPUSH foo d e f`
步骤3: 电脑A
` >>> list_on_computer_a[:]['a', 'b', 'c', 'd', 'e', 'f']>>> 'c' in list_on_computer_a # 可以像 python 内置 list 一样使用True>>> list_on_computer_a.reverse()>>> list_on_computer_a[:]['f', 'e', 'd', 'c', 'b', 'a']`
配置
默认情况下, HOT Redis 连接本机的
6379
端口
全局配置: 所有对象使用同一配置
你可以在实例化
所有HOT Redis对象
之前, 通过hot_redis.configure
来指定连接的HOST,PORT
及其他授权信息
`>>> from hot_redis import configure>>> configure(host='myremotehost', port=6380)`
单一配置: 为每一对象指定配置
通过显式使用
HotClient
来创建, 并为每一对象指定HotClient
>>> from hot_redis import HotClient, Queue
>>> client = HotClient(host="myremotehost", port=6380)
>>> my_queue = Queue(client=client)
事务支持
由
MULTI和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 会被合并到一条指令, 在事务上下文退出时, 一次执行
数据类型
HOT Redis 类型与 Python 类型的对应关系
HOT Redis | Python | Redis |
---|---|---|
List | list | list |
Set | set | set |
Dict | dict | hash |
String | string | string |
ImmutableString | string | string |
Int | int | int |
Float | float | float |
Queue | Queue.Queue | list |
LifoQueue | Queue.LifoQueue | list |
SetQueue | N/A | list + set |
LifoSetQueue | N/A | list + set |
BoundedSemaphore | threading.BoundedSemaphore | list |
Semaphore | threading.Semaphore | list |
Lock | threading.Lock | list |
RLock | threading.RLock | list |
DefaultDict | collections.DefaultDict | hash |
MultiSet | collections.Counter | hash |
样例代码
环境: python 3.6
# redis 连接配置
redis_conf = {
'host': 'localhost',
'port': 6379,
'password': '123456',
'socket_timeout': 3,
'socket_connect_timeout': 3,
'db': 0,
}
def ():
"""
测试 hot redis 常用功能
:return:
"""
import hot_redis as rds
import redis
rdb_out = rds.HotClient(**redis_conf)
rd = redis.StrictRedis(**redis_conf)
clear = True
dat = {
'base': 10,
'crx': 2,
'jobbole': 1,
}
# 初始化一个字典
_dict = rds.Dict(client=rdb_out, key='ig.dict')
# 更新, 自动写入 redis
_dict['fns'] = json.dumps(dat)
# 删除字典
if clear:
_dict.clear()
# 测试 list
_list = rds.List(client=rdb_out, key='ig.list')
_list += list(dat.keys())
if clear:
for i in range(len(_list)):
_list.pop()
_string = rds.String(client=rdb_out,
key='ig.string',
initial=','.join([str(_) for _ in dat.keys()])
)
if clear:
rd.delete('ig.string')
网友评论