Weird issue with the cache - it's fixed now!

This commit is contained in:
RDeck 2026-06-09 13:25:15 -04:00
parent 79340a9197
commit 60da5c74ef
2 changed files with 81 additions and 31 deletions

View File

@ -1,9 +1,9 @@
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
//using System;
//using System.Linq;
//using System.Threading.Tasks;
using log4net;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Cryptography;
@ -15,18 +15,29 @@ public class MainScriptingObject : IDisposable
SortedDictionary<string, string> scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it (so we can cache it)
string scriptSource = string.Empty;
string tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar;
Dictionary<string, Dictionary<string, string>>? dctCache = null;
Dictionary<string, Dictionary<string, string>> dctCache = new(StringComparer.OrdinalIgnoreCase);
Dictionary<string, MdlVbsSources> dctFileCache = new();//(StringComparer.OrdinalIgnoreCase);
Random rn = new();
List<string> lstFilesToRemove = new();
bool removedOldScripts = false;
ILog? appLog = null;
public void Dispose()
{
if (dctCache != null)
dctCache.Clear();
if (dctFileCache != null)
dctFileCache.Clear();
if (AllowRemovalOfOldScripts && lstFilesToRemove.Count > 0)
{
foreach (var f in lstFilesToRemove)
{
try { File.Delete(f); } catch { }
}
lstFilesToRemove.Clear();
}
}
public bool HadErrors { get; set; } = false;
@ -40,7 +51,7 @@ public class MainScriptingObject : IDisposable
set
{
scriptSource = CleanUserCode(value);
dctCache = new();
dctCache.Clear(); //= new(StringComparer.);
}
}
@ -80,7 +91,7 @@ public class MainScriptingObject : IDisposable
if (dctFileCache.ContainsKey(theKey))
{
scriptSource = dctFileCache[theKey].VbsScriptSource;
dctCache = dctFileCache[theKey].CacheDict;
//dctCache = dctFileCache[theKey].CacheDict;
rv = true;
}
else
@ -88,7 +99,7 @@ public class MainScriptingObject : IDisposable
if (!File.Exists(VBScriptPath))
{
scriptSource = string.Empty;
dctCache = null;
dctCache.Clear();
rv = false;
}
else
@ -98,15 +109,26 @@ public class MainScriptingObject : IDisposable
scriptSource = sr.ReadToEnd();
z.VbsScriptSource = scriptSource;
z.CacheDict = new();
//z.CacheDict = new();
dctFileCache[theKey] = z;
dctCache = dctFileCache[theKey].CacheDict;
//dctCache = dctFileCache[theKey].CacheDict;
rv = true;
}
}
return rv;
}
public MainScriptingObject()
{
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var configFileDirectory = Path.Combine(baseDirectory, "log4net.config");
var configFileInfo = new FileInfo(configFileDirectory);
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo);
appLog = log4net.LogManager.GetLogger("log4netFileLogger");
}
public void ClearScriptInputs()
{
scrInp1.Clear();
@ -150,16 +172,24 @@ public class MainScriptingObject : IDisposable
var scriptFile = Path.Combine(tempPath, tStamp + ".vbs");
var iClearValues = string.Join('\t', scrInp2.ToArray());
var iKey = SHA256Hash(iClearValues);
var toReturn = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var retVals = string.Empty;
var varName = string.Empty;
var varVal = string.Empty;
Dictionary<string, string> toReturn = new(); // new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
HadErrors = false;
lstFilesToRemove.Add(outFile);
lstFilesToRemove.Add(scriptFile);
if (UseCache && dctCache != null && dctCache.ContainsKey(iKey))
toReturn = dctCache[iKey];
if (DebugMode)
appLog.DebugFormat("INPUT == {0}", iClearValues);
if (UseCache && dctCache.ContainsKey(iKey))
{
toReturn = dctCache[iKey] as Dictionary<string,string>;
if (DebugMode)
appLog.DebugFormat("OUTPUT FROM CACHE ({0}) == {1}", iKey, string.Join('~', toReturn.ToArray())); //dctCache[iKey].ToArray()));
}
else
{
var genCodeHeader = new StringBuilder();
@ -180,8 +210,8 @@ public class MainScriptingObject : IDisposable
genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *");
genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*");
genCodeHeader.AppendLine("'*************************************************");
genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues);
genCodeHeader.AppendLine("'Crypto Lookup: " + iKey);
//genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues);
//genCodeHeader.AppendLine("'Crypto Lookup: " + iKey);
genCodeHeader.AppendLine("'*************************************************");
genCodeHeader.AppendLine("outFile = \"" + outFile + "\"");
@ -219,7 +249,7 @@ public class MainScriptingObject : IDisposable
varVal = string.Format("\"{0}\"", kvp.Value.ToString().Replace("\"", "\"\""));
}
varVal = Regex.Replace(varVal, "(\r\n|\n\r|\r|\n)", "\"\n\"");
varVal = Regex.Replace(varVal, @"(\r\n|\n\r|\r|\n)", "\"\n\"");
varName = "\"" + kvp.Key.ToUpper().Replace("\"", "\"\"") + "\"";
genCodeHeader.AppendLine(" Case " + varName);
genCodeHeader.AppendLine(" retVal = " + varVal);
@ -275,28 +305,43 @@ public class MainScriptingObject : IDisposable
retVals = sr.ReadToEnd();
}
toReturn.Clear();
foreach (var f0 in Regex.Split(retVals, "(\r\n|\n\r|\r|\n)"))
//toReturn = new(StringComparer.OrdinalIgnoreCase);
foreach (var f0 in Regex.Split(retVals, @"(\r\n|\n\r|\r|\n)"))
{
if (f0.Length >= 3 && f0.IndexOf('\t') > 0)
{
var nvp = f0.Split('\t');
nvp[1] = Regex.Replace(nvp[1], "\x7eNewLine\x7e", "\n");
toReturn[nvp[0]] = nvp[1];
nvp[1] = Regex.Replace(nvp[1], @"\x7eNewLine\x7e", "\n");
//toReturn[nvp[0]] = nvp[1];
toReturn.Add(nvp[0], nvp[1]);
}
}
//if (DebugMode)
// appLog.DebugFormat("TO-RETURN NOW HAS {0} ITEMS.", toReturn.Count);
if (UseCache && dctCache != null)
dctCache[iKey] = toReturn;
if (UseCache)// && dctCache != null)
{
dctCache.Add(iKey, toReturn);// ?? new Dictionary<string, string>());
if (DebugMode)
appLog.DebugFormat("--> VERIFIED FROM CACHE({0}): {1}", iKey, string.Join('~', dctCache[iKey].ToArray()));
//dctCache[iKey] = toReturn;
}
if (DebugMode)
{
appLog.DebugFormat("OUTPUT (NON-CACHED) == {0}", string.Join('~', toReturn.ToArray()));
}
}
catch (Exception ex)
{
if (DebugMode)
appLog.Error(ex);
using (var sw = new StreamWriter(outFile, true))
sw.WriteLine(ex.ToString());
}
}
return toReturn;
return new Dictionary<string, string>(toReturn);
}
#region "Maint routines"
@ -328,7 +373,7 @@ public class MainScriptingObject : IDisposable
string StripUnprintables(string inp)
{
var rv = Regex.Replace(inp, "[^\x20-\x7E]", " ");
var rv = Regex.Replace(inp, @"[^\x20-\x7E]", " ");
rv = Regex.Replace(rv, " +", " ").Trim();
return rv;
}
@ -429,11 +474,11 @@ public class MainScriptingObject : IDisposable
{
public void Dispose()
{
CacheDict.Clear();
//CacheDict.Clear();
}
public string VbsScriptSource { get; set; } = string.Empty;
public Dictionary<string, Dictionary<string, string>> CacheDict { get; set; } = new();
//public Dictionary<string, Dictionary<string, string>> CacheDict { get; set; } = new();
}
#endregion

View File

@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-windows</TargetFramework>
<TargetFramework>net10.0-windows10.0.22000.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.2.0.21</AssemblyVersion>
<FileVersion>1.2.0.21</FileVersion>
<Version>1.0.0.21</Version>
<AssemblyVersion>1.2.0.53</AssemblyVersion>
<FileVersion>1.2.0.53</FileVersion>
<Version>1.0.0.53</Version>
<SupportedOSPlatformVersion>10.0.22000.0</SupportedOSPlatformVersion>
</PropertyGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="if /I &quot;$(ConfigurationName)&quot; == &quot;Release&quot; c:\Misc\AutoVersionIncrement.exe &quot;$(ProjectDir)&quot;" />
</Target>
<ItemGroup>
<PackageReference Include="log4net" Version="3.3.1" />
</ItemGroup>
</Project>