Skip to content

Commit

Permalink
🔨 Refactoring HybridCachingProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Apr 16, 2018
1 parent 81d82c0 commit a59e135
Showing 1 changed file with 52 additions and 127 deletions.
179 changes: 52 additions & 127 deletions src/EasyCaching.HybridCache/HybridCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,11 @@ public bool Exists(string cacheKey)

var flag = false;

var local = _providers.FirstOrDefault();

flag = local.Exists(cacheKey);

if (!flag)
foreach (var provider in _providers)
{
//remote
foreach (var provider in _providers.Skip(1))
{
flag = provider.Exists(cacheKey);
flag = provider.Exists(cacheKey);

if (flag) break;
}
if (flag) break;
}

return flag;
Expand All @@ -104,23 +96,15 @@ public async Task<bool> ExistsAsync(string cacheKey)
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var flag = false;

var local = _providers.FirstOrDefault();

flag = await local.ExistsAsync(cacheKey);

if (!flag)

foreach (var provider in _providers)
{
//remote
foreach (var provider in _providers.Skip(1))
{
flag = provider.Exists(cacheKey);
flag = provider.Exists(cacheKey);

if (flag) break;
}
if (flag) break;
}

return flag;
return await Task.FromResult(flag);
}

/// <summary>
Expand All @@ -136,17 +120,9 @@ public CacheValue<T> Get<T>(string cacheKey, Func<T> dataRetriever, TimeSpan exp
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

var local = _providers.FirstOrDefault();

CacheValue<T> cachedValue = local.Get(cacheKey, dataRetriever, expiration);

if (cachedValue.HasValue)
{
return cachedValue;
}

//remote
foreach (var provider in _providers.Skip(1))
CacheValue<T> cachedValue = null;

foreach (var provider in _providers)
{
cachedValue = provider.Get(cacheKey, dataRetriever, expiration);

Expand Down Expand Up @@ -184,17 +160,9 @@ public CacheValue<T> Get<T>(string cacheKey) where T : class
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var local = _providers.FirstOrDefault();

CacheValue<T> cachedValue = local.Get<T>(cacheKey);

if (cachedValue.HasValue)
{
return cachedValue;
}

//remote
foreach (var provider in _providers.Skip(1))
CacheValue<T> cachedValue = null;

foreach (var provider in _providers)
{
cachedValue = provider.Get<T>(cacheKey);

Expand Down Expand Up @@ -225,17 +193,9 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey, Func<Task<T>> data
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

var local = _providers.FirstOrDefault();

CacheValue<T> cachedValue = await local.GetAsync(cacheKey, dataRetriever, expiration);

if (cachedValue.HasValue)
{
return cachedValue;
}
CacheValue<T> cachedValue = null;

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
cachedValue = provider.Get<T>(cacheKey);

Expand All @@ -254,9 +214,7 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey, Func<Task<T>> data
return new CacheValue<T>(retriever, true);
}
else
{
//TODO : Set a null value to cache!!

{
return CacheValue<T>.NoValue;
}
}
Expand All @@ -274,12 +232,9 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var local = _providers.FirstOrDefault();

CacheValue<T> cachedValue = await local.GetAsync<T>(cacheKey);
CacheValue<T> cachedValue = null;

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
cachedValue = provider.Get<T>(cacheKey);

Expand All @@ -294,7 +249,7 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class
return CacheValue<T>.NoValue;
}

return cachedValue;
return await Task.FromResult(cachedValue);
}

/// <summary>
Expand All @@ -306,12 +261,7 @@ public void Remove(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var local = _providers.FirstOrDefault();

local.Remove(cacheKey);

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.Remove(cacheKey);
}
Expand All @@ -326,15 +276,14 @@ public async Task RemoveAsync(string cacheKey)
{
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

var local = _providers.FirstOrDefault();

await local.RemoveAsync(cacheKey);
var tasks = new List<Task>();

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.Remove(cacheKey);
tasks.Add(provider.RemoveAsync(cacheKey));
}

await Task.WhenAll(tasks);
}

/// <summary>
Expand All @@ -351,12 +300,7 @@ public void Set<T>(string cacheKey, T cacheValue, TimeSpan expiration) where T :
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

var local = _providers.FirstOrDefault();

local.Set(cacheKey, cacheValue, expiration);

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.Set(cacheKey, cacheValue, expiration);
}
Expand All @@ -376,15 +320,14 @@ public async Task SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue));
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

var local = _providers.FirstOrDefault();

await local.SetAsync(cacheKey, cacheValue, expiration);
var tasks = new List<Task>();

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.Set(cacheKey, cacheValue, expiration);
tasks.Add(provider.SetAsync(cacheKey, cacheValue, expiration));
}

await Task.WhenAll(tasks);
}

/// <summary>
Expand Down Expand Up @@ -430,13 +373,8 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
public void RemoveByPrefix(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

var local = _providers.FirstOrDefault();

local.RemoveByPrefix(prefix);

//remote
foreach (var provider in _providers.Skip(1))

foreach (var provider in _providers)
{
provider.RemoveByPrefix(prefix);
}
Expand All @@ -451,15 +389,14 @@ public async Task RemoveByPrefixAsync(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

var local = _providers.FirstOrDefault();

await local.RemoveByPrefixAsync(prefix);
var tasks = new List<Task>();

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.RemoveByPrefix(prefix);
tasks.Add(provider.RemoveByPrefixAsync(prefix));
}

await Task.WhenAll(tasks);
}

/// <summary>
Expand All @@ -473,12 +410,7 @@ public void SetAll<T>(IDictionary<string, T> values, TimeSpan expiration) where
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));

var local = _providers.FirstOrDefault();

local.SetAll(values, expiration);

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.SetAll(values, expiration);
}
Expand All @@ -496,15 +428,14 @@ public async Task SetAllAsync<T>(IDictionary<string, T> values, TimeSpan expirat
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));

var local = _providers.FirstOrDefault();

await local.SetAllAsync(values, expiration);
var tasks = new List<Task>();

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.SetAll(values, expiration);
tasks.Add(provider.SetAllAsync(values, expiration));
}

await Task.WhenAll(tasks);
}

/// <summary>
Expand Down Expand Up @@ -655,12 +586,7 @@ public void RemoveAll(IEnumerable<string> cacheKeys)
{
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

var local = _providers.FirstOrDefault();

local.RemoveAll(cacheKeys);

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.RemoveAll(cacheKeys);
}
Expand All @@ -674,16 +600,15 @@ public void RemoveAll(IEnumerable<string> cacheKeys)
public async Task RemoveAllAsync(IEnumerable<string> cacheKeys)
{
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

var tasks = new List<Task>();

var local = _providers.FirstOrDefault();

await local.RemoveAllAsync(cacheKeys);

//remote
foreach (var provider in _providers.Skip(1))
foreach (var provider in _providers)
{
provider.RemoveAll(cacheKeys);
tasks.Add(provider.RemoveAllAsync(cacheKeys));
}

await Task.WhenAll(tasks);
}

/// <summary>
Expand Down

0 comments on commit a59e135

Please sign in to comment.