On 11/28/07, Alec Thomas <[EMAIL PROTECTED]> wrote:
>
> This is the area that we can improve. Try the attached patch, it caches
> the results of get_user_permissions() for up to 5 seconds, which should
> massively reduce the number of hits on the database.

Slightly cleaner patch attached.

> > I don't know perm.py well enough right now to come with a good
> > suggestion for how to fix this. But imho we need to find a way to
> > improve the cache before 0.11 is released.
>
> Try the patch out and let me know if it's much better.

One other slowdown you might be experiencing is caused by logging: each
permission check is logged at DEBUG level, so try decreasing the log
level as well.


-- 
Evolution: Taking care of those too stupid to take care of themselves.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/trac-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Index: trac/perm.py
===================================================================
--- trac/perm.py	(revision 6213)
+++ trac/perm.py	(working copy)
@@ -18,6 +18,7 @@
 
 """Management of permissions."""
 
+from time import time
 from trac.config import ExtensionOption, OrderedExtensionsOption
 from trac.core import *
 from trac.resource import Resource, get_resource_name
@@ -254,11 +255,28 @@
 
     implements(IPermissionPolicy)
 
+    # Number of seconds a cached user permission set is valid for.
+    CACHE_EXPIRY = 5
+
+    def __init__(self):
+        self.permission_cache = {}
+
     # IPermissionPolicy methods
 
     def check_permission(self, action, username, resource, perm):
-        return PermissionSystem(self.env). \
-               get_user_permissions(username).get(action, None)
+        now = time()
+
+        timestamp, permissions = self.permission_cache.get(username, (0, None))
+
+        # Cache hit?
+        if now - timestamp > self.CACHE_EXPIRY:
+            # No, pull permissions from database.
+            permissions = PermissionSystem(self.env). \
+                          get_user_permissions(username)
+            self.permission_cache[username] = (now, permissions)
+
+        return action in permissions
+
 
 
 class PermissionSystem(Component):

Reply via email to