Skip to content

Commit

Permalink
⚡ refactor: IMultilevelCacheClient => IDistributedCacheClient
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Sep 14, 2024
1 parent 14f7d4f commit 322bd0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
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

0 comments on commit 322bd0b

Please sign in to comment.