InventoryCache.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 = 3600.0; // 1 hour
  40. private static ExpiringCache<UUID, InventoryFolderBase> m_RootFolders = new ExpiringCache<UUID, InventoryFolderBase>();
  41. private static ExpiringCache<UUID, Dictionary<AssetType, InventoryFolderBase>> m_FolderTypes = new ExpiringCache<UUID, Dictionary<AssetType, InventoryFolderBase>>();
  42. private static ExpiringCache<UUID, InventoryCollection> m_Inventories = new ExpiringCache<UUID, InventoryCollection>();
  43. public void Cache(UUID userID, InventoryFolderBase root)
  44. {
  45. m_RootFolders.AddOrUpdate(userID, root, CACHE_EXPIRATION_SECONDS);
  46. }
  47. public InventoryFolderBase GetRootFolder(UUID userID)
  48. {
  49. InventoryFolderBase root = null;
  50. if (m_RootFolders.TryGetValue(userID, out root))
  51. return root;
  52. return null;
  53. }
  54. public void Cache(UUID userID, AssetType type, InventoryFolderBase folder)
  55. {
  56. Dictionary<AssetType, InventoryFolderBase> ff = null;
  57. if (!m_FolderTypes.TryGetValue(userID, out ff))
  58. {
  59. ff = new Dictionary<AssetType, InventoryFolderBase>();
  60. m_FolderTypes.Add(userID, ff, CACHE_EXPIRATION_SECONDS);
  61. }
  62. // We need to lock here since two threads could potentially retrieve the same dictionary
  63. // and try to add a folder for that type simultaneously. Dictionary<>.Add() is not described as thread-safe in the SDK
  64. // even if the folders are identical.
  65. lock (ff)
  66. {
  67. if (!ff.ContainsKey(type))
  68. ff.Add(type, folder);
  69. }
  70. }
  71. public InventoryFolderBase GetFolderForType(UUID userID, AssetType type)
  72. {
  73. Dictionary<AssetType, InventoryFolderBase> ff = null;
  74. if (m_FolderTypes.TryGetValue(userID, out ff))
  75. {
  76. InventoryFolderBase f = null;
  77. lock (ff)
  78. {
  79. if (ff.TryGetValue(type, out f))
  80. return f;
  81. }
  82. }
  83. return null;
  84. }
  85. public void Cache(UUID userID, InventoryCollection inv)
  86. {
  87. m_Inventories.AddOrUpdate(userID, inv, 120);
  88. }
  89. public InventoryCollection GetUserInventory(UUID userID)
  90. {
  91. InventoryCollection inv = null;
  92. if (m_Inventories.TryGetValue(userID, out inv))
  93. return inv;
  94. return null;
  95. }
  96. public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  97. {
  98. InventoryCollection inv = null;
  99. InventoryCollection c;
  100. if (m_Inventories.TryGetValue(userID, out inv))
  101. {
  102. c = new InventoryCollection();
  103. c.UserID = userID;
  104. c.Folders = inv.Folders.FindAll(delegate(InventoryFolderBase f)
  105. {
  106. return f.ParentID == folderID;
  107. });
  108. c.Items = inv.Items.FindAll(delegate(InventoryItemBase i)
  109. {
  110. return i.Folder == folderID;
  111. });
  112. return c;
  113. }
  114. return null;
  115. }
  116. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  117. {
  118. InventoryCollection inv = null;
  119. if (m_Inventories.TryGetValue(userID, out inv))
  120. {
  121. List<InventoryItemBase> items = inv.Items.FindAll(delegate(InventoryItemBase i)
  122. {
  123. return i.Folder == folderID;
  124. });
  125. return items;
  126. }
  127. return null;
  128. }
  129. }
  130. }