Skip to content

Commit

Permalink
Added caching simple types tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 2, 2023
1 parent fe950a9 commit 8a1c31f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
63 changes: 63 additions & 0 deletions CacheDrive.Tests/CachingSimpleTypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using CacheDrive.Configuration;
using CacheDrive.Services;
using CacheDrive.Tests.Helpers;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

namespace CacheDrive.Tests;

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

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

await cacheService.InitializeAsync();

// Act
(string key, int value) intItem = ("int_test", 10);
await cacheService.SetAsync(intItem.key, intItem.value);

(string key, char value) charItem = ("char_test", 'p');
await cacheService.SetAsync(charItem.key, charItem.value);

(string key, float value) floatItem = ("float_test", 4.8f);
await cacheService.SetAsync(floatItem.key, floatItem.value);

(string key, double value) doubleItem = ("double_test", 6.8d);
await cacheService.SetAsync(doubleItem.key, doubleItem.value);

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


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

await cacheService.FlushAsync();

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

public enum Tt
{
One,
Two
}
22 changes: 18 additions & 4 deletions CacheDrive/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ namespace CacheDrive.Services;

public interface ICacheService
{
/// <summary>
/// Initializes the cache, such as loading data from a file, database, and so on.
/// Should be run before the cache is used, usually at application startup.
/// Can be used many times, each time adding or overwriting data if they have the same keys.
/// </summary>
/// <returns></returns>
Task InitializeAsync();

/// <summary>
/// Dumps cached data into files, database, and so on.
/// Usually it should be run before the application terminates.
/// Can be used many times, each time saving data that are new or has been changed.
/// </summary>
/// <returns></returns>
Task FlushAsync();

bool HasItem(string key);

bool TryGetValue<T>(string key, out T value);
Expand All @@ -23,8 +39,6 @@ public interface ICacheService
void DeletePrefix(string prefix);

Task DeletePrefixAsync(string prefix);

Task FlushAsync();

Task InitializeAsync();

int CountCacheItems();
}
13 changes: 8 additions & 5 deletions CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public MemoryCacheService(IOptions<CacheSettings> settings, IDateService dateSer

protected IDateService DateService => _dateService;

public virtual Task InitializeAsync()
=> Task.CompletedTask;

public virtual Task FlushAsync()
=> Task.CompletedTask;

public bool HasItem(string key)
{
if (Storage.TryGetValue(key, out CachedItem cachedItem))
Expand Down Expand Up @@ -212,11 +218,8 @@ public Task DeletePrefixAsync(string prefix)
return Task.CompletedTask;
}

public virtual Task FlushAsync()
=> Task.CompletedTask;

public virtual Task InitializeAsync()
=> Task.CompletedTask;
public int CountCacheItems()
=> Storage.Count;

private CachedItem Get(ICacheable item)
=> Get(item.CacheKey);
Expand Down

0 comments on commit 8a1c31f

Please sign in to comment.