Skip to content

Commit

Permalink
Fixed deleting items from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 3, 2023
1 parent ec57717 commit 9589bd7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
37 changes: 37 additions & 0 deletions CacheDrive.Tests/CachingSimpleTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,41 @@ public async Task SimpleTypesShouldBeProperlyCachedAndRestoredFromTheMemory()
cachedDoubleItem.Should().Be(doubleItem.value);
cachedBoolItem.Should().Be(boolItem.value);
}

[Test]
public async Task SimpleTypesShouldBeProperlyDeletedFromTheMemoryCache()
{
// 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

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

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

// // Assert
int countCachedItems = cacheService.CountCachedItems();
countCachedItems.Should().Be(25);
}
}
6 changes: 3 additions & 3 deletions CacheDrive/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public interface ICacheService
/// </summary>
/// <param name="key">The key of the element to be remove.</param>
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
bool Delete(string key);
bool Delete<T>(string key);

/// <summary>
/// Deletes the object associated with the given key.
/// </summary>
/// <param name="key">The key of the element to be remove.</param>
/// <returns>true if an object was removed successfully; otherwise, false.</returns>
Task<bool> DeleteAsync(string key);
Task<bool> DeleteAsync<T>(string key);

/// <summary>
/// Returns the number of items in the cache.
/// </summary>
/// <returns>The number of items in the cache.</returns>
int CountCacheItems();
int CountCachedItems();
}
17 changes: 8 additions & 9 deletions CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using CacheDrive.Configuration;
Expand Down Expand Up @@ -76,7 +75,7 @@ public bool TryGetValue<T>(string key, out T value)

if (cachedItem.Expired(_dateService))
{
DeleteAsync(cachedItem);
Delete<T>(cachedItem.Key);
value = default;
return false;
}
Expand Down Expand Up @@ -168,19 +167,19 @@ internal Task SetAsync(CachedItem cachedItem)
return Task.CompletedTask;
}

public bool Delete(string key)
=> key is not null && Storage.TryRemove(key, out _);
public bool Delete<T>(string key)
=> key is not null && Storage.TryRemove(CacheableItem<T>.GetCacheKey(key), out _);

public Task<bool> DeleteAsync(string key)
=> key is null ? Task.FromResult(false) : Task.FromResult(Storage.TryRemove(key, out _));
public Task<bool> DeleteAsync<T>(string key)
=> key is null ? Task.FromResult(false) : DeleteAsync(CacheableItem<T>.GetCacheKey(key));

private bool Delete(CachedItem item)
=> Delete(item.Key);
private Task<bool> DeleteAsync(string key)
=> key is null ? Task.FromResult(false) : Task.FromResult(Storage.TryRemove(key, out _));

private Task<bool> DeleteAsync(CachedItem item)
=> DeleteAsync(item.Key);

public int CountCacheItems()
public int CountCachedItems()
=> Storage.Count;

private CachedItem Get(ICacheable item)
Expand Down

0 comments on commit 9589bd7

Please sign in to comment.