Make Cacheable object shared the same dict for all thread and use dbname as

primary key.
This commit is contained in:
Cédric Krier committed 2008-04-24 15:25:56 +02:00
1 parent 48989ef03f
commit 58894f5739
2 files changed
+24 -20

No files matched your search

+12 -11
View File
@@ -43,6 +43,7 @@ class Translation(OSV, Cacheable):
def __init__(self):
super(Translation, self).__init__()
Cacheable.__init__(self)
self._sql_constraints += [
('translation_uniq', 'UNIQUE (name, res_id, lang, type, src)',
'Translation must be unique'),
@@ -104,7 +105,7 @@ class Translation(OSV, Cacheable):
name, ttype, lang)
return translations
for obj_id in ids:
trans = self.get((lang, name, obj_id))
trans = self.get(cursor, (lang, name, obj_id))
if trans is not None:
translations[obj_id] = trans
else:
@@ -119,11 +120,11 @@ class Translation(OSV, Cacheable):
','.join([str(x) for x in to_fetch]) + ')',
(lang, ttype, name))
for res_id, value in cursor.fetchall():
self.add((lang, ttype, name, res_id), value)
self.add(cursor, (lang, ttype, name, res_id), value)
translations[res_id] = value
for res_id in ids:
if res_id not in translations:
self.add((lang, ttype, name, res_id), False)
self.add(cursor, (lang, ttype, name, res_id), False)
translations[res_id] = False
return translations
@@ -178,7 +179,7 @@ class Translation(OSV, Cacheable):
return len(ids)
def _get_source(self, cursor, name, ttype, lang, source=None):
trans = self.get((lang, ttype, name, source))
trans = self.get(cursor, (lang, ttype, name, source))
if trans is not None:
return trans
@@ -202,10 +203,10 @@ class Translation(OSV, Cacheable):
(lang, ttype, str(name)))
res = cursor.fetchone()
if res:
self.add((lang, ttype, name, source), res[0])
self.add(cursor, (lang, ttype, name, source), res[0])
return res[0]
else:
self.add((lang, ttype, name, source), False)
self.add(cursor, (lang, ttype, name, source), False)
return False
def _get_sources(self, cursor, args):
@@ -218,7 +219,7 @@ class Translation(OSV, Cacheable):
clause = ''
value = []
for name, ttype, lang, source in args:
trans = self.get((lang, ttype, name, source))
trans = self.get(cursor, (lang, ttype, name, source))
if trans is not None:
res[(name, ttype, lang, source)] = trans
else:
@@ -243,18 +244,18 @@ class Translation(OSV, Cacheable):
for lang, ttype, name, src, value in cursor.fetchall():
res[(name, ttype, lang, source)] = value
for name, ttype, lang, source in args:
self.add((lang, ttype, name, source),
self.add(cursor, (lang, ttype, name, source),
res[(name, ttype, lang, source)])
return res
def unlink(self, cursor, user, ids, context=None):
self.clear()
self.clear(cursor)
self.fields_view_get()
return super(Translation, self).unlink(cursor, user, ids,
context=context)
def create(self, cursor, user, vals, context=None):
self.clear()
self.clear(cursor)
self.fields_view_get()
if vals.get('type', '') in ('odt', 'view', 'wizard_button',
'selection'):
@@ -284,7 +285,7 @@ class Translation(OSV, Cacheable):
context=context)
def write(self, cursor, user, ids, vals, context=None):
self.clear()
self.clear(cursor)
self.fields_view_get()
return super(Translation, self).write(cursor, user, ids, vals,
context=context)
+12 -9
View File
@@ -193,19 +193,22 @@ osv = OSV
class Cacheable(object):
_cache = UpdateableDict()
def __init__(self):
super(Cacheable, self).__init__()
self._cache = {}
def add(self, key, value):
self._cache[key] = value
def add(self, cursor, key, value):
self._cache.setdefault(cursor.dbname, {})
self._cache[cursor.dbname][key] = value
def invalidate(self, key):
del self._cache[key]
def invalidate(self, cursor, key):
del self._cache[cursor.dbname][key]
def get(self, key):
def get(self, cursor, key):
try:
return self._cache[key]
return self._cache[cursor.dbname][key]
except KeyError:
return None
def clear(self):
self._cache.clear()
def clear(self, cursor):
self._cache[cursor.dbname].clear()