[pyramid] Drop module-level cache and cleanup looping tasks in tools
And use a LRU cache over cached_build_user function.
This looping task is problematic because it would not be run from within a
WSGI application which does not have a repository with a tasks manager.
This pulls an explicit dependency on 'repoze.lru' but it's not a big deal
since pyramid already depends on this. RPM spec file not update since it does
not even mention pyramid...
--- a/cubicweb/pyramid/__init__.py Tue Feb 28 16:11:17 2017 +0100
+++ b/cubicweb/pyramid/__init__.py Tue Feb 28 16:46:16 2017 +0100
@@ -230,7 +230,6 @@
for name in aslist(config.registry.settings.get('cubicweb.includes', [])):
config.include(name)
- config.include('cubicweb.pyramid.tools')
config.include('cubicweb.pyramid.core')
config.include('cubicweb.pyramid.syncsession')
--- 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
--- a/debian/control Tue Feb 28 16:11:17 2017 +0100
+++ b/debian/control Tue Feb 28 16:46:16 2017 +0100
@@ -26,6 +26,7 @@
python-pyramid-multiauth,
python-waitress,
python-passlib (>= 1.7.0),
+ python-repoze.lru,
python-wsgicors,
sphinx-common,
Standards-Version: 3.9.6
@@ -157,6 +158,7 @@
python-pyramid-multiauth,
python-waitress (>= 0.8.9),
python-wsgicors,
+ python-repoze.lru,
Recommends:
python-pyramid-debugtoolbar
Conflicts:
--- a/setup.py Tue Feb 28 16:11:17 2017 +0100
+++ b/setup.py Tue Feb 28 16:46:16 2017 +0100
@@ -230,6 +230,7 @@
'waitress >= 0.8.9',
'wsgicors >= 0.3',
'pyramid_multiauth',
+ 'repoze.lru',
],
'rdf': [
'rdflib',