redis支持的一组基本python集合。
redis-collections的Python项目详细描述
redis collections是一个提供高级 接口到Redis,优秀的键值存储。
快速启动
使用pip install redis-collections安装库。 从顶层redis_collections包导入集合。
标准系列
标准集合(例如Dict,List,Set)的行为与 python对应项:
>>>fromredis_collectionsimportDict,List,Set>>>D=Dict()>>>D['answer']=42>>>D['answer']42
Collection | Redis type | Description |
---|---|---|
^{tt3}$ | Hash | Emulates Python’s ^{tt7}$ |
^{tt4}$ | List | Emulates Python’s ^{tt9}$ |
^{tt5}$ | Set | Emulates Python’s ^{tt11}$ |
^{tt12}$ | Hash | Emulates Python’s ^{tt13}$ |
^{tt14}$ | Hash | Emulates Python’s ^{tt15}$ |
^{tt16}$ | List | Emulates Python’s ^{tt17}$ |
可同步的集合
此包中的可同步集合提供 内容保存在内存中。当它们的sync方法被调用时 内容写入redis:
>>>fromredis_collectionsimportSyncableDict>>>withSyncableDict()asD:...D['a']=1# No write to Redis...D['a']+=1# No read from or write to Redis>>>D['a']# D.sync() is called at the end of the with block2
Collection | Python type | Description |
---|---|---|
^{tt19}$ | ^{tt7}$ | Syncs to a Redis Hash |
^{tt21}$ | ^{tt9}$ | Syncs to a Redis List |
^{tt23}$ | ^{tt11}$ | Syncs to a Redis Set |
^{tt25}$ | ^{tt13}$ | Syncs to a Redis Hash |
^{tt27}$ | ^{tt17}$ | Syncs to a Redis List |
^{tt29}$ | ^{tt15}$ | Syncs to a Redis Hash |
其他收藏
LRUDict集合将最近使用的项存储在内存中。 它将旧项目推送到redis:
>>>fromredis_collectionsimportLRUDict>>>D=LRUDict(maxsize=2)>>>D['a']=1>>>D['b']=2>>>D['c']=2# 'a' is pushed to Redis and 'c' is stored locally>>>D['a']# 'b' is pushed to Redis and 'a' is retrieved for local storage1>>>D.sync()# All items are copied to Redis
SortedSetCounter提供对redis的访问 Sorted Set类型:
>>>fromredis_collectionsimportSortedSetCounter>>>ssc=SortedSetCounter([('earth',300),('mercury',100)])>>>ssc.set_score('venus',200)>>>ssc.get_score('venus')200.0>>>ssc.items()[('mercury',100.0),('venus',200.0),('earth',300.0)]
文档
有关详细信息,请参见 redis-collections.readthedocs.io
维护人员
- 博贝勒斯(@bbayles)
- 哈沃雷克酒店(@honzajavorek)