add some caching for userAssignement in LdapRbacManager

git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1437326 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2013-01-23 10:31:19 +00:00
parent 0e3020b8b0
commit f11ba97b41
2 changed files with 27 additions and 11 deletions

View File

@ -106,8 +106,11 @@ public class LdapRbacManager
@Named( value = "cache#ldapRoles" ) @Named( value = "cache#ldapRoles" )
private Cache<String, Role> rolesCache; private Cache<String, Role> rolesCache;
private boolean writableLdap = false; @Inject
@Named( value = "cache#userAssignments" )
private Cache<String, UserAssignment> userAssignmentsCache;
private boolean writableLdap = false;
@PostConstruct @PostConstruct
public void initialize() public void initialize()
@ -191,6 +194,7 @@ public class LdapRbacManager
} }
} }
this.rolesCache.clear(); this.rolesCache.clear();
this.userAssignmentsCache.clear();
this.rbacImpl.eraseDatabase(); this.rbacImpl.eraseDatabase();
} }
@ -286,6 +290,7 @@ public class LdapRbacManager
{ {
UserAssignment userAssignment = new UserAssignmentImpl( entry.getKey(), entry.getValue() ); UserAssignment userAssignment = new UserAssignmentImpl( entry.getKey(), entry.getValue() );
userAssignments.add( userAssignment ); userAssignments.add( userAssignment );
userAssignmentsCache.put( userAssignment.getPrincipal(), userAssignment );
} }
return userAssignments; return userAssignments;
@ -575,6 +580,11 @@ public class LdapRbacManager
public UserAssignment getUserAssignment( String username ) public UserAssignment getUserAssignment( String username )
throws RbacManagerException throws RbacManagerException
{ {
UserAssignment ua = userAssignmentsCache.get( username );
if ( ua != null )
{
return ua;
}
LdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
@ -583,7 +593,11 @@ public class LdapRbacManager
context = ldapConnection.getDirContext(); context = ldapConnection.getDirContext();
List<String> roles = ldapRoleMapper.getRoles( username, context, getRealRoles() ); List<String> roles = ldapRoleMapper.getRoles( username, context, getRealRoles() );
return new UserAssignmentImpl( username, roles ); ua = new UserAssignmentImpl( username, roles );
userAssignmentsCache.put( username, ua );
return ua;
} }
catch ( MappingException e ) catch ( MappingException e )
{ {
@ -778,12 +792,17 @@ public class LdapRbacManager
throws RbacManagerException throws RbacManagerException
{ {
// TODO ldap cannot or isWritable ldap ? // TODO ldap cannot or isWritable ldap ?
userAssignmentsCache.remove( username );
this.rbacImpl.removeUserAssignment( username ); this.rbacImpl.removeUserAssignment( username );
} }
public void removeUserAssignment( UserAssignment userAssignment ) public void removeUserAssignment( UserAssignment userAssignment )
throws RbacManagerException throws RbacManagerException
{ {
if ( userAssignment != null )
{
userAssignmentsCache.remove( userAssignment.getPrincipal() );
}
// TODO ldap cannot or isWritable ldap ? // TODO ldap cannot or isWritable ldap ?
this.rbacImpl.removeUserAssignment( userAssignment ); this.rbacImpl.removeUserAssignment( userAssignment );
} }
@ -977,6 +996,7 @@ public class LdapRbacManager
} }
} }
userAssignmentsCache.put( userAssignment.getPrincipal(), userAssignment );
return userAssignment; return userAssignment;
} }
catch ( UserManagerException e ) catch ( UserManagerException e )
@ -1000,6 +1020,10 @@ public class LdapRbacManager
public boolean userAssignmentExists( String principal ) public boolean userAssignmentExists( String principal )
{ {
if ( userAssignmentsCache.hasKey( principal ) )
{
return true;
}
LdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try

View File

@ -108,15 +108,7 @@
<property name="userPermissionsCache" ref="cache#userPermissions"/> <property name="userPermissionsCache" ref="cache#userPermissions"/>
</bean> </bean>
<bean name="cache#userAssignments" class="org.apache.archiva.redback.components.cache.ehcache.EhcacheCache" <bean name="cache#userAssignments" class="org.apache.archiva.redback.components.cache.impl.NoCacheCache">
init-method="initialize">
<property name="diskPersistent" value="false"/>
<property name="eternal" value="false"/>
<property name="maxElementsInMemory" value="1000"/>
<property name="memoryEvictionPolicy" value="LRU"/>
<property name="name" value="userAssignments"/>
<property name="timeToIdleSeconds" value="300"/>
<property name="timeToLiveSeconds" value="600"/>
</bean> </bean>
<bean name="cache#userPermissions" class="org.apache.archiva.redback.components.cache.ehcache.EhcacheCache" <bean name="cache#userPermissions" class="org.apache.archiva.redback.components.cache.ehcache.EhcacheCache"