Skip to content

Commit

Permalink
🎨 Improving structure / format of the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Apr 15, 2018
1 parent 8bb6ac0 commit 81d82c0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 101 deletions.
15 changes: 15 additions & 0 deletions src/EasyCaching.Core/CachingProviderType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace EasyCaching.Core
{
/// <summary>
/// Caching provider type.
/// </summary>
public enum CachingProviderType
{
InMemory,
Memcached,
Redis,
SQLite,
Ext1,
Ext2
}
}
28 changes: 0 additions & 28 deletions src/EasyCaching.Core/EasyCachingType.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/EasyCaching.Core/IEasyCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,6 @@ public interface IEasyCachingProvider
/// Gets the type of the caching provider.
/// </summary>
/// <value>The type of the caching provider.</value>
Internal.CachingProviderType CachingProviderType { get; }
CachingProviderType CachingProviderType { get; }
}
}
13 changes: 1 addition & 12 deletions src/EasyCaching.Core/Internal/BaseProviderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,5 @@ public class BaseProviderOptions
public int Order { get; set; }
}

/// <summary>
/// Caching provider type.
/// </summary>
public enum CachingProviderType
{
InMemory,
Memcached,
Redis,
SQLite,
Ext1,
Ext2
}

}
18 changes: 0 additions & 18 deletions src/EasyCaching.Core/Internal/HybridCachingKeyType.cs

This file was deleted.

96 changes: 54 additions & 42 deletions src/EasyCaching.HybridCache/HybridCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,60 @@
/// Hybrid caching provider.
/// </summary>
public class HybridCachingProvider : IHybridCachingProvider
{
{
/// <summary>
/// The caching providers.
/// </summary>
private readonly IEnumerable<IEasyCachingProvider> _providers;

/// <summary>
/// Gets a value indicating whether this <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> is
/// distributed cache.
/// </summary>
/// <value><c>true</c> if is distributed cache; otherwise, <c>false</c>.</value>
public bool IsDistributedCache => throw new NotImplementedException();

/// <summary>
/// Gets the order.
/// </summary>
/// <value>The order.</value>
public int Order => throw new NotImplementedException();

/// <summary>
/// Gets the max rd second.
/// </summary>
/// <value>The max rd second.</value>
public int MaxRdSecond => throw new NotImplementedException();

/// <summary>
/// Gets the type of the caching provider.
/// </summary>
/// <value>The type of the caching provider.</value>
public CachingProviderType CachingProviderType => throw new NotImplementedException();

/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
/// </summary>
/// <param name="providers">Providers.</param>
public HybridCachingProvider(IEnumerable<IEasyCachingProvider> providers)
{
if(providers == null || !providers.Any())
if (providers == null || !providers.Any())
{
throw new ArgumentNullException(nameof(providers));
}

//2-level and 3-level are enough for hybrid
if(providers.Count() > 3)
if (providers.Count() > 3)
{
throw new ArgumentOutOfRangeException(nameof(providers));
throw new ArgumentOutOfRangeException(nameof(providers));
}

//
this._providers = providers.OrderBy(x=>x.Order);
this._providers = providers.OrderBy(x => x.Order);

//TODO: local cache should subscribe the remote cache
}

/// <summary>
/// Gets a value indicating whether this <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> is
/// distributed cache.
/// </summary>
/// <value><c>true</c> if is distributed cache; otherwise, <c>false</c>.</value>
public bool IsDistributedCache => true;

public int Order => throw new NotImplementedException();

public int MaxRdSecond => throw new NotImplementedException();

public CachingProviderType CachingProviderType => throw new NotImplementedException();

/// <summary>
/// Exists the specified cacheKey.
/// </summary>
Expand All @@ -68,16 +80,16 @@ public bool Exists(string cacheKey)

flag = local.Exists(cacheKey);

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

if (flag) break;
}
}
}
}

return flag;
}
Expand All @@ -97,17 +109,17 @@ public async Task<bool> ExistsAsync(string cacheKey)

flag = await local.ExistsAsync(cacheKey);

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

if (flag) break;
}
}
}

return flag;
}

Expand All @@ -126,9 +138,9 @@ public CacheValue<T> Get<T>(string cacheKey, Func<T> dataRetriever, TimeSpan exp

var local = _providers.FirstOrDefault();

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

if(cachedValue.HasValue)
if (cachedValue.HasValue)
{
return cachedValue;
}
Expand Down Expand Up @@ -282,7 +294,7 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class
return CacheValue<T>.NoValue;
}

return cachedValue;
return cachedValue;
}

/// <summary>
Expand Down Expand Up @@ -347,7 +359,7 @@ public void Set<T>(string cacheKey, T cacheValue, TimeSpan expiration) where T :
foreach (var provider in _providers.Skip(1))
{
provider.Set(cacheKey, cacheValue, expiration);
}
}
}

/// <summary>
Expand All @@ -372,7 +384,7 @@ public async Task SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration
foreach (var provider in _providers.Skip(1))
{
provider.Set(cacheKey, cacheValue, expiration);
}
}
}

/// <summary>
Expand Down Expand Up @@ -427,7 +439,7 @@ public void RemoveByPrefix(string prefix)
foreach (var provider in _providers.Skip(1))
{
provider.RemoveByPrefix(prefix);
}
}
}

/// <summary>
Expand All @@ -447,7 +459,7 @@ public async Task RemoveByPrefixAsync(string prefix)
foreach (var provider in _providers.Skip(1))
{
provider.RemoveByPrefix(prefix);
}
}
}

/// <summary>
Expand All @@ -469,7 +481,7 @@ public void SetAll<T>(IDictionary<string, T> values, TimeSpan expiration) where
foreach (var provider in _providers.Skip(1))
{
provider.SetAll(values, expiration);
}
}
}

/// <summary>
Expand All @@ -492,7 +504,7 @@ public async Task SetAllAsync<T>(IDictionary<string, T> values, TimeSpan expirat
foreach (var provider in _providers.Skip(1))
{
provider.SetAll(values, expiration);
}
}
}

/// <summary>
Expand Down Expand Up @@ -527,7 +539,7 @@ public IDictionary<string, CacheValue<T>> GetAll<T>(IEnumerable<string> cacheKey
localDict.Concat(disDict).ToDictionary(k => k.Key, v => v.Value);
}

return localDict;
return localDict;
}

/// <summary>
Expand Down Expand Up @@ -562,7 +574,7 @@ public async Task<IDictionary<string, CacheValue<T>>> GetAllAsync<T>(IEnumerable
localDict.Concat(disDict).ToDictionary(k => k.Key, v => v.Value);
}

return localDict;
return localDict;
}

/// <summary>
Expand All @@ -586,7 +598,7 @@ public IDictionary<string, CacheValue<T>> GetByPrefix<T>(string prefix) where T
{
return localDict;
}

foreach (var item in localNotFindKeys)
localDict.Remove(item);

Expand All @@ -597,7 +609,7 @@ public IDictionary<string, CacheValue<T>> GetByPrefix<T>(string prefix) where T
localDict.Concat(disDict).ToDictionary(k => k.Key, v => v.Value);
}

return localDict;
return localDict;
}

/// <summary>
Expand Down Expand Up @@ -641,12 +653,12 @@ public async Task<IDictionary<string, CacheValue<T>>> GetByPrefixAsync<T>(string
/// <param name="cacheKeys">Cache keys.</param>
public void RemoveAll(IEnumerable<string> cacheKeys)
{
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

var local = _providers.FirstOrDefault();

local.RemoveAll(cacheKeys);

//remote
foreach (var provider in _providers.Skip(1))
{
Expand Down Expand Up @@ -715,7 +727,7 @@ public async Task FlushAsync()
tasks.Add(provider.FlushAsync());
}

await Task.WhenAll(tasks);
await Task.WhenAll(tasks);
}
}
}
1 change: 1 addition & 0 deletions src/EasyCaching.InMemory/InMemoryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EasyCaching.InMemory
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;

public class InMemoryOptions : BaseProviderOptions
Expand Down
1 change: 1 addition & 0 deletions src/EasyCaching.Memcached/MemcachedOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EasyCaching.Memcached
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;

public class MemcachedOptions : BaseProviderOptions
Expand Down
1 change: 1 addition & 0 deletions src/EasyCaching.Redis/RedisOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EasyCaching.Redis
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;

public class RedisOptions: BaseProviderOptions
Expand Down
1 change: 1 addition & 0 deletions src/EasyCaching.SQLite/SQLiteOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EasyCaching.SQLite
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;

public class SQLiteOptions: BaseProviderOptions
Expand Down

0 comments on commit 81d82c0

Please sign in to comment.