Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ refactor: IMultilevelCacheClient => IDistributedCacheClient #598

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions src/Infrastructure/Masa.Mc.Infrastructure.Cache/CacheContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,48 @@

internal class CacheContext : ICacheContext
{
readonly IMultilevelCacheClient _multilevelCache;

readonly MultilevelCacheGlobalOptions _cacheOption;
readonly IDistributedCacheClient _distributedCache;

public CacheContext(
IMultilevelCacheClient multilevelCache,
IOptions<MultilevelCacheGlobalOptions> cacheOption)
IDistributedCacheClient distributedCache)
{
_multilevelCache = multilevelCache;
_cacheOption = cacheOption.Value;
_distributedCache = distributedCache;
}


public async Task SetAsync<T>(string key, T item, CacheEntryOptions? cacheEntryOptions)
{
await _multilevelCache.SetAsync(key, item, cacheEntryOptions ?? _cacheOption.CacheEntryOptions);
await _distributedCache.SetAsync(key, item, cacheEntryOptions);
}


public async Task<T?> GetAsync<T>(string key)
{
return await _multilevelCache.GetAsync<T>(key);
return await _distributedCache.GetAsync<T>(key);
}

public async Task Remove<T>(string key)
{
await _multilevelCache.RemoveAsync<T>(key);
await _distributedCache.RemoveAsync<T>(key);
}

public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> setter, CacheEntryOptions? cacheEntryOptions)
{
cacheEntryOptions ??= _cacheOption.CacheEntryOptions!;

var value = await _multilevelCache.GetAsync<T>(key);
var value = await _distributedCache.GetAsync<T>(key);

if (value != null)
return value;

value = await setter();

await _multilevelCache.SetAsync(key, value, new CombinedCacheEntryOptions
{
MemoryCacheEntryOptions = cacheEntryOptions,
DistributedCacheEntryOptions = cacheEntryOptions
});
await _distributedCache.SetAsync(key, value, cacheEntryOptions);

return value;
}

public async Task<T> GetOrSetAsync<T>(string key, Func<Task<(T, CacheEntryOptions cacheEntryOptions)>> setter)
{
var value = await _multilevelCache.GetAsync<T>(key);
var value = await _distributedCache.GetAsync<T>(key);

if (value != null)
return value;
Expand All @@ -62,11 +52,7 @@ public async Task<T> GetOrSetAsync<T>(string key, Func<Task<(T, CacheEntryOption
value = setterResult.Item1;
var cacheEntryOptions = setterResult.Item2;

await _multilevelCache.SetAsync(key, value, new CombinedCacheEntryOptions
{
MemoryCacheEntryOptions = cacheEntryOptions,
DistributedCacheEntryOptions = cacheEntryOptions
});
await _distributedCache.SetAsync(key, value, cacheEntryOptions);

return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ private async Task<string> GetClientCredentialsTokenAsync()
};
var tokenResponse = await _httpClient.RequestClientCredentialsTokenAsync(request);

var cacheEntryOptions = new CacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(tokenResponse.ExpiresIn - 60)
};
var cacheEntryOptions = new CacheEntryOptions(TimeSpan.FromSeconds(tokenResponse.ExpiresIn - 60));
return (tokenResponse.AccessToken, cacheEntryOptions);
}
);
Expand Down