4 logger = logging.getLogger(__name__)
6 class ormcache(object):
7 """ LRU cache decorator for orm methods,
10 def __init__(self, skiparg=2, size=8192, multi=None, timeout=None):
11 self.skiparg = skiparg
20 def lookup(self2, cr, *args, **argv):
21 r = self.lookup(self2, cr, *args, **argv)
23 lookup.clear_cache = self.clear
27 return "lookup-stats hit=%s miss=%s err=%s ratio=%.1f" % (self.stat_hit,self.stat_miss,self.stat_err, (100*float(self.stat_hit))/(self.stat_miss+self.stat_hit) )
31 ormcache = getattr(self2, '_ormcache')
32 except AttributeError:
33 ormcache = self2._ormcache = {}
35 d = ormcache[self.method]
37 d = ormcache[self.method] = lru.LRU(self.size)
40 def lookup(self, self2, cr, *args, **argv):
42 key = args[self.skiparg-2:]
49 value = d[key] = self.method(self2, cr, *args)
53 return self.method(self2, cr, *args)
55 def clear(self, self2, *args):
56 """ Remove *args entry from the cache or all keys if *args is undefined
60 logger.warn("ormcache.clear arguments are deprecated and ignored "
61 "(while clearing caches on (%s).%s)",
62 self2._name, self.method.__name__)
64 self2.pool._any_cache_cleared = True
66 class ormcache_context(ormcache):
67 def __init__(self, skiparg=2, size=8192, accepted_keys=()):
68 super(ormcache_context,self).__init__(skiparg,size)
69 self.accepted_keys = accepted_keys
71 def lookup(self, self2, cr, *args, **argv):
74 context = argv.get('context', {})
75 ckey = filter(lambda x: x[0] in self.accepted_keys, context.items())
79 key = args[self.skiparg-2:]+tuple(ckey)
86 value = d[key] = self.method(self2, cr, *args, **argv)
90 return self.method(self2, cr, *args, **argv)
93 class ormcache_multi(ormcache):
94 def __init__(self, skiparg=2, size=8192, multi=3):
95 super(ormcache_multi,self).__init__(skiparg,size)
96 self.multi = multi - 2
98 def lookup(self, self2, cr, *args, **argv):
108 key = tuple(args[self.skiparg-2:])
118 r.update(self.method(self2, cr, *args))
122 key = tuple(args[self.skiparg-2:])
127 class dummy_cache(object):
128 """ Cache decorator replacement to actually do no caching.
130 def __init__(self, *l, **kw):
132 def __call__(self, fn):
133 fn.clear_cache = self.clear
135 def clear(self, *l, **kw):
138 if __name__ == '__main__':
143 print "A::m(", self,a,b
146 @ormcache_multi(multi=3)
147 def n(self,cr,uid,ids):
148 print "m", self,cr,uid,ids
149 return dict([(i,i) for i in ids])
154 r=a.n("cr",1,[1,2,3,4])
157 for i in a._ormcache:
158 print a._ormcache[i].d
160 a.n.clear_cache(a,1,1)
165 # For backward compatibility
168 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: