asp.net core上使用Redis探索(2)

时间:2023-03-08 15:47:09

在<<asp.net core上使用Redis探索(1)>>中,我介绍了一个微软官方实现Microsoft.Extensions.Caching.Redis的类库,这次,我们使用微软官方的Redis客户端。

使用Nuget引入Microsoft.Extensions.Caching.Redis库, 依赖项:
Microsoft.Extensions.Caching.Abstract
Microsoft.Extensions.Options
StackExchange.Redis.StrongName

原来Microsoft.Extensions.Caching.Redis其实就是封装了StackExchange.redis,作为.net core平台下的redis客户端与redis通信的。

源码地址:
https://github.com/aspnet/Caching/tree/dev/src/Microsoft.Extensions.Caching.Redis
源码分析:
RedisCache继承自IDistributeCache接口,该接口是Microsoft.Extensions.Caching.Distributed命名空间下的一个接口,主要就是封装了Redis的一些最基本的操作,比如,Set, Get, Refresh, Remove.就是最基本的增删改查。
RedisCache类中有一个用于同步的SemaphoreSlim类,该类是CLR中的同步遍历的混合构造——内核模式和用户模式的混合。该类除了显式实现了IDistributedCache的所有方法外,还有一个重要的方法要说下,那就是Connect()方法:

 private void Connect()
{
if (_cache != null)
{
return;
} _connectionLock.Wait();
try
{
if (_cache == null)
{
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
_cache = _connection.GetDatabase();
}
}
finally
{
_connectionLock.Release();
}
}

该方法用于连接Redis服务器,通过RedisCacheOptions这个类的属性,来连接redis数据库,源码:

 /// <summary>
/// Configuration options for <see cref="RedisCache"/>.
/// </summary>
public class RedisCacheOptions : IOptions<RedisCacheOptions>
{
/// <summary>
/// The configuration used to connect to Redis.
/// </summary>
public string Configuration { get; set; } /// <summary>
/// The Redis instance name.
/// </summary>
public string InstanceName { get; set; } RedisCacheOptions IOptions<RedisCacheOptions>.Value
{
get { return this; }
}
}

其中的Configuration属性就是我们的一般在.config文件中配置redis连接语句的地方,随后我们会讲到应用。而InstanceName是什么?继续看源码(RedisCache.cs文件):

private readonly RedisCacheOptions _options;
public RedisCache(IOptions<RedisCacheOptions> optionsAccessor)
{
if (optionsAccessor == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
} _options = optionsAccessor.Value; // This allows partitioning a single backend cache for use with multiple apps/services.
_instance = _options.InstanceName ?? string.Empty;
}

可以看到,这个属性是可以设置为空的,那么它到底是什么呢?这个就是我们在存储redis的时候的前缀了,我们可以这么这是Demo, 或者Demo:test等。
也就是说RedisCache类主要就是实现了IDistributedCache接口的所有方法,同时另外实现了Connect()方法用来连接redis,而这个的连接又是基于StackExchange.Redis(关于StackExchange.Redis请看  StackExchange.Redis通用封装类分享 )的ConnectionMultiplexer。但是我们在大型项目中使用的redis队列在RedisCache类中并没有实现,但是,要知道整个asp.net-core都是可拓展的,我们可以基于RedisCache类再实现一个pub/sub方法用来做消息队列。
最后使用asp.net-core默认的DI容器将RedisCache类注册到框架中。RedisCacheServiceCollectionExtensions类源码:

 /// <summary>
/// Extension methods for setting up Redis distributed cache related services in an <see cref="IServiceCollection" />.
/// </summary>
public static class RedisCacheServiceCollectionExtensions
{
/// <summary>
/// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{RedisCacheOptions}"/> to configure the provided
/// <see cref="RedisCacheOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddDistributedRedisCache(this IServiceCollection services, Action<RedisCacheOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
} services.AddOptions();
services.Configure(setupAction);
// .net core DI容器的使用无非就是在容器中实例化接口,而接口的的实例化,是通过实例化接口的派生类(即以接口为父类的子类)...
services.Add(ServiceDescriptor.Singleton<IDistributedCache, RedisCache>()); return services;
}
}

在这里我们基于IServiceCollection接口拓展出来了一个方法,该方法就是微软官方为我们创建的基于Redis的实现了,在最后使用DI的三种方法的Singleton来实现IOC。那么我们怎么来使用呢?
引入完了Microsoft.Extensions.Caching.Redis之后,在Startup类中:

 services.AddDistributedRedisCache(option =>
{
option.Configuration = "123.207.96.138:6379, password=*****";
option.InstanceName = "";
});

个人的建议是给Redis设置一个密码,要不然容易被攻击。
接下来就是给Redis设置值了, 我们新建一个WebApi的程序,然后新建一个HomeController Api控制器

 [Produces("application/json")]
[Route("api/[controller]")]
public class HomeController : Controller
{
// 通过构造函数注入,内置IOC容器实例化了之后,就可以通过接口对象来调用相应的函数了.
private readonly IDistributedCache _distributedCache; public HomeController(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
} [HttpGet]
public async Task<string> Get()
{ var cacheKey = "TheTime"; //await _distributedCache.RemoveAsync(cacheKey); var existingTime = _distributedCache.GetString(cacheKey);
if (!string.IsNullOrEmpty(existingTime))
{
return "Fetched from cache : " + existingTime;
}
else
{
existingTime = DateTime.UtcNow.ToString(); _distributedCache.SetString(cacheKey, existingTime);
return "Added to cache : " + existingTime;
}
}
}

代码我就不运行了,亲测能用。

百度云盘demo下载地址:

链接:https://pan.baidu.com/s/1vzuS5pEk_Ouh9I-NkPWXfg 密码:armw