Skip to content

Commit

Permalink
New methods have been added to the cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 8, 2023
1 parent e975936 commit e15b174
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CacheDrive.Tests/CachingSimpleTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task SimpleTypesShouldBeProperlyDeletedFromTheMemoryCacheAsync()
}

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

Expand Down Expand Up @@ -160,7 +160,7 @@ public void SimpleTypesShouldBeProperlyDeletedFromTheMemoryCache()
}

// // Assert
int countCachedItems = cacheService.CountCachedItems();
int countCachedItems = cacheService.CountCachedObjects();
countCachedItems.Should().Be(25);
}
}
12 changes: 9 additions & 3 deletions CacheDrive.Tests/Helpers/TestDateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
62 changes: 62 additions & 0 deletions CacheDrive.Tests/MemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICacheService>();

// 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<ICacheService>();
IDateService dateService = serviceProvider.GetRequiredService<IDateService>();

// 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);
}
}
12 changes: 9 additions & 3 deletions CacheDrive/Services/DateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 13 additions & 3 deletions CacheDrive/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,18 @@ public interface ICacheService
Task<bool> DeleteAsync<T>(string key);

/// <summary>
/// Returns the number of items in the cache.
/// Returns the number of objects in the cache.
/// </summary>
/// <returns>The number of items in the cache.</returns>
int CountCachedItems();
/// <returns>The number of objects in the cache.</returns>
int CountCachedObjects();

/// <summary>
/// Removes all keys and values from the cache
/// </summary>
void ClearCache();

/// <summary>
/// Removes all expired keys and values from the cache
/// </summary>
void ClearExpiredObjects();
}
14 changes: 14 additions & 0 deletions CacheDrive/Services/IDateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@ namespace CacheDrive.Services;

public interface IDateService
{
/// <summary>
/// Returns the current utc date or the date that was set using the SetUtcNow method.
/// </summary>
DateTime GetUtcNow();

/// <summary>
/// Sets the given date as the current UTC date.
/// From now on, the GetUtcNow method will return this date.
/// </summary>
void SetUtcNow(DateTime dateNow);

/// <summary>
/// Removes the date set by the SetUtcNow method and restores the current UTC date.
/// </summary>
void SetUtcNow();
}
16 changes: 15 additions & 1 deletion CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,23 @@ private Task<bool> 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);

Expand Down

0 comments on commit e15b174

Please sign in to comment.