开发者

OpenERP cache features

I want to cache some results in my OpenERP module, so I dug around a bit and found the cache decorator. Unfortunately, the most documentation I've been able to find is in the class declaration:

Use it as a decorator of the function you plan to cache Timeout: 0 = no timeout, otherwise in seconds

Can anybody recommend a good example of how to use this? Are there known probl开发者_StackOverflow社区ems to avoid?


After digging around some more, the simplest example I've found is the ir_model_data._get_id() method:

@tools.cache()
def _get_id(self, cr, uid, module, xml_id):
    ids = self.search(cr, uid, [('module','=',module),('name','=', xml_id)])
    if not ids:
        raise ValueError('No references to %s.%s' % (module, xml_id))
    # the sql constraints ensure us we have only one result
    return ids[0]

It seems like you just choose a model method you want to cache and then add the cache as a decorator. If some events should clear the cache like this update() method, you use the cached method as a cache object:

            if not result3:
                self._get_id.clear_cache(cr.dbname, uid, module, xml_id)

It looks like by default, the first two parameters of the method are ignored when caching (cursor and user id in most cases).

This is all just based on skimming the code. I'd love to hear some feedback from anyone who's actually used it.


The cache is currently more usable since it is LRU and not an infinite cache anymore.

http://bazaar.launchpad.net/~openerp/openobject-server/5.0/revision/2151

It looks like by default, the first two parameters of the method are ignored when caching (cursor and user id in most cases).

  1. this can be modified by passing skiparg parameter
  2. the arguments being skipped are the implicitly passed self and cursor. The userid is used in caching when the skiparg is 2.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