From 8a1c31f57cdafd7589947ba5b505af2ea6d453ef Mon Sep 17 00:00:00 2001 From: kubagdynia Date: Sat, 2 Dec 2023 09:54:10 +0100 Subject: [PATCH] Added caching simple types tests --- CacheDrive.Tests/CachingSimpleTypesTests.cs | 63 +++++++++++++++++++++ CacheDrive/Services/ICacheService.cs | 22 +++++-- CacheDrive/Services/MemoryCacheService.cs | 13 +++-- 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 CacheDrive.Tests/CachingSimpleTypesTests.cs diff --git a/CacheDrive.Tests/CachingSimpleTypesTests.cs b/CacheDrive.Tests/CachingSimpleTypesTests.cs new file mode 100644 index 0000000..26a5f7b --- /dev/null +++ b/CacheDrive.Tests/CachingSimpleTypesTests.cs @@ -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(); + + 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(intItem.key); + char cachedCharItem = await cacheService.GetAsync(charItem.key); + float cachedFloatItem = await cacheService.GetAsync(floatItem.key); + double cachedDoubleItem = await cacheService.GetAsync(doubleItem.key); + bool cachedBoolItem = await cacheService.GetAsync(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 +} \ No newline at end of file diff --git a/CacheDrive/Services/ICacheService.cs b/CacheDrive/Services/ICacheService.cs index 827696b..f022540 100644 --- a/CacheDrive/Services/ICacheService.cs +++ b/CacheDrive/Services/ICacheService.cs @@ -4,6 +4,22 @@ namespace CacheDrive.Services; public interface ICacheService { + /// + /// 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. + /// + /// + Task InitializeAsync(); + + /// + /// 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. + /// + /// + Task FlushAsync(); + bool HasItem(string key); bool TryGetValue(string key, out T value); @@ -23,8 +39,6 @@ public interface ICacheService void DeletePrefix(string prefix); Task DeletePrefixAsync(string prefix); - - Task FlushAsync(); - - Task InitializeAsync(); + + int CountCacheItems(); } \ No newline at end of file diff --git a/CacheDrive/Services/MemoryCacheService.cs b/CacheDrive/Services/MemoryCacheService.cs index 94715f6..1f87233 100644 --- a/CacheDrive/Services/MemoryCacheService.cs +++ b/CacheDrive/Services/MemoryCacheService.cs @@ -27,6 +27,12 @@ public MemoryCacheService(IOptions 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)) @@ -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);