Skip to content

Commit

Permalink
Merge pull request #66 from BloodHoundAD/cache_updates
Browse files Browse the repository at this point in the history
Cache updates
  • Loading branch information
rvazarkar authored Jul 31, 2023
2 parents dcd0588 + fcf95f0 commit 9a3dd1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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);
}
}
}

0 comments on commit 9a3dd1e

Please sign in to comment.