From 645ad683b00d55e307bc7df5b51faa49408fdb09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 13 Oct 2014 08:51:19 +0200 Subject: [PATCH] Cache ACL filtering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filtering the projects by ACL can be expensive on larger setups, so let's cache the result. Signed-off-by: Michal Čihař --- weblate/trans/models/project.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/weblate/trans/models/project.py b/weblate/trans/models/project.py index ca6f5e488194..861bb48fe243 100644 --- a/weblate/trans/models/project.py +++ b/weblate/trans/models/project.py @@ -27,6 +27,7 @@ from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import Group +from django.core.cache import cache import os import os.path from weblate.lang.models import Language @@ -66,10 +67,21 @@ def get_acl_status(self, user): and flag whether there is any filtering active. """ projects = self.all() - project_ids = [ - project.id for project in projects if project.has_acl(user) - ] - if projects.count() == len(project_ids): + + cache_key = 'acl-project-{0}'.format(user.id) + + last_result = cache.get(cache_key) + if last_result is not None: + all_projects, project_ids = last_result + else: + project_ids = [ + project.id for project in projects if project.has_acl(user) + ] + all_projects = (projects.count() == len(project_ids)) + + cache.set(cache_key, (all_projects, project_ids)) + + if all_projects: return projects, False return self.filter(id__in=project_ids), True