Question

I have multiple classes of the following structure:

class Thing(Base):
    id = Column(Integer, primary_key=True)

    @cache_region('short_term', 'get_children')
    def get_children(self, childrentype=None):
        return DBSession.query()...

The problem however is that beaker will cache get_children() in the same region regardless of self, making caching pointless. A hack is:

def get_children(self, id, childrentype=None):
    ...

children = thing.get_children(thing.id, 'asdf')

but passing Thing.id every time I call the method is just ugly. I'm looking to use cache.region as a regular function and not as a decorator, but I can't find any documentation on that. Something along the lines of:

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

or even more awesome would be something like:

@cache_region('short_term', 'get_children', self.id)
def get_children(self, childrentype=None):
    ...

What is the best approach?

Was it helpful?

Solution

I'm retarded. I should be doing something like:

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)

of course it would better if I didn't have to define another function inside that method, but this is simple enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top