Skip to content

Commit

Permalink
Changed the way cache is used
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Nov 28, 2023
1 parent fe9ca50 commit 716f376
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions CacheDrive.Tests/CachingObjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task ObjectShouldBeProperlyCachedAndRestoredFromTheMemory()

await cacheService.InitializeAsync();

await cacheService.SetAsync(_testClass);
await cacheService.SetAsync(_testClass.Id.ToString(), _testClass);

var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString());

Expand All @@ -59,7 +59,7 @@ public async Task ObjectShoulBeProperlyCachedInMemoryAndPersistedToTheFile()

await cacheService.InitializeAsync();

await cacheService.SetAsync(_testClass);
await cacheService.SetAsync(_testClass.Id.ToString(), _testClass);

var cachedTestClass = await cacheService.GetAsync<TestClass>(_testClass.Id.ToString());

Expand Down
2 changes: 1 addition & 1 deletion CacheDrive.Tests/MemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task StringShouldBeCorrectlySavedAndReadFromTheCacheInMemory(string

await cacheService.SetAsync(key, text);

string resultValue = await cacheService.GetAsync(key);
string resultValue = await cacheService.GetAsync<string>(key);

await cacheService.FlushAsync();

Expand Down
22 changes: 22 additions & 0 deletions CacheDrive/Models/ObjectItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;

namespace CacheDrive.Models;

public class ObjectItem<T> : ICacheable
{
[JsonPropertyName("key")]
public string Key { get; set; }

[JsonPropertyName("value")]
public T Value { get; set; }

[JsonIgnore]
public string CacheKey
=> GetCacheKey(Key);

public static string GetCacheKey(string key)
=> $"{nameof(ObjectItem<T>)}@{key}";

public static ObjectItem<T> Create(string key, T value)
=> new() { Key = key, Value = value };
}
10 changes: 3 additions & 7 deletions CacheDrive/Services/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ public interface ICacheService
{
bool HasItem(string key);

bool TryGetValue(string key, out string value);

bool TryGetValue<T>(string key, out T value) where T : ICacheable;
bool TryGetValue<T>(string key, out T value);

Task<string> GetAsync(string key);

Task<T> GetAsync<T>(string key) where T : ICacheable;
Task<T> GetAsync<T>(string key);

void Set<T>(T item, int expirySeconds = 0) where T : ICacheable;

Task SetAsync(string key, string value);
Task SetAsync<T>(string key, T value);

Task SetAsync<T>(T item, int expirySeconds = 0) where T : ICacheable;

Expand Down
49 changes: 32 additions & 17 deletions CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,30 @@ public bool HasItem(string key)
return false;
}

public bool TryGetValue(string key, out string value)
public bool TryGetValue<T>(string key, out T value)
{
if (TryGetValue(key, out SpecificField cachedSpecificField))
if (TryGetCacheableValue(key, out ObjectItem<T> result))
{
value = cachedSpecificField.Value;
return true;
if (result == null)
{
value = default;
return true;
}

if (result.Value is { } item)
{
value = item;
return true;
}
}

value = null;
value = default;
return false;
}

public bool TryGetValue<T>(string key, out T value) where T : ICacheable
private bool TryGetCacheableValue<T>(string key, out T value) where T : ICacheable
{
if (!Storage.TryGetValue(SpecificField.GetCacheKey(key), out var cachedItem))
if (!Storage.TryGetValue(ObjectItem<T>.GetCacheKey(key), out CachedItem cachedItem))
{
value = default;
return false;
Expand All @@ -68,25 +77,31 @@ public bool TryGetValue<T>(string key, out T value) where T : ICacheable
return true;
}

public async Task<string> GetAsync(string key)
public async Task<T> GetAsync<T>(string key)
{
var cachedSpecificField = await GetAsync<SpecificField>(key);
return await Task.FromResult(cachedSpecificField.Value);
var cachedItem = await GetCacheableAsync<ObjectItem<T>>(key);

if (cachedItem is null)
{
return await Task.FromResult<T>(default);
}

return await Task.FromResult(cachedItem.Value);
}

public async Task<T> GetAsync<T>(string key) where T : ICacheable
private async Task<T> GetCacheableAsync<T>(string key) where T : ICacheable
{
var cacheKey = T.GetCacheKey(key);

var cachedItem = Get(cacheKey);

if (cachedItem == null)
return default;
if (cachedItem is null)
return await Task.FromResult<T>(default);

if (cachedItem.Expired(_dateService))
{
await DeleteAsync(cachedItem);
return default;
return await Task.FromResult<T>(default);
}

return cachedItem.Unwrap<T>();
Expand All @@ -108,10 +123,10 @@ public void Set<T>(T item, int expirySeconds = 0) where T : ICacheable
var cachedItem = CachedItem.FromCacheable(item, length, _dateService, true);
Set(cachedItem);
}

public Task SetAsync(string key, string value)
public Task SetAsync<T>(string key, T value)
{
return SetAsync(SpecificField.Create(key, value));
return SetAsync(ObjectItem<T>.Create(key, value));
}

public Task SetAsync<T>(T item, int expirySeconds = 0) where T : ICacheable
Expand Down

0 comments on commit 716f376

Please sign in to comment.