RegionInfoCache.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 OpenSimulator 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;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenSim.Services.Interfaces;
  32. using OpenMetaverse;
  33. using log4net;
  34. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  35. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
  36. {
  37. public class RegionInfoCache
  38. {
  39. private const double CACHE_EXPIRATION_SECONDS = 300.0; // 5 minutes
  40. // private static readonly ILog m_log =
  41. // LogManager.GetLogger(
  42. // MethodBase.GetCurrentMethod().DeclaringType);
  43. internal struct ScopedRegionUUID
  44. {
  45. public UUID m_scopeID;
  46. public UUID m_regionID;
  47. public ScopedRegionUUID(UUID scopeID, UUID regionID)
  48. {
  49. m_scopeID = scopeID;
  50. m_regionID = regionID;
  51. }
  52. }
  53. internal struct ScopedRegionName
  54. {
  55. public UUID m_scopeID;
  56. public string m_name;
  57. public ScopedRegionName(UUID scopeID, string name)
  58. {
  59. m_scopeID = scopeID;
  60. m_name = name;
  61. }
  62. }
  63. internal struct ScopedRegionPosition
  64. {
  65. public UUID m_scopeID;
  66. public ulong m_regionHandle;
  67. public ScopedRegionPosition(UUID scopeID, ulong handle)
  68. {
  69. m_scopeID = scopeID;
  70. m_regionHandle = handle;
  71. }
  72. }
  73. private ExpiringCache<ScopedRegionUUID, GridRegion> m_UUIDCache;
  74. private ExpiringCache<ScopedRegionName, ScopedRegionUUID> m_NameCache;
  75. private ExpiringCache<ScopedRegionPosition, GridRegion> m_PositionCache;
  76. public RegionInfoCache()
  77. {
  78. m_UUIDCache = new ExpiringCache<ScopedRegionUUID, GridRegion>();
  79. m_NameCache = new ExpiringCache<ScopedRegionName, ScopedRegionUUID>();
  80. m_PositionCache = new ExpiringCache<ScopedRegionPosition, GridRegion>();
  81. }
  82. public void Cache(GridRegion rinfo)
  83. {
  84. if (rinfo != null)
  85. this.Cache(rinfo.ScopeID,rinfo.RegionID,rinfo);
  86. }
  87. public void Cache(UUID scopeID, UUID regionID, GridRegion rinfo)
  88. {
  89. // for now, do not cache negative results; this is because
  90. // we need to figure out how to handle regions coming online
  91. // in a timely way
  92. if (rinfo == null)
  93. return;
  94. ScopedRegionUUID id = new ScopedRegionUUID(scopeID,regionID);
  95. // Cache even null accounts
  96. m_UUIDCache.AddOrUpdate(id, rinfo, CACHE_EXPIRATION_SECONDS);
  97. if (rinfo != null)
  98. {
  99. ScopedRegionName name = new ScopedRegionName(scopeID,rinfo.RegionName);
  100. m_NameCache.AddOrUpdate(name, id, CACHE_EXPIRATION_SECONDS);
  101. ScopedRegionPosition pos = new ScopedRegionPosition(scopeID, rinfo.RegionHandle);
  102. m_PositionCache.AddOrUpdate(pos, rinfo, CACHE_EXPIRATION_SECONDS);
  103. }
  104. }
  105. public GridRegion Get(UUID scopeID, UUID regionID, out bool inCache)
  106. {
  107. inCache = false;
  108. GridRegion rinfo = null;
  109. ScopedRegionUUID id = new ScopedRegionUUID(scopeID,regionID);
  110. if (m_UUIDCache.TryGetValue(id, out rinfo))
  111. {
  112. inCache = true;
  113. return rinfo;
  114. }
  115. return null;
  116. }
  117. public GridRegion Get(UUID scopeID, ulong handle, out bool inCache)
  118. {
  119. inCache = false;
  120. GridRegion rinfo = null;
  121. ScopedRegionPosition pos = new ScopedRegionPosition(scopeID, handle);
  122. if (m_PositionCache.TryGetValue(pos, out rinfo))
  123. {
  124. inCache = true;
  125. return rinfo;
  126. }
  127. return null;
  128. }
  129. public GridRegion Get(UUID scopeID, string name, out bool inCache)
  130. {
  131. inCache = false;
  132. ScopedRegionName sname = new ScopedRegionName(scopeID,name);
  133. ScopedRegionUUID id;
  134. if (m_NameCache.TryGetValue(sname, out id))
  135. {
  136. GridRegion rinfo = null;
  137. if (m_UUIDCache.TryGetValue(id, out rinfo))
  138. {
  139. inCache = true;
  140. return rinfo;
  141. }
  142. }
  143. return null;
  144. }
  145. }
  146. }