Python库,用于在缓存存储上存储连接的节点及其属性
graphcache的Python项目详细描述
笔画
在缓存存储(redis)上存储连接的节点及其属性的Python库
安装
要安装graphcache
,只需:
pip install graphcache
发展
基本用途
确保redis
服务正在运行
在macos上
redis-server /usr/local/etc/redis.conf
要使用graphcache,必须首先创建graphcache的实例, 并构造节点和边:
# Import GraphCachefromgraphcacheimportGraphCache# default # host = localhost# port = 6379# db = 0g=GraphCache(host='localhost',port=6379,db=0)# Add optimisation keysg.optimise_for('bananas')g.optimise_for('apples')# Create Nodes (need 'apples', 'bananas' keys in all nodes, as they have been added to optimisation_keys)n1=g.add_vertex({'name':'Tom','age':24,'bananas':5,'apples':4})n2=g.add_vertex({'name':'Bob','bananas':0,'apples':8})n3=g.add_vertex({'name':'Harry','gender':'Male','bananas':3,'apples':1})n4=g.add_vertex({'name':'Jill','bananas':8,'apples':1})# Connect themg.add_edge(g.entry,n2)# g.entry specifies the entry point for graphg.add_edge(g.entry,n3)g.add_edge(n2,n3)g.add_edge(n3,n4)g.add_edge(n2,n1)
然后,您可以对任何节点执行筛选/排序操作,以从该节点获取所需的相邻节点:
# Filter By# Get all outgoing nodes (only adjacent) from n2 which have only 1 applesnodes1=n2.get_outgoing().filter_by('apples',[1]).get_all_nodes()# Sort By# Get all incoming nodes (only adjacent) to n3 sorted by number of bananas they havenodes2=n3.get_incoming().sort_by('bananas').get_all_nodes()# Get first incoming node to n1 sorted by bananasnode1=n1.get_incoming().sort_by('bananas').get_node_indexed_at(0)
您还可以执行连锁的复杂操作:
# Filter By, Sort By# Get all outgoing nodes (only adjacent) from n2 with apples less than 5 and sorted by bananasnodes1=n2.get_outgoing().filter_by('apples',5,"lt").sort_by('bananas').get_all_nodes()# Sort By, Filter By, Filter Bynodes2=n3.get_incoming().sort_by('bananas').filter_by('bananas',[1,5],"in").filter_by('apples',[1]).get_all_nodes()
获取graphcache对象的键
g.cache_key# graphcache-MZ5SQR
从redis缓存中检索以前存储的graphcache对象(cache_key=graphcache-MZ5SQR
)
# using default redis connection# host = localhost# port = 6379# db = 0g1=GraphCache(graphcache_ref='graphcache-MZ5SQR')
- 项目
标签: