|
@@ -11,6 +11,10 @@ using StackExchange.Redis;
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
{
|
|
{
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Distributed cache implementation using Redis.
|
|
|
|
|
+ /// <para>Uses <c>StackExchange.Redis</c> as the Redis client.</para>
|
|
|
|
|
+ /// </summary>
|
|
|
public class RedisCache : IDistributedCache, IDisposable
|
|
public class RedisCache : IDistributedCache, IDisposable
|
|
|
{
|
|
{
|
|
|
// KEYS[1] = = key
|
|
// KEYS[1] = = key
|
|
@@ -39,6 +43,10 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
|
|
|
|
|
private readonly SemaphoreSlim _connectionLock = new SemaphoreSlim(initialCount: 1, maxCount: 1);
|
|
private readonly SemaphoreSlim _connectionLock = new SemaphoreSlim(initialCount: 1, maxCount: 1);
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Initializes a new instance of <see cref="RedisCache"/>.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="optionsAccessor">The configuration options.</param>
|
|
|
public RedisCache(IOptions<RedisCacheOptions> optionsAccessor)
|
|
public RedisCache(IOptions<RedisCacheOptions> optionsAccessor)
|
|
|
{
|
|
{
|
|
|
if (optionsAccessor == null)
|
|
if (optionsAccessor == null)
|
|
@@ -52,6 +60,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
_instance = _options.InstanceName ?? string.Empty;
|
|
_instance = _options.InstanceName ?? string.Empty;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public byte[] Get(string key)
|
|
public byte[] Get(string key)
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -62,6 +71,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
return GetAndRefresh(key, getData: true);
|
|
return GetAndRefresh(key, getData: true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public async Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
|
|
public async Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -74,6 +84,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
return await GetAndRefreshAsync(key, getData: true, token: token).ConfigureAwait(false);
|
|
return await GetAndRefreshAsync(key, getData: true, token: token).ConfigureAwait(false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
|
|
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -107,6 +118,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
|
|
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -142,6 +154,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
}).ConfigureAwait(false);
|
|
}).ConfigureAwait(false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void Refresh(string key)
|
|
public void Refresh(string key)
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -152,6 +165,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
GetAndRefresh(key, getData: false);
|
|
GetAndRefresh(key, getData: false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
|
|
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -301,6 +315,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void Remove(string key)
|
|
public void Remove(string key)
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -314,6 +329,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
// TODO: Error handling
|
|
// TODO: Error handling
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
|
|
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
|
|
|
{
|
|
{
|
|
|
if (key == null)
|
|
if (key == null)
|
|
@@ -432,6 +448,7 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis
|
|
|
return options.AbsoluteExpiration;
|
|
return options.AbsoluteExpiration;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void Dispose()
|
|
public void Dispose()
|
|
|
{
|
|
{
|
|
|
if (_disposed)
|
|
if (_disposed)
|