English 中文(简体)
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?

最佳回答

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.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签