1
0

UserAccountCache.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using log4net;
  5. using OpenMetaverse;
  6. using OpenSim.Services.Interfaces;
  7. namespace OpenSim.Services.HypergridService
  8. {
  9. public class UserAccountCache : IUserAccountService
  10. {
  11. private const double CACHE_EXPIRATION_SECONDS = 120000.0; // 33 hours!
  12. // private static readonly ILog m_log =
  13. // LogManager.GetLogger(
  14. // MethodBase.GetCurrentMethod().DeclaringType);
  15. private ExpiringCache<UUID, UserAccount> m_UUIDCache;
  16. private IUserAccountService m_UserAccountService;
  17. private static UserAccountCache m_Singleton;
  18. public static UserAccountCache CreateUserAccountCache(IUserAccountService u)
  19. {
  20. if (m_Singleton == null)
  21. m_Singleton = new UserAccountCache(u);
  22. return m_Singleton;
  23. }
  24. private UserAccountCache(IUserAccountService u)
  25. {
  26. m_UUIDCache = new ExpiringCache<UUID, UserAccount>();
  27. m_UserAccountService = u;
  28. }
  29. public void Cache(UUID userID, UserAccount account)
  30. {
  31. // Cache even null accounts
  32. m_UUIDCache.AddOrUpdate(userID, account, CACHE_EXPIRATION_SECONDS);
  33. //m_log.DebugFormat("[USER CACHE]: cached user {0}", userID);
  34. }
  35. public UserAccount Get(UUID userID, out bool inCache)
  36. {
  37. UserAccount account = null;
  38. inCache = false;
  39. if (m_UUIDCache.TryGetValue(userID, out account))
  40. {
  41. //m_log.DebugFormat("[USER CACHE]: Account {0} {1} found in cache", account.FirstName, account.LastName);
  42. inCache = true;
  43. return account;
  44. }
  45. return null;
  46. }
  47. public UserAccount GetUser(string id)
  48. {
  49. UUID uuid = UUID.Zero;
  50. UUID.TryParse(id, out uuid);
  51. bool inCache = false;
  52. UserAccount account = Get(uuid, out inCache);
  53. if (!inCache)
  54. {
  55. account = m_UserAccountService.GetUserAccount(UUID.Zero, uuid);
  56. Cache(uuid, account);
  57. }
  58. return account;
  59. }
  60. #region IUserAccountService
  61. public UserAccount GetUserAccount(UUID scopeID, UUID userID)
  62. {
  63. return GetUser(userID.ToString());
  64. }
  65. public UserAccount GetUserAccount(UUID scopeID, string FirstName, string LastName)
  66. {
  67. return null;
  68. }
  69. public UserAccount GetUserAccount(UUID scopeID, string Email)
  70. {
  71. return null;
  72. }
  73. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  74. {
  75. return null;
  76. }
  77. public bool StoreUserAccount(UserAccount data)
  78. {
  79. return false;
  80. }
  81. #endregion
  82. }
  83. }