From 58894f5739a331ff884eb71a544879e31484004b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 24 Apr 2008 15:25:56 +0200 Subject: [PATCH] Make Cacheable object shared the same dict for all thread and use dbname as primary key. --- trytond/ir/translation.py | 23 ++++++++++++----------- trytond/osv/osv.py | 21 ++++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index a788d2561d..930773c583 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -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) diff --git a/trytond/osv/osv.py b/trytond/osv/osv.py index 04f6fb0d49..f8fcf97f00 100644 --- a/trytond/osv/osv.py +++ b/trytond/osv/osv.py @@ -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()