changeset a18d951b647d in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=a18d951b647d
description:
        Remove user login and user preferences cache

        The user login cache could result in an error when using trytond in a
        multiprocess environment due to a delay in the broadcast of the cache
        invalidation.

        The user preferences cache was there only to set the context when 
validating
        the records but it is useless since the context is now set from the
        transaction.

        review401251002
        issue11430
diffstat:

 trytond/model/modelstorage.py |  12 +---------
 trytond/res/__init__.py       |   2 -
 trytond/res/ir.py             |  39 ----------------------------------
 trytond/res/user.py           |  49 ++----------------------------------------
 4 files changed, 4 insertions(+), 98 deletions(-)

diffs (232 lines):

diff -r 633a31cb1026 -r a18d951b647d trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py     Wed Apr 27 18:28:46 2022 +0200
+++ b/trytond/model/modelstorage.py     Fri Apr 29 17:14:27 2022 +0200
@@ -1146,15 +1146,6 @@
         # also convert iterator to list
         records = cls.browse(records)
 
-        ctx_pref = {}
-        if Transaction().user:
-            try:
-                User = pool.get('res.user')
-            except KeyError:
-                pass
-            else:
-                ctx_pref = User.get_preferences(context_only=True)
-
         def is_pyson(test):
             if isinstance(test, PYSON):
                 return True
@@ -1324,8 +1315,7 @@
             field_names = set(field_names)
         function_fields = {name for name, field in cls._fields.items()
             if isinstance(field, fields.Function)}
-        ctx_pref['active_test'] = False
-        with Transaction().set_context(ctx_pref):
+        with Transaction().set_context(active_test=False):
             for field_name, field in cls._fields.items():
                 if (field_name not in field_names
                         and not (field.validation_depends & field_names)
diff -r 633a31cb1026 -r a18d951b647d trytond/res/__init__.py
--- a/trytond/res/__init__.py   Wed Apr 27 18:28:46 2022 +0200
+++ b/trytond/res/__init__.py   Fri Apr 29 17:14:27 2022 +0200
@@ -30,10 +30,8 @@
         ir.ModelButtonRule,
         ir.ModelButtonClick,
         ir.RuleGroupGroup,
-        ir.Lang,
         ir.SequenceType,
         ir.SequenceTypeGroup,
-        ir.ModuleConfigWizardItem,
         ir.Export,
         ir.Export_Group,
         ir.Export_Write_Group,
diff -r 633a31cb1026 -r a18d951b647d trytond/res/ir.py
--- a/trytond/res/ir.py Wed Apr 27 18:28:46 2022 +0200
+++ b/trytond/res/ir.py Fri Apr 29 17:14:27 2022 +0200
@@ -176,16 +176,6 @@
             select=True, required=True)
 
 
