InventoryCache.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Collections.Generic;
  29. using System.Threading;
  30. using OpenSim.Framework;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
  33. {
  34. /// <summary>
  35. /// Cache root and system inventory folders to reduce number of potentially remote inventory calls and associated holdups.
  36. /// </summary>
  37. public class InventoryCache
  38. {
  39. private const double CACHE_EXPIRATION_SECONDS = 60.0; // 1 minute
  40. private static ExpiringCache<UUID, InventoryFolderBase> m_RootFolders = new ExpiringCache<UUID, InventoryFolderBase>();
  41. private static ExpiringCache<UUID, Dictionary<FolderType, InventoryFolderBase>> m_FolderTypes = new ExpiringCache<UUID, Dictionary<FolderType, InventoryFolderBase>>();
  42. private static ExpiringCache<UUID, InventoryCollection> m_Inventories = new ExpiringCache<UUID, InventoryCollection>();
  43. public void RemoveAll(UUID userID)
  44. {
  45. if(m_RootFolders.Contains(userID))
  46. m_RootFolders.Remove(userID);
  47. if(m_FolderTypes.Contains(userID))
  48. m_FolderTypes.Remove(userID);
  49. if(m_Inventories.Contains(userID))
  50. m_Inventories.Remove(userID);
  51. }
  52. public void Cache(UUID userID, InventoryFolderBase root)
  53. {
  54. m_RootFolders.AddOrUpdate(userID, root, CACHE_EXPIRATION_SECONDS);
  55. }
  56. public InventoryFolderBase GetRootFolder(UUID userID)
  57. {
  58. InventoryFolderBase root = null;
  59. if (m_RootFolders.TryGetValue(userID, out root))
  60. return root;
  61. return null;
  62. }
  63. public void Cache(UUID userID, FolderType type, InventoryFolderBase folder)
  64. {
  65. Dictionary<FolderType, InventoryFolderBase> ff = null;
  66. if (!m_FolderTypes.TryGetValue(userID, out ff))
  67. {
  68. ff = new Dictionary<FolderType, InventoryFolderBase>();
  69. m_FolderTypes.Add(userID, ff, CACHE_EXPIRATION_SECONDS);
  70. }
  71. // We need to lock here since two threads could potentially retrieve the same dictionary
  72. // and try to add a folder for that type simultaneously. Dictionary<>.Add() is not described as thread-safe in the SDK
  73. // even if the folders are identical.
  74. lock (ff)
  75. {
  76. if (!ff.ContainsKey(type))
  77. ff.Add(type, folder);
  78. }
  79. }
  80. public InventoryFolderBase GetFolderForType(UUID userID, FolderType type)
  81. {
  82. Dictionary<FolderType, InventoryFolderBase> ff = null;
  83. if (m_FolderTypes.TryGetValue(userID, out ff))
  84. {
  85. InventoryFolderBase f = null;
  86. lock (ff)
  87. {
  88. if (ff.TryGetValue(type, out f))
  89. return f;
  90. }
  91. }
  92. return null;
  93. }
  94. public void Cache(UUID userID, InventoryCollection inv)
  95. {
  96. m_Inventories.AddOrUpdate(userID, inv, 120);
  97. }
  98. public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  99. {
  100. InventoryCollection inv = null;
  101. InventoryCollection c;
  102. if (m_Inventories.TryGetValue(userID, out inv))
  103. {
  104. c = new InventoryCollection();
  105. c.OwnerID = userID;
  106. c.Folders = inv.Folders.FindAll(delegate(InventoryFolderBase f)
  107. {
  108. return f.ParentID == folderID;
  109. });
  110. c.Items = inv.Items.FindAll(delegate(InventoryItemBase i)
  111. {
  112. return i.Folder == folderID;
  113. });
  114. return c;
  115. }
  116. return null;
  117. }
  118. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  119. {
  120. InventoryCollection inv = null;
  121. if (m_Inventories.TryGetValue(userID, out inv))
  122. {
  123. List<InventoryItemBase> items = inv.Items.FindAll(delegate(InventoryItemBase i)
  124. {
  125. return i.Folder == folderID;
  126. });
  127. return items;
  128. }
  129. return null;
  130. }
  131. }
  132. }