From 2a6dd4ccdcd4a89e06d9fb5afcab99ea5380afdb Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 10 Jul 2020 17:49:30 +0200 Subject: [PATCH] add notecardcache --- src/helper/NotecardCache.cs | 123 ++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/helper/NotecardCache.cs diff --git a/src/helper/NotecardCache.cs b/src/helper/NotecardCache.cs new file mode 100644 index 0000000..058617f --- /dev/null +++ b/src/helper/NotecardCache.cs @@ -0,0 +1,123 @@ +using OpenMetaverse; +using OpenSim.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OpenSim.Modules.Appearance2Avatar.helper +{ + class NotecardCache + { + protected class Notecard + { + public string[] text; + public DateTime lastRef; + } + + private static Dictionary m_Notecards = + new Dictionary(); + + public static void Cache(UUID assetID, byte[] text) + { + CheckCache(); + + lock (m_Notecards) + { + if (m_Notecards.ContainsKey(assetID)) + return; + + Notecard nc = new Notecard(); + nc.lastRef = DateTime.Now; + nc.text = SLUtil.ParseNotecardToArray(text); + m_Notecards[assetID] = nc; + } + } + + public static bool IsCached(UUID assetID) + { + lock (m_Notecards) + { + 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; + } + } + + /// + /// Get a notecard line. + /// + /// + /// Lines start at index 0 + /// + public static string GetLine(UUID assetID, int lineNumber) + { + if (lineNumber < 0) + return ""; + + string data; + + if (!IsCached(assetID)) + return ""; + + lock (m_Notecards) + { + m_Notecards[assetID].lastRef = DateTime.Now; + + if (lineNumber >= m_Notecards[assetID].text.Length) + return "\n\n\n"; + + data = m_Notecards[assetID].text[lineNumber]; + + return data; + } + } + + /// + /// Get a notecard line. + /// + /// + /// Lines start at index 0 + /// + /// Maximum length of the returned line. + /// + /// + /// If the line length is longer than , + /// the return string will be truncated. + /// + public static string GetLine(UUID assetID, int lineNumber, int maxLength) + { + string line = GetLine(assetID, lineNumber); + + if (line.Length > maxLength) + line = 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); + } + } + } + + } +}