diff -rupN Trac-0.11.2.1-original/trac/perm.py Trac-0.11.2.1/trac/perm.py
--- Trac-0.11.2.1-original/trac/perm.py 2008-11-17 21:01:17.000000000 +0000
+++ Trac-0.11.2.1/trac/perm.py  2009-10-07 13:27:14.000000000 +0000
@@ -153,6 +153,15 @@ class DefaultPermissionStore(Component):
     permissions and groups.
     """
     implements(IPermissionStore)
+
+    # Number of seconds that the cached permission table is valid for.
+    CACHE_EXPIRY = 5
+    # How frequently to clear the permission table cache
+    CACHE_REAP_TIME = 60
+
+    def __init__(self):
+        self.permission_table_cache = {}
+        self.last_reap = time()

     group_providers = ExtensionPoint(IPermissionGroupProvider)

@@ -165,15 +174,29 @@ class DefaultPermissionStore(Component):
         the action column: such a record represents a group and not an actual
         permission, and declares that the user is part of that group.
         """
-        subjects = set([username])
+       subjects = set([username])
         for provider in self.group_providers:  
             subjects.update(provider.get_permission_groups(username))
-
         actions = set([])
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("SELECT username,action FROM permission")
-        rows = cursor.fetchall()
+
+       # Begin caching work
+       now = time()
+
+        if now - self.last_reap > self.CACHE_REAP_TIME:
+            self.permission_table_cache = {}   
+            self.last_reap = time()
+
+        timestamp, rows = self.permission_table_cache.get('query', (0, None))
+
+       # Cache Hit?
+       if now - timestamp > self.CACHE_EXPIRY: 
+           # no, proceed to query the database 
+            db = self.env.get_db_cnx()
+            cursor = db.cursor()
+            cursor.execute("SELECT username,action FROM permission")
+            rows = cursor.fetchall()
+            self.permission_table_cache['query'] = (now, rows)
+
         while True:
             num_users = len(subjects)
             num_actions = len(actions)
@@ -267,7 +290,7 @@ class DefaultPermissionPolicy(Component)
     # IPermissionPolicy methods

     def check_permission(self, action, username, resource, perm):
-        now = time()
+	now = time()
 
         if now - self.last_reap > self.CACHE_REAP_TIME:
             self.permission_cache = {}
@@ -292,6 +315,16 @@ class PermissionSystem(Component):
     implements(IPermissionRequestor)
 
     requestors = ExtensionPoint(IPermissionRequestor)
+    
+    # Number of seconds that cached userlist with a specific
+    # permission is valid for.
+    CACHE_EXPIRY = 5
+    # How frequently to clear the userlist w/permission cache
+    CACHE_REAP_TIME = 60   
+
+    def __init__(self):
+        self.users_with_perm_cache = {}
+	self.last_reap = time()
 
     store = ExtensionOption('trac', 'permission_store', IPermissionStore,
                             'DefaultPermissionStore',
@@ -383,8 +416,8 @@ class PermissionSystem(Component):
             for action in requestor.get_permission_actions():
                 for child in action[1]:
                     parent_map.setdefault(child, []).append(action[0])
-
-        satisfying_perms = {}
+	
+	satisfying_perms = {}
         def _append_with_parents(action):
             if action in satisfying_perms:
                 return # avoid unneccesary work and infinite loops
@@ -392,8 +425,23 @@ class PermissionSystem(Component):
             if action in parent_map:
                 map(_append_with_parents, parent_map[action])
         _append_with_parents(permission)
+	
+	# Begin caching work
+        now = time()
+
+        if now - self.last_reap > self.CACHE_REAP_TIME:
+            self.users_with_perm_cache = {}
+	    self.last_reap = time()
+	
+	timestamp, userlist = self.users_with_perm_cache.get(permission, (0, None))
+	
+	# Cache hit?
+	if now - timestamp > self.CACHE_EXPIRY:
+	    # No, Pull userlist from database.
+	    userlist = self.store.get_users_with_permissions(satisfying_perms.keys())
+	    self.users_with_perm_cache[permission] = (now, userlist)
 
-        return self.store.get_users_with_permissions(satisfying_perms.keys())
+        return userlist or None
 
     def expand_actions(self, actions):
         """Helper method for expanding all meta actions."""

