--- a/cubicweb/pyramid/tools.py Tue Feb 28 16:11:17 2017 +0100
+++ b/cubicweb/pyramid/tools.py Tue Feb 28 16:46:16 2017 +0100
@@ -26,11 +26,7 @@
with caution, as the API may change without notice.
"""
-#: A short-term cache for user clones.
-#: used by cached_build_user to speed-up repetitive calls to build_user
-#: The expiration is handled in a dumb and brutal way: the whole cache is
-#: cleared every 5 minutes.
-_user_cache = {}
+from repoze.lru import lru_cache
def clone_user(repo, user):
@@ -59,34 +55,13 @@
entity.cw_rset.req = cnx
+@lru_cache(10)
def cached_build_user(repo, eid):
"""Cached version of
:meth:`cubicweb.server.repository.Repository._build_user`
"""
- if eid in _user_cache:
- user, lang = _user_cache[eid]
- entity = clone_user(repo, user)
- return entity, lang
-
with repo.internal_cnx() as cnx:
user = repo._build_user(cnx, eid)
lang = user.prefered_language()
user.cw_clear_relation_cache()
- _user_cache[eid] = (clone_user(repo, user), lang)
- return user, lang
-
-
-def clear_cache():
- """Clear the user cache"""
- _user_cache.clear()
-
-
-def includeme(config):
- """Start the cache maintenance loop task.
-
- Automatically included by :mod:`cubicweb.pyramid`.
- """
- repo = config.registry['cubicweb.repository']
- interval = int(config.registry.settings.get(
- 'cubicweb.usercache.expiration_time', 60 * 5))
- repo.looping_task(interval, clear_cache)
+ return clone_user(repo, user), lang