我有以下结构的多个类:
class Thing(Base):
id = Column(Integer, primary_key=True)
@cache_region('short_term', 'get_children')
def get_children(self, childrentype=None):
return DBSession.query()...
然而问题是,beaker 将在同一区域缓存 get_children() 而不管自身如何,这使得缓存毫无意义。一个黑客是:
def get_children(self, id, childrentype=None):
...
children = thing.get_children(thing.id, 'asdf')
但是每次我调用该方法时都传递 Thing.id 只是丑陋的。我希望将 cache.region 用作常规函数而不是装饰器,但我找不到任何文档。类似于以下内容:
def get_children(self, childrentype=None):
if "cached in cache_region(Thing.get_children, 'short_term', 'get_children', self.id, childrentype)":
return "the cache"
else:
query = DBSession.query()...
"cache query in cache_region(Thing.get_children', 'short_term', 'get_children', self.id, childrentype)"
return query
甚至更棒的是:
@cache_region('short_term', 'get_children', self.id)
def get_children(self, childrentype=None):
...
最好的方法是什么?