Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 problems to avoid?

like image 405
Don Kirkby Avatar asked Jun 29 '26 10:06

Don Kirkby


1 Answers

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.

like image 101
Don Kirkby Avatar answered Jul 02 '26 01:07

Don Kirkby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!