Skip to content

Commit

Permalink
Adding Clear-UDCache command and use dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed Oct 2, 2019
1 parent f1df42c commit 7d215db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
14 changes: 14 additions & 0 deletions src/UniversalDashboard/Cmdlets/ClearCacheCommand.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
2 changes: 1 addition & 1 deletion src/UniversalDashboard/Execution/EndpointProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
connectionId
}
Expand Down
66 changes: 44 additions & 22 deletions src/UniversalDashboard/Execution/GlobalCachedVariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<string, object> Cache;

#region Constructor

/// <summary>
/// The constructor for the provider that exposes variables to the user
/// as drives.
/// </summary>
public GlobalCachedVariableProvider()
static GlobalCachedVariableProvider()
{
_memoryCache = ExecutionService.MemoryCache;
} // constructor
Cache = new Dictionary<string, object>();
}

#endregion Constructor

Expand All @@ -49,24 +45,40 @@ protected override Collection<PSDriveInfo> 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) {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<string, object> _memoryCache;

public MemoryCacheContentReaderWriter(string key, IMemoryCache memoryCache)
public MemoryCacheContentReaderWriter(string key, Dictionary<string, object> memoryCache)
{
_key = key;
_memoryCache = memoryCache;
Expand All @@ -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
{
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/UniversalDashboard/Models/SessionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ namespace UniversalDashboard.Models
{
public class SessionState
{
public SessionState()
public SessionState(string id)
{
Id = id;
Endpoints = new Dictionary<string, Endpoint>();
SessionVariables = new Dictionary<string, object>();
ConnectionIds = new List<string>();
SyncRoot = new object();
}

public object SyncRoot { get; set; }
public string Id { get; set; }
public List<string> ConnectionIds { get; set; }
public Dictionary<string, Endpoint> Endpoints { get; set; }
public Dictionary<string, object> SessionVariables { get; set; }
Expand Down

0 comments on commit 7d215db

Please sign in to comment.