Skip to content

Commit

Permalink
Added the ability to hash the key. Can be used for long keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed May 8, 2024
1 parent 3c33e47 commit 88b850e
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 3 deletions.
24 changes: 21 additions & 3 deletions CacheDrive.ExampleConsoleApp/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@ public async Task Run()
Console.WriteLine(cacheService.TryGetValue(cacheKey1, out string cachedValue2)
? $"TryGetValue OK - cached value: {cachedValue2}"
: $"TryGetValue NOK - cached value: {cachedValue2}");

// SetAsync, GetAsync and TryGetValue with hashKey
string cacheKey2 = "testKey";
await cacheService.SetAsync(cacheKey2, "test text...", hashKey: true);
var cachedValueWithHashKey1 = await cacheService.GetAsync<string>(cacheKey2, hashKey: true);
Console.WriteLine($"GetAsync with hashKey - cached value: {cachedValueWithHashKey1}");

Console.WriteLine(cacheService.TryGetValue(cacheKey2, hashKey: true, out string cachedValueWithHashKey2)
? $"TryGetValue with haskkey OK - cached value: {cachedValueWithHashKey2}"
: $"TryGetValue with haskkey NOK - cached value: {cachedValueWithHashKey2}");

Console.WriteLine();

// Set, Get
string cacheKey2 = "testKey2";
cacheService.Set(cacheKey2, 1234567);
int cachedValue3 = cacheService.Get<int>(cacheKey2);
string cacheKey3 = "testKey2";
cacheService.Set(cacheKey3, 1234567);
int cachedValue3 = cacheService.Get<int>(cacheKey3);
Console.WriteLine($"Get - cached value: {cachedValue3} ");

// Set, Get with hashKey
string cacheKey4 = "testKey2";
cacheService.Set(cacheKey4, 1234567, hashKey: true);
int cachedValueWithHashKey3 = cacheService.Get<int>(cacheKey4, hashKey: true);
Console.WriteLine($"Get with hashKey - cached value: {cachedValueWithHashKey3} ");
}
}
132 changes: 132 additions & 0 deletions CacheDrive.Tests/CachingObjectsHashKeyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Text.Json.Serialization;
using CacheDrive.Configuration;
using CacheDrive.Services;
using CacheDrive.Tests.Helpers;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

namespace CacheDrive.Tests;

public class CachingObjectsHashKeyTests
{
private TestClass _testClass;

[SetUp]
public void SetUp()
{
_testClass = new TestClass
{
Id = Guid.Parse("5b8e8e5c-6ad5-43c0-94d8-ef68e96eaef8"),
FirstName = "John",
LastName = "Doe",
Age = 50
};
}

[Test]
public async Task ObjectShouldBeProperlyCachedAndRestoredFromTheMemory()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.Memory);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
await cacheService.SetAsync(_testClass.Id.ToString(), _testClass, hashKey: true);

var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString(), hashKey: true);

await cacheService.FlushAsync();

// Assert
cachedTestClass.Should().BeEquivalentTo(_testClass);
}

[Test, Order(1)]
public async Task ObjectShoulBeProperlyCachedInMemoryAndPersistedToTheFile()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.MemoryAndFile);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
await cacheService.SetAsync(_testClass.Id.ToString(), _testClass, hashKey: true);

var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString(), hashKey: true);

await cacheService.FlushAsync();

// Assert
cachedTestClass.Should().BeEquivalentTo(_testClass);
}

[Test, Order(2)]
public async Task CacheShouldBeProperlyLoadedFromTheFile_Then_ObjectShouldBeProperlyRestoredFromTheMemory()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.MemoryAndFile);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString(), hashKey: true);

await cacheService.FlushAsync();

// Assert
cachedTestClass.Should().BeEquivalentTo(_testClass);
}

[Test, Order(3)]
public async Task CacheShouldExpiredDuringLoadedFromTheFile_Then_ObjectShouldNotBeProperlyRestoredFromTheMemory()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now.AddHours(3),
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.MemoryAndFile);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString(), hashKey: true);

await cacheService.FlushAsync();

// Assert
cachedTestClass.Should().BeNull();
}

private class TestClass
{
[JsonPropertyName("id")]
public Guid Id { get; set; }

[JsonPropertyName("firstName")]
public string FirstName { get; set; }

[JsonPropertyName("lastName")]
public string LastName { get; set; }

[JsonPropertyName("age")]
public int Age { get; set; }
}
}
166 changes: 166 additions & 0 deletions CacheDrive.Tests/CachingSimpleTypesHashKeyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using CacheDrive.Configuration;
using CacheDrive.Services;
using CacheDrive.Tests.Helpers;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

namespace CacheDrive.Tests;

