add notecardcache

master
Christopher 2020-07-10 17:49:30 +02:00
parent c5cc87e10d
commit 2a6dd4ccdc
1 changed files with 123 additions and 0 deletions

123
src/helper/NotecardCache.cs Normal file
View File

@ -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<UUID, Notecard> m_Notecards =
new Dictionary<UUID, Notecard>();
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;
}
}
/// <summary>
/// Get a notecard line.
/// </summary>
/// <param name="assetID"></param>
/// <param name="lineNumber">Lines start at index 0</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// Get a notecard line.
/// </summary>
/// <param name="assetID"></param>
/// <param name="lineNumber">Lines start at index 0</param>
/// <param name="maxLength">
/// Maximum length of the returned line.
/// </param>
/// <returns>
/// If the line length is longer than <paramref name="maxLength"/>,
/// the return string will be truncated.
/// </returns>
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<UUID>(m_Notecards.Keys))
{
Notecard nc = m_Notecards[key];
if (nc.lastRef.AddSeconds(30) < DateTime.Now)
m_Notecards.Remove(key);
}
}
}
}
}