3 class ormcache(object):
4 """ LRU cache decorator for orm methods,
7 def __init__(self, skiparg=2, size=8192, multi=None, timeout=None):
17 def lookup(self2, cr, *args):
18 r = self.lookup(self2, cr, *args)
20 lookup.clear_cache = self.clear
24 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) )
28 ormcache = getattr(self2, '_ormcache')
29 except AttributeError:
30 ormcache = self2._ormcache = {}
32 d = ormcache[self.method]
34 d = ormcache[self.method] = lru.LRU(self.size)
37 def lookup(self, self2, cr, *args):
39 key = args[self.skiparg-2:]
46 value = d[key] = self.method(self2, cr, *args)
50 return self.method(self2, cr, *args)
52 def clear(self, self2, *args):
53 """ Remove *args entry from the cache or all keys if *args is undefined
58 key = args[self.skiparg-2:]
60 self2.pool._any_cache_cleared = True
65 self2.pool._any_cache_cleared = True
67 class ormcache_multi(ormcache):
68 def __init__(self, skiparg=2, size=8192, multi=3):
69 super(ormcache_multi,self).__init__(skiparg,size)
70 self.multi = multi - 2
72 def lookup(self, self2, cr, *args):
82 key = tuple(args[self.skiparg-2:])
92 r.update(self.method(self2, cr, *args))
96 key = tuple(args[self.skiparg-2:])
101 class dummy_cache(object):
102 """ Cache decorator replacement to actually do no caching.
104 def __init__(self, *l, **kw):
106 def __call__(self, fn):
107 fn.clear_cache = self.clear
109 def clear(self, *l, **kw):
112 if __name__ == '__main__':
117 print "A::m(", self,a,b
120 @ormcache_multi(multi=3)
121 def n(self,cr,uid,ids):
122 print "m", self,cr,uid,ids
123 return dict([(i,i) for i in ids])
128 r=a.n("cr",1,[1,2,3,4])
131 for i in a._ormcache:
132 print a._ormcache[i].d
133 a.n.clear_cache(a,1,1)
138 # For backward compatibility
141 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: