hot-redis
简介
HOT Redis是redis-py的封装库. 它不需要使用Redis_指令直接操作客户端. 它模拟了 python内置的如 lists, dicts, sets等数据类型, 以及Queue, threading和collections等模块提供的标准库类复杂结构类型, 只需要操作这些类型便可以进行redis操作
这些类型都由 Redis 支持, 可以在网络上进行原子操作 – 关于对象的原子操作是HOT redis一个核心功能, 而这些都是由在 Redis 内部运行的 Lua_脚本来提供支持, 用于确保原子操作正常工作.
安装
使用
入门: 单机新建
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[:] ['a', 'b', 'c'] >>> list_on_computer_b += ['d', 'e', 'f']
|
步骤3: 电脑A
>>> list_on_computer_a[:] ['a', 'b', 'c', 'd', 'e', 'f'] >>> 'c' in list_on_computer_a 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)
|
数据类型
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_conf = { 'host': 'localhost', 'port': 6379, 'password': '123456', 'socket_timeout': 3, 'socket_connect_timeout': 3, 'db': 0, } def hot_redis_test(): """ 测试 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') _dict['fns'] = dat if clear: _dict.clear() _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')
|
链接