pyramid_cubicweb/tools.py
changeset 11551 444cd2bba89d
parent 11550 38ed4c3ac3de
equal deleted inserted replaced
11550:38ed4c3ac3de 11551:444cd2bba89d
     1 """Various tools"""
     1 """Various tools.
       
     2 
       
     3 .. warning::
       
     4 
       
     5     This module should be considered as internal implementation details. Use
       
     6     with caution, as the API may change without notice.
       
     7 """
     2 
     8 
     3 #: A short-term cache for user clones.
     9 #: A short-term cache for user clones.
     4 #: used by cached_build_user to speed-up repetitive calls to build_user
    10 #: used by cached_build_user to speed-up repetitive calls to build_user
     5 #: The expiration is handled in a dumb and brutal way: the whole cache is
    11 #: The expiration is handled in a dumb and brutal way: the whole cache is
     6 #: cleared every 5 minutes.
    12 #: cleared every 5 minutes.
    41     :meth:`cubicweb.server.repository.Repository._build_user`
    47     :meth:`cubicweb.server.repository.Repository._build_user`
    42     """
    48     """
    43     with repo.internal_cnx() as cnx:
    49     with repo.internal_cnx() as cnx:
    44         if eid in _user_cache:
    50         if eid in _user_cache:
    45             entity = clone_user(repo, _user_cache[eid])
    51             entity = clone_user(repo, _user_cache[eid])
       
    52             # XXX the cnx is needed here so that the CWUser instance has an
       
    53             # access to the vreg, which it needs when its 'prefered_language'
       
    54             # property is accessed.
       
    55             # If this property did not need a cnx to access a vreg, we could
       
    56             # avoid the internal_cnx() and save more time.
    46             cnx_attach_entity(cnx, entity)
    57             cnx_attach_entity(cnx, entity)
    47             return entity
    58             return entity
    48 
    59 
    49         user = repo._build_user(cnx, eid)
    60         user = repo._build_user(cnx, eid)
    50         user.cw_clear_relation_cache()
    61         user.cw_clear_relation_cache()
    56     """Clear the user cache"""
    67     """Clear the user cache"""
    57     _user_cache.clear()
    68     _user_cache.clear()
    58 
    69 
    59 
    70 
    60 def includeme(config):
    71 def includeme(config):
       
    72     """Start the cache maintenance loop task.
       
    73 
       
    74     Automatically included by :func:`pyramid_cubicweb.make_cubicweb_application`.
       
    75     """
    61     repo = config.registry['cubicweb.repository']
    76     repo = config.registry['cubicweb.repository']
    62     interval = int(config.registry.settings.get(
    77     interval = int(config.registry.settings.get(
    63         'cubicweb.usercache.expiration_time', 60*5))
    78         'cubicweb.usercache.expiration_time', 60*5))
    64     repo.looping_task(interval, clear_cache)
    79     repo.looping_task(interval, clear_cache)