diff --git a/src/UniversalDashboard/Cmdlets/ClearCacheCommand.cs b/src/UniversalDashboard/Cmdlets/ClearCacheCommand.cs new file mode 100644 index 00000000..ddaf11c5 --- /dev/null +++ b/src/UniversalDashboard/Cmdlets/ClearCacheCommand.cs @@ -0,0 +1,14 @@ +using System.Management.Automation; +using UniversalDashboard.Execution; + +namespace UniversalDashboard.Cmdlets +{ + [Cmdlet(VerbsCommon.Clear, "UDCache")] + public class ClearCacheCommand : PSCmdlet + { + protected override void EndProcessing() + { + GlobalCachedVariableProvider.Cache.Clear(); + } + } +} \ No newline at end of file diff --git a/src/UniversalDashboard/Execution/EndpointProvider.cs b/src/UniversalDashboard/Execution/EndpointProvider.cs index 962de927..75647ffd 100644 --- a/src/UniversalDashboard/Execution/EndpointProvider.cs +++ b/src/UniversalDashboard/Execution/EndpointProvider.cs @@ -60,7 +60,7 @@ public void StartSession(string sessionId, string connectionId) } else { - Sessions.Add(sessionId, new SessionState { + Sessions.Add(sessionId, new SessionState(sessionId) { ConnectionIds = new List { connectionId } diff --git a/src/UniversalDashboard/Execution/GlobalCachedVariableProvider.cs b/src/UniversalDashboard/Execution/GlobalCachedVariableProvider.cs index 1d770c8f..90a76b26 100644 --- a/src/UniversalDashboard/Execution/GlobalCachedVariableProvider.cs +++ b/src/UniversalDashboard/Execution/GlobalCachedVariableProvider.cs @@ -3,8 +3,8 @@ using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Provider; -using Microsoft.Extensions.Caching.Memory; using System.IO; +using System.Collections.Generic; namespace UniversalDashboard.Execution { @@ -16,18 +16,14 @@ namespace UniversalDashboard.Execution [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.NewItem)] public sealed class GlobalCachedVariableProvider : ContainerCmdletProvider, IContentCmdletProvider { - private readonly IMemoryCache _memoryCache; + public static readonly Dictionary Cache; #region Constructor - /// - /// The constructor for the provider that exposes variables to the user - /// as drives. - /// - public GlobalCachedVariableProvider() + static GlobalCachedVariableProvider() { - _memoryCache = ExecutionService.MemoryCache; - } // constructor + Cache = new Dictionary(); + } #endregion Constructor @@ -49,24 +45,40 @@ protected override Collection InitializeDefaultDrives() } // InitializeDefaultDrives protected override void GetItem(string name) { - var item = _memoryCache.Get(name); - if (item != null) { + + if (Cache.ContainsKey(name.ToLower())) + { + var item = Cache[name.ToLower()]; base.WriteItemObject(item, name, false); } } protected override void NewItem(string path, string itemTypeName, object newItemValue) { - _memoryCache.Set(path.ToLower(), newItemValue); + if (Cache.ContainsKey(path.ToLower())) + { + Cache[path.ToLower()] = newItemValue; + } + else + { + Cache.Add(path.ToLower(), newItemValue); + } } protected override void SetItem(string name, object value) { - _memoryCache.Set(name.ToLower(), value); + if (Cache.ContainsKey(name.ToLower())) + { + Cache[name.ToLower()] = value; + } + else + { + Cache.Add(name.ToLower(), value); + } } protected override bool ItemExists(string path) { - return _memoryCache.TryGetValue(path.ToLower(), out object val); + return Cache.ContainsKey(path.ToLower()); } protected override bool IsValidPath(string path) { @@ -75,7 +87,7 @@ protected override bool IsValidPath(string path) { public void ClearContent(string path) { - _memoryCache.Remove(path.ToLower()); + Cache.Remove(path.ToLower()); } public object ClearContentDynamicParameters(string path) @@ -85,7 +97,7 @@ public object ClearContentDynamicParameters(string path) public IContentReader GetContentReader(string path) { - return new MemoryCacheContentReaderWriter(path.ToLower(), _memoryCache); + return new MemoryCacheContentReaderWriter(path.ToLower(), Cache); } public object GetContentReaderDynamicParameters(string path) @@ -95,7 +107,7 @@ public object GetContentReaderDynamicParameters(string path) public IContentWriter GetContentWriter(string path) { - return new MemoryCacheContentReaderWriter(path.ToLower(), _memoryCache); + return new MemoryCacheContentReaderWriter(path.ToLower(), Cache); } public object GetContentWriterDynamicParameters(string path) @@ -108,9 +120,9 @@ public object GetContentWriterDynamicParameters(string path) internal class MemoryCacheContentReaderWriter : IContentWriter, IContentReader { private readonly string _key; - private readonly IMemoryCache _memoryCache; + private readonly Dictionary _memoryCache; - public MemoryCacheContentReaderWriter(string key, IMemoryCache memoryCache) + public MemoryCacheContentReaderWriter(string key, Dictionary memoryCache) { _key = key; _memoryCache = memoryCache; @@ -128,7 +140,7 @@ public void Dispose() public IList Read(long readCount) { - if (_memoryCache.TryGetValue(_key, out object value)) + if (_memoryCache.TryGetValue(_key.ToLower(), out object value)) { return new ArrayList { @@ -146,13 +158,23 @@ public void Seek(long offset, SeekOrigin origin) public IList Write(IList content) { + object value; if (content.Count == 1) { - _memoryCache.Set(_key, content[0]); + value = content[0]; } else { - _memoryCache.Set(_key, content); + value = content; + } + + if (_memoryCache.ContainsKey(_key.ToLower())) + { + _memoryCache[_key.ToLower()] = value; + } + else + { + _memoryCache.Add(_key.ToLower(), value); } return content; diff --git a/src/UniversalDashboard/Models/SessionState.cs b/src/UniversalDashboard/Models/SessionState.cs index d7bd8425..54a391db 100644 --- a/src/UniversalDashboard/Models/SessionState.cs +++ b/src/UniversalDashboard/Models/SessionState.cs @@ -4,8 +4,9 @@ namespace UniversalDashboard.Models { public class SessionState { - public SessionState() + public SessionState(string id) { + Id = id; Endpoints = new Dictionary(); SessionVariables = new Dictionary(); ConnectionIds = new List(); @@ -13,6 +14,7 @@ public SessionState() } public object SyncRoot { get; set; } + public string Id { get; set; } public List ConnectionIds { get; set; } public Dictionary Endpoints { get; set; } public Dictionary SessionVariables { get; set; }