Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
billhong-just committed Jan 10, 2024
1 parent 266bec0 commit e65af7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
32 changes: 17 additions & 15 deletions src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ public override async Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey, Func<
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

var result = _cache.Get<T>(cacheKey);
if (result.HasValue)
bool getLock;
do
{
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");
var result = _cache.Get<T>(cacheKey);
if (result.HasValue)
{
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");

CacheStats.OnHit();
CacheStats.OnHit();

return result;
}
return result;
}

CacheStats.OnMiss();
CacheStats.OnMiss();

if (_options.EnableLogging)
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");

if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)))
{
//wait for some ms
await Task.Delay(_options.SleepMs, cancellationToken);
return await GetAsync(cacheKey, dataRetriever, expiration);
getLock = _cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs));
if (!getLock)
await Task.Delay(_options.SleepMs, cancellationToken);
}
while (!getLock);

try
{
Expand Down
31 changes: 17 additions & 14 deletions src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,30 @@ public override CacheValue<T> BaseGet<T>(string cacheKey, Func<T> dataRetriever,
////mutex key
//Lock(cacheKey);

var result = _cache.Get<T>(cacheKey);
if (result.HasValue)
bool getLock;
do
{
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");
var result = _cache.Get<T>(cacheKey);
if (result.HasValue)
{
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");

CacheStats.OnHit();
CacheStats.OnHit();

return result;
}
return result;
}

CacheStats.OnMiss();
CacheStats.OnMiss();

if (_options.EnableLogging)
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
if (_options.EnableLogging)
_logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");

if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)))
{
System.Threading.Thread.Sleep(_options.SleepMs);
return Get(cacheKey, dataRetriever, expiration);
getLock = _cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs));
if (!getLock)
System.Threading.Thread.Sleep(_options.SleepMs);
}
while (!getLock);

try
{
Expand Down

0 comments on commit e65af7c

Please sign in to comment.