0

我有以下结构的多个类:

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):
    ...

最好的方法是什么?

4

1 回答 1

3

我是慢热型。我应该做类似的事情:

class Thing(Base):
    id = ...

    def get_children(self, childrentype, invalidate=False):
        if invalidate:
            region_invalidate(_get_children, None, self.id, childrentype)

        @cache_region('short_term', 'get_children')
        def _get_children(id, childrentype):
            ...
            return query

        return _get_children(self.id, childrentype)

当然,如果我不必在该方法中定义另一个函数会更好,但这很简单。

于 2011-12-23T02:36:18.937 回答