使用redis的轻量级、高可用性和可扩展的缓存解决方案
ring_redis的Python项目详细描述
ring redis
===
what for:
--
我想要一个使用redis的轻量级、高可用性和可扩展的缓存解决方案,但是nutcacker对于只有2或3个应用服务器和2或3个redis实例的系统来说太重了。使用纯python实现一致散列还不够好。所以我写了这个。我在2个项目中用过,到现在半年过去了,他们运行良好。因此,我为有相同需求的人分享它。
BR/>特征:
-BR/> BR/> -轻量级和纯Python解决方案
-自动弹出和重新发现ReISS节点
-O(log(SLICEX号))时间复杂度的一致散列计算。(SLICEXIONTION= MAX(2000, 200×NoDeIX号)).HbR/*-O(SLICEXO No.*log(SLICHEL号))Hash环重建的时间复杂度。使用“O(SLICEXO号))内存空间。
API列表:
< BR/> -ReDISCODIGT(ReDISIOBCOSS,前缀=),KEY=STR,Exurie=无,OnAuthOpRe=无,OnNoNoDyEJECTORCENET= NONE,ONNONDEYA重新发现= NONE,retry_ratio=1e-2,hash_function=crc32)`:构造一个redis_dict实例,它可以作为一个普通的python dict
-`some_redis_dict_instance.visit_redis(cmd,k,args)`:访问较低级别的redis api
-`一些redis dict`实例。get` entry(k)`:返回k的真正redis条目
-`一些redis dict`实例。alive`哈希(redis` entry)`:通过alive`哈希返回redis条目的节点名
-`一些redis dict`实例。total`哈希(redis` entry)`:通过total`哈希返回redis`条目的节点名
-`len(some_redis_dict_instance.alive_hash)`: return the alive nodes number
Install
---
### via pip
```shell
pip install ring_redis
```
### via source code
```shell
cd path/to/ring_redis
python setup.py install
```
How to use:
---
```python
################### your redis configuration #####################
REDIS_CONF = {
'group0' : {
'node0': {
'capacity': 50 * 1024 ** 2,
'connection': {
'host' : '192.168.230.45',
'port' : 15061,
'db': 0,
'socket_timeout': 5e-3,
},
},
'node1': {
'capacity': 50 * 1024 ** 2,
'connection': {
'host' : '192.168.230.46',
'port' : 15061,
'db': 0,
'socket_timeout': 5e-3,
},
},
},
}
############################ useage ##############################
from ring_redis import redis_dict
test = redis_dict(REDIS_CONF['group0'], prefix='test.', expire=20)
test['a']='abc'
print(“test['a']:%s”%(test['a'])
print(“len(test):%s”%(len(test)))
print(“test.keys():%s”%(test.keys()[:100])
print(“a”在test中?:%s“%(“a”在测试中))
打印(“b”在测试中?:%s“%(测试中为“b”)
打印(“测试。访问redis('incr','x',1):%s”%(测试。访问redis('incr','x',,)
print(test.get_entry('x'):%s“%(test.get_entry('x')))
print(test.total_散列(test.get_entry('x'))):%s“%(test.total_散列(test.get_entry('x')))))
print(test.alive_散列(test.get_entry('x'))))
print(test.alive_entry('x')))):%s“%(test.alive_散列(test.get_entry('x')))))))))))))))))))/>--
-真正的redis条目等于dict键**只有当**`prefix+key(dict_key)==redis_entry`
-redis_conf**中的配置字段“socket_timeout”应该仔细选择**时,才需要自己做一些测试,以确定预期的时间(以秒为单位,具体取决于网络延迟&;带宽)对于最大的数据实例来说是必需的。
-python内置函数哈希既不一致也不均匀分布,因此**不要将其用作哈希函数。
-如果redis群集的非实例可用,则会引发异常“redisclusterunavailable”,您应该将'on_fail'作为redis_dict构造函数的参数传递,或者捕获此异常以**亲自处理此情况**。
===
what for:
--
我想要一个使用redis的轻量级、高可用性和可扩展的缓存解决方案,但是nutcacker对于只有2或3个应用服务器和2或3个redis实例的系统来说太重了。使用纯python实现一致散列还不够好。所以我写了这个。我在2个项目中用过,到现在半年过去了,他们运行良好。因此,我为有相同需求的人分享它。
BR/>特征:
-BR/> BR/> -轻量级和纯Python解决方案
-自动弹出和重新发现ReISS节点
-O(log(SLICEX号))时间复杂度的一致散列计算。(SLICEXIONTION= MAX(2000, 200×NoDeIX号)).HbR/*-O(SLICEXO No.*log(SLICHEL号))Hash环重建的时间复杂度。使用“O(SLICEXO号))内存空间。
API列表:
< BR/> -ReDISCODIGT(ReDISIOBCOSS,前缀=),KEY=STR,Exurie=无,OnAuthOpRe=无,OnNoNoDyEJECTORCENET= NONE,ONNONDEYA重新发现= NONE,retry_ratio=1e-2,hash_function=crc32)`:构造一个redis_dict实例,它可以作为一个普通的python dict
-`some_redis_dict_instance.visit_redis(cmd,k,args)`:访问较低级别的redis api
-`一些redis dict`实例。get` entry(k)`:返回k的真正redis条目
-`一些redis dict`实例。alive`哈希(redis` entry)`:通过alive`哈希返回redis条目的节点名
-`一些redis dict`实例。total`哈希(redis` entry)`:通过total`哈希返回redis`条目的节点名
-`len(some_redis_dict_instance.alive_hash)`: return the alive nodes number
Install
---
### via pip
```shell
pip install ring_redis
```
### via source code
```shell
cd path/to/ring_redis
python setup.py install
```
How to use:
---
```python
################### your redis configuration #####################
REDIS_CONF = {
'group0' : {
'node0': {
'capacity': 50 * 1024 ** 2,
'connection': {
'host' : '192.168.230.45',
'port' : 15061,
'db': 0,
'socket_timeout': 5e-3,
},
},
'node1': {
'capacity': 50 * 1024 ** 2,
'connection': {
'host' : '192.168.230.46',
'port' : 15061,
'db': 0,
'socket_timeout': 5e-3,
},
},
},
}
############################ useage ##############################
from ring_redis import redis_dict
test = redis_dict(REDIS_CONF['group0'], prefix='test.', expire=20)
test['a']='abc'
print(“test['a']:%s”%(test['a'])
print(“len(test):%s”%(len(test)))
print(“test.keys():%s”%(test.keys()[:100])
print(“a”在test中?:%s“%(“a”在测试中))
打印(“b”在测试中?:%s“%(测试中为“b”)
打印(“测试。访问redis('incr','x',1):%s”%(测试。访问redis('incr','x',,)
print(test.get_entry('x'):%s“%(test.get_entry('x')))
print(test.total_散列(test.get_entry('x'))):%s“%(test.total_散列(test.get_entry('x')))))
print(test.alive_散列(test.get_entry('x'))))
print(test.alive_entry('x')))):%s“%(test.alive_散列(test.get_entry('x')))))))))))))))))))/>--
-真正的redis条目等于dict键**只有当**`prefix+key(dict_key)==redis_entry`
-redis_conf**中的配置字段“socket_timeout”应该仔细选择**时,才需要自己做一些测试,以确定预期的时间(以秒为单位,具体取决于网络延迟&;带宽)对于最大的数据实例来说是必需的。
-python内置函数哈希既不一致也不均匀分布,因此**不要将其用作哈希函数。
-如果redis群集的非实例可用,则会引发异常“redisclusterunavailable”,您应该将'on_fail'作为redis_dict构造函数的参数传递,或者捕获此异常以**亲自处理此情况**。