From 5bafd97c6988e86b8ec248a1e233a93b3c878da6 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 17 Aug 2020 15:06:52 +0100 Subject: [PATCH] change script notecards cache --- .../Shared/Api/Implementation/LSL_Api.cs | 77 ++++--------------- 1 file changed, 13 insertions(+), 64 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index 12e46c090a..e3dcffc88a 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs @@ -18588,51 +18588,26 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api public class NotecardCache { - protected class Notecard - { - public string[] text; - public DateTime lastRef; - } - - private static Dictionary m_Notecards = - new Dictionary(); + private static ExpiringCacheOS m_Notecards = new ExpiringCacheOS(30000); public static void Cache(UUID assetID, byte[] text) { - CheckCache(); + if (m_Notecards.ContainsKey(assetID)) + return; - lock (m_Notecards) - { - if (m_Notecards.ContainsKey(assetID)) - return; - - Notecard nc = new Notecard - { - lastRef = DateTime.Now, - text = SLUtil.ParseNotecardToArray(text) - }; - m_Notecards[assetID] = nc; - } + m_Notecards.AddOrUpdate(assetID, SLUtil.ParseNotecardToArray(text), 30); } public static bool IsCached(UUID assetID) { - lock (m_Notecards) - { - return m_Notecards.ContainsKey(assetID); - } + return m_Notecards.ContainsKey(assetID); } public static int GetLines(UUID assetID) { - if (!IsCached(assetID)) - return -1; - - lock (m_Notecards) - { - m_Notecards[assetID].lastRef = DateTime.Now; - return m_Notecards[assetID].text.Length; - } + if (m_Notecards.TryGetValue(assetID, 0, out string[] text)) + return text.Length; + return -1; } /// @@ -18643,25 +18618,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api /// public static string GetLine(UUID assetID, int lineNumber) { - if (lineNumber < 0) - return ""; - - string data; - - if (!IsCached(assetID)) - return ""; - - lock (m_Notecards) + if (lineNumber >= 0 && m_Notecards.TryGetValue(assetID, 0, out string[] text)) { - m_Notecards[assetID].lastRef = DateTime.Now; - - if (lineNumber >= m_Notecards[assetID].text.Length) + if (lineNumber >= text.Length) return "\n\n\n"; - - data = m_Notecards[assetID].text[lineNumber]; - - return data; + return text[lineNumber]; } + return ""; } /// @@ -18681,23 +18644,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api string line = GetLine(assetID, lineNumber); if (line.Length > maxLength) - line = line.Substring(0, maxLength); + return line.Substring(0, maxLength); return line; } - - public static void CheckCache() - { - lock (m_Notecards) - { - foreach (UUID key in new List(m_Notecards.Keys)) - { - Notecard nc = m_Notecards[key]; - if (nc.lastRef.AddSeconds(30) < DateTime.Now) - m_Notecards.Remove(key); - } - } - } - } }