Skip to content

Commit 9a3dd1e

Browse files
authored
Merge pull request #66 from BloodHoundAD/cache_updates
Cache updates
2 parents dcd0588 + fcf95f0 commit 9a3dd1e

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/CommonLib/Cache.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Concurrent;
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.ComponentModel;
24
using System.Runtime.Serialization;
35
using SharpHoundCommonLib.Enums;
46

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

23+
private static Version defaultVersion = new(1, 0, 0);
24+
2125
private Cache()
2226
{
2327
ValueToIdCache = new ConcurrentDictionary<string, string>();
@@ -36,6 +40,8 @@ private Cache()
3640
[DataMember] public ConcurrentDictionary<string, string> SIDToDomainCache { get; private set; }
3741

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

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

@@ -137,9 +143,17 @@ private static string GetPrefixKey(string key, string domain)
137143
/// Creates a new empty cache instance
138144
/// </summary>
139145
/// <returns></returns>
140-
public static Cache CreateNewCache()
146+
public static Cache CreateNewCache(Version version = null)
141147
{
142-
return new Cache();
148+
if (version == null)
149+
{
150+
version = defaultVersion;
151+
}
152+
return new Cache
153+
{
154+
CacheCreationVersion = version,
155+
CacheCreationDate = DateTime.Now.Date
156+
};
143157
}
144158

145159
/// <summary>

test/unit/CacheTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using SharpHoundCommonLib;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace CommonLibTest
8+
{
9+
public class CacheTest
10+
{
11+
private ITestOutputHelper _testOutputHelper;
12+
public CacheTest(ITestOutputHelper testOutputHelper)
13+
{
14+
_testOutputHelper = testOutputHelper;
15+
}
16+
17+
[Fact]
18+
public void Cache_TestNewCache()
19+
{
20+
var cache = Cache.CreateNewCache();
21+
Assert.Equal(cache.CacheCreationVersion, new Version(1,0,0));
22+
var version = new Version(1, 0, 1);
23+
cache = Cache.CreateNewCache(version);
24+
var time = DateTime.Now.Date;
25+
Assert.Equal(cache.CacheCreationVersion, version);
26+
Assert.Equal(cache.CacheCreationDate, time);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)