[IMP] tools.cache: added missing clean_caches_for_db replacement.
authorVo Minh Thu <vmt@openerp.com>
Thu, 25 Aug 2011 12:47:11 +0000 (14:47 +0200)
committerVo Minh Thu <vmt@openerp.com>
Thu, 25 Aug 2011 12:47:11 +0000 (14:47 +0200)
bzr revid: vmt@openerp.com-20110825124711-y2uckmvcoa0arz4h

openerp/modules/registry.py
openerp/osv/orm.py
openerp/service/web_services.py
openerp/sql_db.py

index 11f484a..19b6227 100644 (file)
@@ -82,6 +82,15 @@ class Registry(object):
 
         return res
 
+    def clear_caches
+        """ Clear the caches
+
+        This clears the caches associated to methods decorated with
+        ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models.
+        """
+        for model in self.models.itervalues():
+            model.clear_caches()
+
 
 class RegistryManager(object):
     """ Model registries manager.
@@ -155,4 +164,19 @@ class RegistryManager(object):
             del cls.registries[db_name]
 
 
+    @classmethod
+    def clear_caches(db_name)
+        """ Clear the caches
+
+        This clears the caches associated to methods decorated with
+        ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models
+        of the given database name.
+
+        This method is given to spare you a ``RegistryManager.get(db_name)``
+        that would loads the given database if it was not already loaded.
+        """
+        if db_name in cls.registries:
+            del cls.registries[db_name].clear_caches()
+
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
index 9c09935..d2e1cb9 100644 (file)
@@ -2251,6 +2251,18 @@ class orm_template(object):
             values = defaults
         return values
 
+    def clear_caches(self):
+        """ Clear the caches
+
+        This clears the caches associated to methods decorated with
+        ``tools.ormcache`` or ``tools.ormcache_multi``.
+        """
+        try:
+            getattr(self, '_ormcache')
+            self._ormcache = {}
+        except AttributeError:
+            pass
+
 class orm_memory(orm_template):
 
     _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count', 'exists']
index c3b23a4..c768a3c 100644 (file)
@@ -155,6 +155,7 @@ class db(netsvc.ExportService):
 
     def exp_drop(self, db_name):
         sql_db.close_db(db_name)
+        openerp.modules.registry.RegistryManager.clear_caches(db_name)
         openerp.netsvc.Agent.cancel(db_name)
         logger = netsvc.Logger()
 
@@ -258,7 +259,8 @@ class db(netsvc.ExportService):
 
     def exp_rename(self, old_name, new_name):
         sql_db.close_db(old_name)
-        openerp.netsvc.Agent.cancel(db_name)
+        openerp.modules.registry.RegistryManager.clear_caches(old_name)
+        openerp.netsvc.Agent.cancel(old_name)
         logger = netsvc.Logger()
 
         db = sql_db.db_connect('template1')
index 18efb62..f4a899c 100644 (file)
@@ -500,7 +500,6 @@ def db_connect(db_name):
 def close_db(db_name):
     """ You might want to call openerp.netsvc.Agent.cancel(db_name) along this function."""
     _Pool.close_all(dsn(db_name))
-    tools.cache.clean_caches_for_db(db_name)
     ct = currentThread()
     if hasattr(ct, 'dbname'):
         delattr(ct, 'dbname')