Skip to content

Commit cf66ea5

Browse files
Merge pull request #469 from johelvisguzman/issue465
Added a generic caching provider interface
2 parents cb53f01 + a4a5844 commit cf66ea5

File tree

9 files changed

+106
-56
lines changed

9 files changed

+106
-56
lines changed

src/DotNetToolkit.Repository.Caching.InMemory/InMemoryCache.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,17 @@ public int Increment([NotNull] string key, int defaultValue, int incrementValue)
120120
}
121121

122122
#endregion
123+
124+
#region Implementation of ICache
125+
126+
/// <summary>
127+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
128+
/// </summary>
129+
public void Dispose()
130+
{
131+
_cache.Dispose();
132+
}
133+
134+
#endregion
123135
}
124136
}

src/DotNetToolkit.Repository.Caching.InMemory/InMemoryCacheProvider.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
using Utility;
88

99
/// <summary>
10-
/// An implementation of <see cref="ICacheProvider" />.
10+
/// An implementation of <see cref="ICacheProvider{TCache}" />.
1111
/// </summary>
12-
public class InMemoryCacheProvider : ICacheProvider
12+
public class InMemoryCacheProvider : ICacheProvider<InMemoryCache>
1313
{
1414
#region Constructors
1515

@@ -49,11 +49,23 @@ public InMemoryCacheProvider([NotNull] IMemoryCache cache, [CanBeNull] TimeSpan?
4949
/// Gets or sets the caching expiration time.
5050
/// </summary>
5151
public TimeSpan? Expiry { get; set; }
52+
53+
/// <summary>
54+
/// Gets the cache.
55+
/// </summary>
56+
ICache ICacheProvider.Cache
57+
{
58+
get { return Cache; }
59+
}
60+
61+
#endregion
62+
63+
#region Implementation of ICacheProvider<TCache>
5264

5365
/// <summary>
5466
/// Gets the cache.
5567
/// </summary>
56-
public ICache Cache { get; }
68+
public InMemoryCache Cache { get; }
5769

5870
#endregion
5971
}

src/DotNetToolkit.Repository.Caching.Redis/RedisCache.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public class RedisCache : ICache
2626
/// </summary>
2727
public ConnectionMultiplexer Connection { get { return _lazyConnection.Value; } }
2828

29+
/// <summary>
30+
/// Gets the redis server.
31+
/// </summary>
32+
public IServer Server
33+
{
34+
get { return Connection.GetServer(Connection.GetEndPoints()[0]); }
35+
}
36+
2937
/// <summary>
3038
/// Gets the redis database.
3139
/// </summary>
@@ -243,5 +251,23 @@ public int Increment([NotNull] string key, int defaultValue, int incrementValue)
243251
}
244252

245253
#endregion
254+
255+
#region Implementation of ICache
256+
257+
/// <summary>
258+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
259+
/// </summary>
260+
public void Dispose()
261+
{
262+
if (_lazyConnection.IsValueCreated)
263+
{
264+
var conn = _lazyConnection.Value;
265+
266+
conn.Close(false);
267+
conn.Dispose();
268+
}
269+
}
270+
271+
#endregion
246272
}
247273
}

src/DotNetToolkit.Repository.Caching.Redis/RedisCacheProvider.cs

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,10 @@
77
using Utility;
88

