Commit abbf7195 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[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...
parent 4d414ecf8416
......@@ -230,7 +230,6 @@ def includeme(config):
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')
......
......@@ -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 @@ def cnx_attach_entity(cnx, entity):
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
......@@ -26,6 +26,7 @@ Build-Depends:
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 @@ Depends:
python-pyramid-multiauth,
python-waitress (>= 0.8.9),
python-wsgicors,
python-repoze.lru,
Recommends:
python-pyramid-debugtoolbar
Conflicts:
......
......@@ -230,6 +230,7 @@ setup(
'waitress >= 0.8.9',
'wsgicors >= 0.3',
'pyramid_multiauth',
'repoze.lru',
],
'rdf': [
'rdflib',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment