From cab7a2a45ccc37acb4ea95f38dfc780580d0b161 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Thu, 16 Apr 2009 20:12:46 +0000 Subject: * Add name keyed cache to UserProfileCacheService --- .../Cache/UserProfileCacheService.cs | 147 +++++++++++++++------ 1 file changed, 105 insertions(+), 42 deletions(-) (limited to 'OpenSim/Framework/Communications') diff --git a/OpenSim/Framework/Communications/Cache/UserProfileCacheService.cs b/OpenSim/Framework/Communications/Cache/UserProfileCacheService.cs index b1ce3e7..4aed338 100644 --- a/OpenSim/Framework/Communications/Cache/UserProfileCacheService.cs +++ b/OpenSim/Framework/Communications/Cache/UserProfileCacheService.cs @@ -39,16 +39,28 @@ namespace OpenSim.Framework.Communications.Cache { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + /// + /// Standard format for names. + /// + public const string NAME_FORMAT = "{0} {1}"; + /// /// The comms manager holds references to services (user, grid, inventory, etc.) /// private readonly CommunicationsManager m_commsManager; /// - /// Each user has a cached profile. + /// User profiles indexed by UUID /// - private readonly Dictionary m_userProfiles = new Dictionary(); - + private readonly Dictionary m_userProfilesById + = new Dictionary(); + + /// + /// User profiles indexed by name + /// + private readonly Dictionary m_userProfilesByName + = new Dictionary(); + /// /// The root library folder. /// @@ -89,25 +101,49 @@ namespace OpenSim.Framework.Communications.Cache /// true if the user was successfully removed, false otherwise public bool RemoveUser(UUID userId) { - lock (m_userProfiles) + if (!RemoveFromCaches(userId)) { - if (m_userProfiles.ContainsKey(userId)) - { - m_userProfiles.Remove(userId); - return true; - } + m_log.WarnFormat( + "[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userId); + + return false; } - m_log.WarnFormat( - "[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userId); - - return false; + return true; } /// - /// Get cached details of the given user. If the user isn't in cache then the user is requested from the - /// profile service. + /// Get details of the given user. /// + /// If the user isn't in cache then the user is requested from the profile service. + /// + /// null if no user details are found + public CachedUserInfo GetUserDetails(string fname, string lname) + { + lock (m_userProfilesByName) + { + CachedUserInfo userInfo; + + if (m_userProfilesByName.TryGetValue(string.Format(NAME_FORMAT, fname, lname), out userInfo)) + { + return userInfo; + } + else + { + UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(fname, lname); + + if (userProfile != null) + return AddToCaches(userProfile); + else + return null; + } + } + } + + /// + /// Get details of the given user. + /// + /// If the user isn't in cache then the user is requested from the profile service. /// /// null if no user details are found public CachedUserInfo GetUserDetails(UUID userID) @@ -115,51 +151,78 @@ namespace OpenSim.Framework.Communications.Cache if (userID == UUID.Zero) return null; - lock (m_userProfiles) + lock (m_userProfilesById) { - if (m_userProfiles.ContainsKey(userID)) + if (m_userProfilesById.ContainsKey(userID)) { - return m_userProfiles[userID]; + return m_userProfilesById[userID]; } else { - UserProfileData userprofile = m_commsManager.UserService.GetUserProfile(userID); - if (userprofile != null) - { - CachedUserInfo userinfo = new CachedUserInfo(m_commsManager, userprofile); - m_userProfiles.Add(userID, userinfo); - return userinfo; - } + UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(userID); + if (userProfile != null) + return AddToCaches(userProfile); else - { return null; - } } } } - + /// - /// Preloads User data into the region cache. Modules may use this service to add non-standard clients + /// Populate caches with the given user profile /// - /// - /// - public void PreloadUserCache(UUID userID, UserProfileData userData) + /// + protected CachedUserInfo AddToCaches(UserProfileData userProfile) { - if (userID == UUID.Zero) - return; - - lock (m_userProfiles) + CachedUserInfo createdUserInfo = new CachedUserInfo(m_commsManager, userProfile); + + lock (m_userProfilesById) { - if (m_userProfiles.ContainsKey(userID)) + m_userProfilesById[createdUserInfo.UserProfile.ID] = createdUserInfo; + + lock (m_userProfilesByName) { - return; + m_userProfilesByName[createdUserInfo.UserProfile.Name] = createdUserInfo; } - else + } + + return createdUserInfo; + } + + /// + /// Remove profile belong to the given uuid from the caches + /// + /// + /// true if there was a profile to remove, false otherwise + protected bool RemoveFromCaches(UUID userId) + { + lock (m_userProfilesById) + { + if (m_userProfilesById.ContainsKey(userId)) { - CachedUserInfo userInfo = new CachedUserInfo(m_commsManager, userData); - m_userProfiles.Add(userID, userInfo); + CachedUserInfo userInfo = m_userProfilesById[userId]; + m_userProfilesById.Remove(userId); + + lock (m_userProfilesByName) + { + m_userProfilesByName.Remove(userInfo.UserProfile.Name); + } + + return true; } - } + } + + return false; + } + + /// + /// Preloads User data into the region cache. Modules may use this service to add non-standard clients + /// + /// + /// + public void PreloadUserCache(UUID userID, UserProfileData userData) + { + AddToCaches(userData); } } } -- cgit v1.1