Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache updates #66

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/CommonLib/Cache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.Serialization;
using SharpHoundCommonLib.Enums;

Expand All @@ -18,6 +20,8 @@ public class Cache
//
// [DataMember]private ConcurrentDictionary<string, string> _valueToIDCache;

private static Version defaultVersion = new(1, 0, 0);

private Cache()
{
ValueToIdCache = new ConcurrentDictionary<string, string>();
Expand All @@ -36,6 +40,8 @@ private Cache()
[DataMember] public ConcurrentDictionary<string, string> SIDToDomainCache { get; private set; }

[DataMember] public ConcurrentDictionary<string, string> ValueToIdCache { get; private set; }
[DataMember] public DateTime CacheCreationDate { get; set; }
[DataMember] public Version CacheCreationVersion { get; set; }

[IgnoreDataMember] private static Cache CacheInstance { get; set; }

Expand Down Expand Up @@ -137,9 +143,17 @@ private static string GetPrefixKey(string key, string domain)
/// Creates a new empty cache instance
/// </summary>
/// <returns></returns>
public static Cache CreateNewCache()
public static Cache CreateNewCache(Version version = null)
{
return new Cache();
if (version == null)
{
version = defaultVersion;
}
return new Cache
{
CacheCreationVersion = version,
CacheCreationDate = DateTime.Now.Date
};
}

/// <summary>
Expand Down
29 changes: 29 additions & 0 deletions test/unit/CacheTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Newtonsoft.Json;
using SharpHoundCommonLib;
using Xunit;
using Xunit.Abstractions;

namespace CommonLibTest
{
public class CacheTest
{
private ITestOutputHelper _testOutputHelper;
public CacheTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void Cache_TestNewCache()
{
var cache = Cache.CreateNewCache();
Assert.Equal(cache.CacheCreationVersion, new Version(1,0,0));
var version = new Version(1, 0, 1);
cache = Cache.CreateNewCache(version);
var time = DateTime.Now.Date;
Assert.Equal(cache.CacheCreationVersion, version);
Assert.Equal(cache.CacheCreationDate, time);
}
}
}
Loading