-class Lang(metaclass=PoolMeta):
-    __name__ = 'ir.lang'
-
-    @classmethod
-    def write(cls, langs, values, *args):
-        super(Lang, cls).write(langs, values, *args)
-        # Restart the cache for get_preferences
-        Pool().get('res.user')._get_preferences_cache.clear()
-
-
 class SequenceType(metaclass=PoolMeta):
     __name__ = 'ir.sequence.type'
     groups = fields.Many2Many('ir.sequence.type-res.group', 'sequence_type',
@@ -224,35 +214,6 @@
         Rule._domain_get_cache.clear()
 
 
-class ModuleConfigWizardItem(metaclass=PoolMeta):
-    __name__ = 'ir.module.config_wizard.item'
-
-    @classmethod
-    def create(cls, vlist):
-        pool = Pool()
-        User = pool.get('res.user')
-        result = super(ModuleConfigWizardItem, cls).create(vlist)
-        # Restart the cache for get_preferences
-        User._get_preferences_cache.clear()
-        return result
-
-    @classmethod
-    def write(cls, items, values, *args):
-        pool = Pool()
-        User = pool.get('res.user')
-        super(ModuleConfigWizardItem, cls).write(items, values, *args)
-        # Restart the cache for get_preferences
-        User._get_preferences_cache.clear()
-
-    @classmethod
-    def delete(cls, items):
-        pool = Pool()
-        User = pool.get('res.user')
-        super(ModuleConfigWizardItem, cls).delete(items)
-        # Restart the cache for get_preferences
-        User._get_preferences_cache.clear()
-
-
 class Export(metaclass=PoolMeta):
     __name__ = 'ir.export'
 
diff -r 633a31cb1026 -r a18d951b647d trytond/res/user.py
--- a/trytond/res/user.py       Wed Apr 27 18:28:46 2022 +0200
+++ b/trytond/res/user.py       Fri Apr 29 17:14:27 2022 +0200
@@ -155,9 +155,7 @@
     warnings = fields.One2Many('res.user.warning', 'user', 'Warnings')
     sessions = fields.Function(fields.Integer('Sessions'),
             'get_sessions')
-    _get_preferences_cache = Cache('res_user.get_preferences')
     _get_groups_cache = Cache('res_user.get_groups', context=False)
-    _get_login_cache = Cache('res_user._get_login', context=False)
 
     @classmethod
     def __setup__(cls):
@@ -374,10 +372,7 @@
     @classmethod
     def create(cls, vlist):
         vlist = [cls._convert_vals(vals) for vals in vlist]
-        res = super(User, cls).create(vlist)
-        # Restart the cache for _get_login
-        cls._get_login_cache.clear()
-        return res
+        return super(User, cls).create(vlist)
 
     @classmethod
     def write(cls, users, values, *args):
@@ -413,10 +408,6 @@
         pool.get('ir.rule')._domain_get_cache.clear()
         # Restart the cache for get_groups
         cls._get_groups_cache.clear()
-        # Restart the cache for _get_login
-        cls._get_login_cache.clear()
-        # Restart the cache for get_preferences
-        cls._get_preferences_cache.clear()
         # Restart the cache of check
         pool.get('ir.model.access')._get_access_cache.clear()
         # Restart the cache of check
@@ -534,15 +525,9 @@
 
     @classmethod
     def get_preferences(cls, context_only=False):
-        key = (Transaction().user, context_only)
-        preferences = cls._get_preferences_cache.get(key)
-        if preferences is not None:
-            return preferences.copy()
         user = Transaction().user
         user = cls(user)
-        preferences = cls._get_preferences(user, context_only=context_only)
-        cls._get_preferences_cache.set(key, preferences)
-        return preferences.copy()
+        return cls._get_preferences(user, context_only=context_only)
 
     @classmethod
     def set_preferences(cls, values):
@@ -642,9 +627,6 @@
 
     @classmethod
     def _get_login(cls, login):
-        result = cls._get_login_cache.get(login)
-        if result:
-            return result
         cursor = Transaction().connection.cursor()
         table = cls.__table__()
         cursor.execute(*table.select(table.id, table.password_hash,
@@ -654,9 +636,7 @@
                     else_=None),
                 where=(table.login == login)
                 & (table.active == Literal(True))))
-        result = cursor.fetchone() or (None, None, None)
-        cls._get_login_cache.set(login, result)
-        return result
+        return cursor.fetchone() or (None, None, None)
 
     @classmethod
     def get_login(cls, login, parameters):
@@ -1014,8 +994,6 @@
         pool.get('ir.rule')._domain_get_cache.clear()
         # Restart the cache for get_groups
         pool.get('res.user')._get_groups_cache.clear()
-        # Restart the cache for get_preferences
-        pool.get('res.user')._get_preferences_cache.clear()
         # Restart the cache for model access and view
         pool.get('ir.model.access')._get_access_cache.clear()
         pool.get('ir.model.field.access')._get_access_cache.clear()
@@ -1030,8 +1008,6 @@
         pool.get('ir.rule')._domain_get_cache.clear()
         # Restart the cache for get_groups
         pool.get('res.user')._get_groups_cache.clear()
-        # Restart the cache for get_preferences
-        pool.get('res.user')._get_preferences_cache.clear()
         # Restart the cache for model access and view
         pool.get('ir.model.access')._get_access_cache.clear()
         pool.get('ir.model.field.access')._get_access_cache.clear()
@@ -1045,8 +1021,6 @@
         pool.get('ir.rule')._domain_get_cache.clear()
         # Restart the cache for get_groups
         pool.get('res.user')._get_groups_cache.clear()
-        # Restart the cache for get_preferences
-        pool.get('res.user')._get_preferences_cache.clear()
         # Restart the cache for model access and view
         pool.get('ir.model.access')._get_access_cache.clear()
         pool.get('ir.model.field.access')._get_access_cache.clear()
@@ -1179,31 +1153,14 @@
 
     @classmethod
     def create(cls, vlist):
-        pool = Pool()
-        User = pool.get('res.user')
         vlist = [v.copy() for v in vlist]
         for values in vlist:
             # Ensure we get a different key for each record
             # default methods are called only once
             values.setdefault('key', cls.default_key())
         applications = super(UserApplication, cls).create(vlist)
-        User._get_preferences_cache.clear()
         return applications
 
-    @classmethod
-    def write(cls, *args):
-        pool = Pool()
-        User = pool.get('res.user')
-        super(UserApplication, cls).write(*args)
-        User._get_preferences_cache.clear()
-
-    @classmethod
-    def delete(cls, applications):
-        pool = Pool()
-        User = pool.get('res.user')
-        super(UserApplication, cls).delete(applications)
-        User._get_preferences_cache.clear()
-
 
 class EmailResetPassword(Report):
     __name__ = 'res.user.email_reset_password'

Reply via email to