Skip to content

Commit

Permalink
Updated InMemoryCachingProvider to stop creating unnecessary threads (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkatsiotis committed Jul 7, 2023
1 parent 3630012 commit 0a45d87
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ public override async Task<object> BaseGetAsync(string cacheKey, Type type, Canc
/// <returns>The async.</returns>
/// <param name="cacheKey">Cache key.</param>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default)
public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

await Task.Run(() => { _cache.Remove(cacheKey); }, cancellationToken);
_cache.Remove(cacheKey);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -175,7 +176,7 @@ public override async Task BaseRemoveAsync(string cacheKey, CancellationToken ca
/// <param name="expiration">Expiration.</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default)
public override Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls);
Expand All @@ -187,12 +188,10 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}

await Task.Run(() =>
{
//var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2);
//var val = new CacheValue<T>(cacheValue, true, valExpiration);
_cache.Set(cacheKey, cacheValue, expiration);
}, cancellationToken);
//var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2);
//var val = new CacheValue<T>(cacheValue, true, valExpiration);
_cache.Set(cacheKey, cacheValue, expiration);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -201,11 +200,11 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
/// <returns>The async.</returns>
/// <param name="cacheKey">Cache key.</param>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task<bool> BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default)
public override Task<bool> BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return await Task.FromResult(_cache.Exists(cacheKey));
return Task.FromResult(_cache.Exists(cacheKey));
}

/// <summary>
Expand All @@ -214,14 +213,15 @@ public override async Task<bool> BaseExistsAsync(string cacheKey, CancellationTo
/// <returns>The by prefix async.</returns>
/// <param name="prefix">Prefix.</param>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

var count = await Task.Run(() => _cache.RemoveByPrefix(prefix), cancellationToken);
var count = _cache.RemoveByPrefix(prefix);

if (_options.EnableLogging)
_logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix} , count = {count}");
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -230,17 +230,18 @@ public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationTo
/// <returns>The by prefix async.</returns>
/// <param name="pattern">Pattern.</param>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default)
public override Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(pattern, nameof(pattern));

var searchPattern = this.ProcessSearchKeyPattern(pattern);
var searchKey = this.HandleSearchKeyPattern(pattern);
var count = await Task.Run(() => _cache.RemoveByPattern(searchKey, searchPattern), cancellationToken);

var count = _cache.RemoveByPattern(searchKey, searchPattern);

if (_options.EnableLogging)
_logger?.LogInformation($"BaseRemoveByPatternAsync : pattern = {pattern} , count = {count}");
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -251,12 +252,13 @@ public override async Task BaseRemoveByPatternAsync(string pattern, Cancellation
/// <param name="expiration">Expiration.</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public override async Task BaseSetAllAsync<T>(IDictionary<string, T> values, TimeSpan expiration, CancellationToken cancellationToken = default)
public override Task BaseSetAllAsync<T>(IDictionary<string, T> values, TimeSpan expiration, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));

await Task.Run(() => _cache.SetAll(values, expiration), cancellationToken);
_cache.SetAll(values, expiration);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -266,16 +268,16 @@ public override async Task BaseSetAllAsync<T>(IDictionary<string, T> values, Tim
/// <param name="cacheKeys">Cache keys.</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
public override Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

if (_options.EnableLogging)
_logger?.LogInformation($"GetAllAsync : cacheKeys = {string.Join(",", cacheKeys)}");

return await Task.FromResult(_cache.GetAll<T>(cacheKeys));
return Task.FromResult(_cache.GetAll<T>(cacheKeys));
}


/// <summary>
/// Get all cacheKey by prefix async.
Expand All @@ -298,15 +300,15 @@ public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string pre
/// <param name="prefix">Prefix.</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public override async Task<IDictionary<string, CacheValue<T>>> BaseGetByPrefixAsync<T>(string prefix, CancellationToken cancellationToken = default)
public override Task<IDictionary<string, CacheValue<T>>> BaseGetByPrefixAsync<T>(string prefix, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));
var map = new Dictionary<string, CacheValue<T>>();

if (_options.EnableLogging)
_logger?.LogInformation($"GetByPrefixAsync : prefix = {prefix}");

return await Task.FromResult(_cache.GetByPrefix<T>(prefix));
return Task.FromResult(_cache.GetByPrefix<T>(prefix));
}

/// <summary>
Expand All @@ -315,28 +317,29 @@ public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string pre
/// <returns>The all async.</returns>
/// <param name="cacheKeys">Cache keys.</param>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task BaseRemoveAllAsync(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
public override Task BaseRemoveAllAsync(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
{
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

if (_options.EnableLogging)
_logger?.LogInformation($"RemoveAllAsync : cacheKeys = {string.Join(",", cacheKeys)}");

await Task.Run(() => _cache.RemoveAll(cacheKeys), cancellationToken);
_cache.RemoveAll(cacheKeys);
return Task.CompletedTask;
}

/// <summary>
/// Flush All Cached Item async.
/// </summary>
/// <returns>The async.</returns>
/// <param name="cancellationToken">CancellationToken</param>
public override async Task BaseFlushAsync(CancellationToken cancellationToken = default)
public override Task BaseFlushAsync(CancellationToken cancellationToken = default)
{
if (_options.EnableLogging)
_logger?.LogInformation("FlushAsync");

_cache.Clear();
await Task.CompletedTask;
return Task.CompletedTask;
}

/// <summary>
Expand Down

0 comments on commit 0a45d87

Please sign in to comment.