Dmem ( Distributed memory )是用 python 实现的一个分布式内存的解决方案,把 Python 的基础数据类型映射到 Redis 上,通过一个“透明”的 Redis Client 实现在多个节点上存取数据。
Github:https://github.com/RealHacker/dmem
应用场景:
- 程序对于内存需求较大,单节点无法满足时,无需更改代码逻辑即可支持分布式内存。
- 需要分布式存储多层数据结构,比如一个 list ,其中每个元素是一个 dict 。( Redis 命令只支持单层数据结构)
- 代码洁癖患者,可以使用 Python 原生操作符(
x[y], del x.y, x in y)和方法来处理分布式对象。
废话不多说,看代码:
from dmem import *
# Configure your redis instances pool
RedisClientPool.get_pool().load_config({
"redis1": {"host":"192.168.1.1", "port": 6379, "db":0},
"redis2": {"host":"192.168.1.2", "port": 6379, "db":0},
"redis3": {"host":"192.168.1.3", "port": 6379, "db":0},
})
# Create a list
mylist = RedisList([1, "abc", 3.1415])
# Now the list is alive on Redis
print mylist[1] # actually retrieved with LGET command
# output: 'abc'
print mylist[0:2] # map to LRANGE command
# output: [1, 'abc']
del mylist[0] # map to LREM command
print mylist[:]
# output: ["abc", 3.1415]
s = RedisStr("Redis string")
# Now the string lives in Redis
mylist.append(s) # APPEND command
print len(mylist) # STRLEN command
# output: 3
print mylist[-1].getvalue() # GET command
# output: 'Redis string'
mydict = RedisDict({"a":1234}) # HSET commands
# Now the dict lives in Redis as a hashmap
mydict["list"] = mylist # HGET command
print mydict.keys() # HKEYS command
# output: ['a', 'list']
for k,v in mydict.items(): # HGETALL command
print k, v
# output: a 1234
# output: list <redislist.RedisList object at 0x01CDEF30>
print mydict["list"][0]
# output: 'abc'
obj = RedisObject()
# For redis object, the attributes are stored in Redis
obj.attr1 = "abc"
obj.attr2 = mydict
print obj.attr2['list'][0]
# output: 'abc'
欢迎提建议、报 Bug 。