public class CachingSimpleTypesHashKeyTests
{
[Test]
public async Task SimpleTypesShouldBeProperlyCachedAndRestoredFromTheMemoryAsync()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.Memory);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
(string key, int value) intItem = ("int_test_async", 10);
await cacheService.SetAsync(intItem.key, intItem.value, hashKey: true);

(string key, char value) charItem = ("char_test_async", 'p');
await cacheService.SetAsync(charItem.key, charItem.value, hashKey: true);

(string key, float value) floatItem = ("float_test_async", 4.8f);
await cacheService.SetAsync(floatItem.key, floatItem.value, hashKey: true);

(string key, double value) doubleItem = ("double_test_async", 6.8d);
await cacheService.SetAsync(doubleItem.key, doubleItem.value, hashKey: true);

(string key, bool value) boolItem = ("bool_test_async", true);
await cacheService.SetAsync(boolItem.key, boolItem.value, hashKey: true);

int cachedIntItem = await cacheService.GetAsync<int>(intItem.key, hashKey: true);
char cachedCharItem = await cacheService.GetAsync<char>(charItem.key, hashKey: true);
float cachedFloatItem = await cacheService.GetAsync<float>(floatItem.key, hashKey: true);
double cachedDoubleItem = await cacheService.GetAsync<double>(doubleItem.key, hashKey: true);
bool cachedBoolItem = await cacheService.GetAsync<bool>(boolItem.key, hashKey: true);

// Assert
cachedIntItem.Should().Be(intItem.value);
cachedCharItem.Should().Be(charItem.value);
cachedFloatItem.Should().Be(floatItem.value);
cachedDoubleItem.Should().Be(doubleItem.value);
cachedBoolItem.Should().Be(boolItem.value);
}

[Test]
public void SimpleTypesShouldBeProperlyCachedAndRestoredFromTheMemory()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.Memory);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act
(string key, int value) intItem = ("int_test", 10);
cacheService.Set(intItem.key, intItem.value, hashKey: true);

(string key, char value) charItem = ("char_test", 'p');
cacheService.Set(charItem.key, charItem.value, hashKey: true);

(string key, float value) floatItem = ("float_test", 4.8f);
cacheService.Set(floatItem.key, floatItem.value, hashKey: true);

(string key, double value) doubleItem = ("double_test", 6.8d);
cacheService.Set(doubleItem.key, doubleItem.value, hashKey: true);

(string key, bool value) boolItem = ("bool_test", true);
cacheService.Set(boolItem.key, boolItem.value, hashKey: true);

int cachedIntItem = cacheService.Get<int>(intItem.key, hashKey: true);
char cachedCharItem = cacheService.Get<char>(charItem.key, hashKey: true);
float cachedFloatItem = cacheService.Get<float>(floatItem.key, hashKey: true);
double cachedDoubleItem = cacheService.Get<double>(doubleItem.key, hashKey: true);
bool cachedBoolItem = cacheService.Get<bool>(boolItem.key, hashKey: true);

// Assert
cachedIntItem.Should().Be(intItem.value);
cachedCharItem.Should().Be(charItem.value);
cachedFloatItem.Should().Be(floatItem.value);
cachedDoubleItem.Should().Be(doubleItem.value);
cachedBoolItem.Should().Be(boolItem.value);
}

[Test]
public async Task SimpleTypesShouldBeProperlyDeletedFromTheMemoryCacheAsync()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.Memory);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act

// Add 50 items
for (int i = 0; i < 50; i++)
{
await cacheService.SetAsync($"test_{i}_async", i, hashKey: true);
}

// Delete every second element
for (int i = 0; i < 50; i++)
{
if ((i + 1) % 2 == 0)
{
await cacheService.DeleteAsync<int>($"test_{i}_async", hashKey: true);
}
}

// // Assert
int countCachedItems = cacheService.CountCachedObjects();
countCachedItems.Should().Be(25);
}

[Test]
public void SimpleTypesShouldBeProperlyDeletedFromTheMemoryCache()
{
// Arrange
ServiceProvider serviceProvider = TestHelper.CreateServiceProvider(
DateTime.Now,
cacheEnabled: true,
cacheExpirationType: CacheExpirationType.Hours,
cacheExpiration: 2,
cacheType: CacheType.Memory);

ICacheService cacheService = serviceProvider.GetRequiredService<ICacheService>();

// Act

// Add 50 items
for (int i = 0; i < 50; i++)
{
cacheService.Set($"test_{i}", i, hashKey: true);
}

// Delete every second element
for (int i = 0; i < 50; i++)
{
if ((i + 1) % 2 == 0)
{
cacheService.Delete<int>($"test_{i}", hashKey: true);
}
}

// // Assert
int countCachedItems = cacheService.CountCachedObjects();
countCachedItems.Should().Be(25);
}
}
Loading

0 comments on commit 88b850e

Please sign in to comment.