UserProfileCacheService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. namespace OpenSim.Framework.Communications.Cache
  32. {
  33. /// <summary>
  34. /// Holds user profile information and retrieves it from backend services.
  35. /// </summary>
  36. public class UserProfileCacheService
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. /// <summary>
  40. /// The comms manager holds references to services (user, grid, inventory, etc.)
  41. /// </summary>
  42. private readonly CommunicationsManager m_commsManager;
  43. /// <summary>
  44. /// Each user has a cached profile.
  45. /// </summary>
  46. private readonly Dictionary<UUID, CachedUserInfo> m_userProfiles = new Dictionary<UUID, CachedUserInfo>();
  47. /// <summary>
  48. /// The root library folder.
  49. /// </summary>
  50. public readonly InventoryFolderImpl LibraryRoot;
  51. /// <summary>
  52. /// Constructor
  53. /// </summary>
  54. /// <param name="commsManager"></param>
  55. /// <param name="libraryRootFolder"></param>
  56. public UserProfileCacheService(CommunicationsManager commsManager, LibraryRootFolder libraryRootFolder)
  57. {
  58. m_commsManager = commsManager;
  59. LibraryRoot = libraryRootFolder;
  60. }
  61. /// <summary>
  62. /// A new user has moved into a region in this instance so retrieve their profile from the user service.
  63. /// </summary>
  64. ///
  65. /// It isn't strictly necessary to make this call since user data can be lazily requested later on. However,
  66. /// it might be helpful in order to avoid an initial response delay later on
  67. ///
  68. /// <param name="userID"></param>
  69. public void AddNewUser(UUID userID)
  70. {
  71. if (userID == UUID.Zero)
  72. return;
  73. //m_log.DebugFormat("[USER CACHE]: Adding user profile for {0}", userID);
  74. GetUserDetails(userID);
  75. }
  76. /// <summary>
  77. /// Remove this user's profile cache.
  78. /// </summary>
  79. /// <param name="userID"></param>
  80. /// <returns>true if the user was successfully removed, false otherwise</returns>
  81. public bool RemoveUser(UUID userId)
  82. {
  83. lock (m_userProfiles)
  84. {
  85. if (m_userProfiles.ContainsKey(userId))
  86. {
  87. m_userProfiles.Remove(userId);
  88. return true;
  89. }
  90. }
  91. m_log.WarnFormat(
  92. "[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userId);
  93. return false;
  94. }
  95. /// <summary>
  96. /// Get cached details of the given user. If the user isn't in cache then the user is requested from the
  97. /// profile service.
  98. /// </summary>
  99. /// <param name="userID"></param>
  100. /// <returns>null if no user details are found</returns>
  101. public CachedUserInfo GetUserDetails(UUID userID)
  102. {
  103. if (userID == UUID.Zero)
  104. return null;
  105. lock (m_userProfiles)
  106. {
  107. if (m_userProfiles.ContainsKey(userID))
  108. {
  109. return m_userProfiles[userID];
  110. }
  111. else
  112. {
  113. UserProfileData userprofile = m_commsManager.UserService.GetUserProfile(userID);
  114. if (userprofile != null)
  115. {
  116. CachedUserInfo userinfo = new CachedUserInfo(m_commsManager, userprofile);
  117. m_userProfiles.Add(userID, userinfo);
  118. return userinfo;
  119. }
  120. else
  121. {
  122. return null;
  123. }
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// Preloads User data into the region cache. Modules may use this service to add non-standard clients
  129. /// </summary>
  130. /// <param name="userID"></param>
  131. /// <param name="userData"></param>
  132. public void PreloadUserCache(UUID userID, UserProfileData userData)
  133. {
  134. if (userID == UUID.Zero)
  135. return;
  136. lock (m_userProfiles)
  137. {
  138. if (m_userProfiles.ContainsKey(userID))
  139. {
  140. return;
  141. }
  142. else
  143. {
  144. CachedUserInfo userInfo = new CachedUserInfo(m_commsManager, userData);
  145. m_userProfiles.Add(userID, userInfo);
  146. }
  147. }
  148. }
  149. }
  150. }