From e15b174740f0e847c665e0ffd53c96e989dfa6d0 Mon Sep 17 00:00:00 2001 From: kubagdynia Date: Fri, 8 Dec 2023 17:30:25 +0100 Subject: [PATCH] New methods have been added to the cache service --- CacheDrive.Tests/CachingSimpleTypesTests.cs | 4 +- CacheDrive.Tests/Helpers/TestDateService.cs | 12 +++- CacheDrive.Tests/MemoryTests.cs | 62 +++++++++++++++++++++ CacheDrive/Services/DateService.cs | 12 +++- CacheDrive/Services/ICacheService.cs | 16 +++++- CacheDrive/Services/IDateService.cs | 14 +++++ CacheDrive/Services/MemoryCacheService.cs | 16 +++++- 7 files changed, 124 insertions(+), 12 deletions(-) diff --git a/CacheDrive.Tests/CachingSimpleTypesTests.cs b/CacheDrive.Tests/CachingSimpleTypesTests.cs index 26b1d5c..28327a3 100644 --- a/CacheDrive.Tests/CachingSimpleTypesTests.cs +++ b/CacheDrive.Tests/CachingSimpleTypesTests.cs @@ -125,7 +125,7 @@ public async Task SimpleTypesShouldBeProperlyDeletedFromTheMemoryCacheAsync() } // // Assert - int countCachedItems = cacheService.CountCachedItems(); + int countCachedItems = cacheService.CountCachedObjects(); countCachedItems.Should().Be(25); } @@ -160,7 +160,7 @@ public void SimpleTypesShouldBeProperlyDeletedFromTheMemoryCache() } // // Assert - int countCachedItems = cacheService.CountCachedItems(); + int countCachedItems = cacheService.CountCachedObjects(); countCachedItems.Should().Be(25); } } \ No newline at end of file diff --git a/CacheDrive.Tests/Helpers/TestDateService.cs b/CacheDrive.Tests/Helpers/TestDateService.cs index a3b0fe9..36d2fe1 100644 --- a/CacheDrive.Tests/Helpers/TestDateService.cs +++ b/CacheDrive.Tests/Helpers/TestDateService.cs @@ -4,8 +4,14 @@ namespace CacheDrive.Tests.Helpers; public class TestDateService(DateTime utcNow) : IDateService { + private DateTime? _utcNow = utcNow; + public DateTime GetUtcNow() - { - return utcNow; - } + => _utcNow ?? DateTime.UtcNow; + + public void SetUtcNow(DateTime dateNow) + => _utcNow = dateNow; + + public void SetUtcNow() + => _utcNow = null; } \ No newline at end of file diff --git a/CacheDrive.Tests/MemoryTests.cs b/CacheDrive.Tests/MemoryTests.cs index 0698980..9a3e8d7 100644 --- a/CacheDrive.Tests/MemoryTests.cs +++ b/CacheDrive.Tests/MemoryTests.cs @@ -30,4 +30,66 @@ public async Task StringShouldBeCorrectlySavedAndReadFromTheCacheInMemory(string // Assert resultValue.Should().Be(text); } + + [Test] + public void ClearingTheCacheShouldRemoveAllObjectsFromTheCache() + { + // Arrange + ServiceProvider serviceProvider = TestHelper.CreateServiceProvider( + DateTime.Now, + cacheEnabled: true, + cacheExpirationType: CacheExpirationType.Hours, + cacheExpiration: 2, + cacheType: CacheType.Memory); + + ICacheService cacheService = serviceProvider.GetRequiredService(); + + // Act + for (int i = 0; i < 100; i++) + { + cacheService.Set($"k-{i}", i); + } + + // Assert + cacheService.CountCachedObjects().Should().Be(100); + cacheService.ClearCache(); + cacheService.CountCachedObjects().Should().Be(0); + } + + [Test] + public void ClearingTheCacheShouldRemoveAllObjectsFromTheCache2() + { + // Arrange + ServiceProvider serviceProvider = TestHelper.CreateServiceProvider( + DateTime.Now, + cacheEnabled: true, + cacheExpirationType: CacheExpirationType.Hours, + cacheExpiration: 2, + cacheType: CacheType.Memory); + + ICacheService cacheService = serviceProvider.GetRequiredService(); + IDateService dateService = serviceProvider.GetRequiredService(); + + // Act + for (int i = 0; i < 50; i++) + { + cacheService.Set($"h0-{i}", i); + } + + dateService.SetUtcNow(dateService.GetUtcNow().AddHours(1)); + + for (int i = 0; i < 50; i++) + { + cacheService.Set($"h1-{i}", i); + } + + dateService.SetUtcNow(dateService.GetUtcNow().AddHours(2)); + + // Assert + cacheService.CountCachedObjects().Should().Be(100); + + cacheService.ClearExpiredObjects(); + + cacheService.CountCachedObjects().Should().Be(50); + } } \ No newline at end of file diff --git a/CacheDrive/Services/DateService.cs b/CacheDrive/Services/DateService.cs index 60d783e..df433f3 100644 --- a/CacheDrive/Services/DateService.cs +++ b/CacheDrive/Services/DateService.cs @@ -4,8 +4,14 @@ namespace CacheDrive.Services; internal class DateService : IDateService { + private DateTime? _utcNow; + public DateTime GetUtcNow() - { - return DateTime.UtcNow; - } + => _utcNow ?? DateTime.UtcNow; + + public void SetUtcNow(DateTime dateNow) + => _utcNow = dateNow; + + public void SetUtcNow() + => _utcNow = null; } \ No newline at end of file diff --git a/CacheDrive/Services/ICacheService.cs b/CacheDrive/Services/ICacheService.cs index 1a5b642..e79d975 100644 --- a/CacheDrive/Services/ICacheService.cs +++ b/CacheDrive/Services/ICacheService.cs @@ -88,8 +88,18 @@ public interface ICacheService Task DeleteAsync(string key); /// - /// Returns the number of items in the cache. + /// Returns the number of objects in the cache. /// - /// The number of items in the cache. - int CountCachedItems(); + /// The number of objects in the cache. + int CountCachedObjects(); + + /// + /// Removes all keys and values from the cache + /// + void ClearCache(); + + /// + /// Removes all expired keys and values from the cache + /// + void ClearExpiredObjects(); } \ No newline at end of file diff --git a/CacheDrive/Services/IDateService.cs b/CacheDrive/Services/IDateService.cs index ace5dcc..81e7978 100644 --- a/CacheDrive/Services/IDateService.cs +++ b/CacheDrive/Services/IDateService.cs @@ -4,5 +4,19 @@ namespace CacheDrive.Services; public interface IDateService { + /// + /// Returns the current utc date or the date that was set using the SetUtcNow method. + /// DateTime GetUtcNow(); + + /// + /// Sets the given date as the current UTC date. + /// From now on, the GetUtcNow method will return this date. + /// + void SetUtcNow(DateTime dateNow); + + /// + /// Removes the date set by the SetUtcNow method and restores the current UTC date. + /// + void SetUtcNow(); } \ No newline at end of file diff --git a/CacheDrive/Services/MemoryCacheService.cs b/CacheDrive/Services/MemoryCacheService.cs index 52fb1da..3378fb5 100644 --- a/CacheDrive/Services/MemoryCacheService.cs +++ b/CacheDrive/Services/MemoryCacheService.cs @@ -220,9 +220,23 @@ private Task DeleteAsync(CachedItem item) private bool Delete(CachedItem item) => Delete(item.Key); - public int CountCachedItems() + public int CountCachedObjects() => Storage.Count; + public void ClearCache() + => Storage.Clear(); + + public void ClearExpiredObjects() + { + foreach ((string key, CachedItem item) in Storage) + { + if (item.Expired(DateService)) + { + Delete(key); + } + } + } + private CachedItem Get(ICacheable item) => Get(item.CacheKey);