99
/// <summary>
10-
/// An implementation of <see cref="ICacheProvider" />.
10+
/// An implementation of <see cref="ICacheProvider{TCache}" />.
1111
/// </summary>
12-
public class RedisCacheProvider : ICacheProvider
12+
public class RedisCacheProvider : ICacheProvider<RedisCache>
1313
{
14-
#region Fields
15-
16-
private readonly RedisCache _redis;
17-
18-
#endregion
19-
20-
#region Properties
21-
22-
/// <summary>
23-
/// Gets the redis connection multiplexer.
24-
/// </summary>
25-
public ConnectionMultiplexer Connection
26-
{
27-
get { return _redis.Connection; }
28-
}
29-
30-
/// <summary>
31-
/// Gets the redis server.
32-
/// </summary>
33-
public IServer Server
34-
{
35-
get { return Connection.GetServer(Connection.GetEndPoints()[0]); }
36-
}
37-
38-
#endregion
39-
4014
#region Constructors
4115

4216
/// <summary>
@@ -52,8 +26,7 @@ public RedisCacheProvider() : this((TimeSpan?)null) { }
5226
/// <remarks>This will connect to a single server on the local machine using the default redis port (6379).</remarks>
5327
public RedisCacheProvider([CanBeNull] TimeSpan? expiry)
5428
{
55-
_redis = new RedisCache();
56-
29+
Cache = new RedisCache();
5730
Expiry = expiry;
5831
}
5932

@@ -66,8 +39,7 @@ public RedisCacheProvider([CanBeNull] TimeSpan? expiry)
6639
/// <remarks>This will connect to a single server on the local machine using the default redis port (6379).</remarks>
6740
public RedisCacheProvider(bool allowAdmin, [CanBeNull] int? defaultDatabase, [CanBeNull] TimeSpan? expiry)
6841
{
69-
_redis = new RedisCache(allowAdmin, defaultDatabase);
70-
42+
Cache = new RedisCache(allowAdmin, defaultDatabase);
7143
Expiry = expiry;
7244
}
7345

@@ -78,10 +50,7 @@ public RedisCacheProvider(bool allowAdmin, [CanBeNull] int? defaultDatabase, [Ca
7850
/// <param name="expiry">The the caching expiration time.</param>
7951
public RedisCacheProvider([NotNull] string configuration, [CanBeNull] TimeSpan? expiry)
8052
{
81-
Guard.NotNull(configuration, nameof(configuration));
82-
83-
_redis = new RedisCache(configuration);
84-
53+
Cache = new RedisCache(Guard.NotNull(configuration, nameof(configuration)));
8554
Expiry = expiry;
8655
}
8756

@@ -92,10 +61,7 @@ public RedisCacheProvider([NotNull] string configuration, [CanBeNull] TimeSpan?
9261
/// <param name="expiry">The the caching expiration time.</param>
9362
public RedisCacheProvider([NotNull] ConfigurationOptions options, [CanBeNull] TimeSpan? expiry)
9463
{
95-
Guard.NotNull(options, nameof(options));
96-
97-
_redis = new RedisCache(options);
98-
64+
Cache = new RedisCache(Guard.NotNull(options, nameof(options)));
9965
Expiry = expiry;
10066
}
10167

@@ -111,8 +77,7 @@ public RedisCacheProvider([NotNull] string host, bool ssl, bool allowAdmin, [Can
11177
{
11278
Guard.NotEmpty(host, nameof(host));
11379

114-
_redis = new RedisCache(host, ssl, allowAdmin, defaultDatabase);
115-
80+
Cache = new RedisCache(host, ssl, allowAdmin, defaultDatabase);
11681
Expiry = expiry;
11782
}
11883

@@ -131,8 +96,7 @@ public RedisCacheProvider([NotNull] string host, int port, [NotNull] string pass
13196
Guard.NotEmpty(host, nameof(host));
13297
Guard.NotEmpty(password, nameof(password));
13398

134-
_redis = new RedisCache(host, port, password, ssl, allowAdmin, defaultDatabase);
135-
99+
Cache = new RedisCache(host, port, password, ssl, allowAdmin, defaultDatabase);
136100
Expiry = expiry;
137101
}
138102

@@ -148,7 +112,19 @@ public RedisCacheProvider([NotNull] string host, int port, [NotNull] string pass
148112
/// <summary>
149113
/// Gets the cache.
150114
/// </summary>
151-
public ICache Cache { get { return _redis; } }
115+
ICache ICacheProvider.Cache
116+
{
117+
get { return Cache; }
118+
}
119+
120+
#endregion
121+
122+
#region Implementation of ICacheProvider<TCache>
123+
124+
/// <summary>
125+
/// Gets the cache.
126+
/// </summary>
127+
public RedisCache Cache { get; }
152128

153129
#endregion
154130
}

src/DotNetToolkit.Repository/Configuration/Caching/ICache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/// <summary>
66
/// Represents an interface for caching query data within the repositories.
77
/// </summary>
8-
public interface ICache
8+
/// <seealso cref="IDisposable" />
9+
public interface ICache : IDisposable
910
{
1011
/// <summary>
1112
/// Create or overwrite an entry in the cache.

src/DotNetToolkit.Repository/Configuration/Caching/ICacheProvider.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ public interface ICacheProvider
1717
/// </summary>
1818
ICache Cache { get; }
1919
}
20+
21+
/// <summary>
22+
/// Represents a caching provider for caching query data within the repositories.
23+
/// </summary>
24+
/// <typeparam name="TCache">The type of the cache.</typeparam>
25+
/// <seealso cref="ICacheProvider" />
26+
public interface ICacheProvider<out TCache> : ICacheProvider where TCache : ICache
27+
{
28+
/// <summary>
29+
/// Gets the cache.
30+
/// </summary>
31+
new TCache Cache { get; }
32+
}
2033
}

src/DotNetToolkit.Repository/Configuration/Caching/Internal/NullCache.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public int Increment(string key, int defaultValue, int incrementValue)
2626
{
2727
return 1;
2828
}
29+
30+
public void Dispose() { }
2931
}
3032
}

src/DotNetToolkit.Repository/Configuration/Caching/Internal/NullCacheProvider.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
using System;
44

55
/// <summary>
6-
/// An implementation of <see cref="ICacheProvider" />.
6+
/// An implementation of <see cref="ICacheProvider{TCache}" />.
77
/// </summary>
8-
internal class NullCacheProvider : ICacheProvider
8+
internal class NullCacheProvider : ICacheProvider<NullCache>
99
{
1010
internal static NullCacheProvider Instance { get; } = new NullCacheProvider();
1111

12-
private NullCacheProvider() { }
12+
private NullCacheProvider()
13+
{
14+
Cache = NullCache.Instance;
15+
}
1316

1417
public TimeSpan? Expiry { get; set; }
1518

16-
public ICache Cache { get; } = NullCache.Instance;
19+
ICache ICacheProvider.Cache
20+
{
21+
get { return Cache; }
22+
}
23+
24+
public NullCache Cache { get; }
1725
}
1826
}

test/DotNetToolkit.Repository.Integration.Test/Data/TestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ private static void ApplyCachingProvider(CachingProviderType cachingProvider, Re
248248
}
249249
case CachingProviderType.Redis:
250250
{
251-
var redis = new RedisCacheProvider(allowAdmin: true, defaultDatabase: 0, expiry: null);
251+
var provider = new RedisCacheProvider(allowAdmin: true, defaultDatabase: 0, expiry: null);
252252

253-
redis.Server.FlushAllDatabases();
253+
provider.Cache.Server.FlushAllDatabases();
254254

255-
builder.UseCachingProvider(redis);
255+
builder.UseCachingProvider(provider);
256256

257257
break;
258258
}

0 commit comments

Comments
 (0)