40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using StackExchange.Redis;
|
|
|
|
namespace Bunny.Common.Context;
|
|
|
|
public static class RedisContext
|
|
{
|
|
public static IDatabase? RedisDatabase;
|
|
private static readonly EndPointCollection EndPointCollection = new();
|
|
|
|
public static void AddRedisContext(this WebApplicationBuilder builder)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
// 获取端口等配置信息
|
|
var host = AppSettings.GetConfig("Redis:Host");
|
|
var port = Convert.ToInt32(AppSettings.GetConfig("Redis:Port"));
|
|
var defaultDb = Convert.ToInt32(AppSettings.GetConfig("Redis:DefaultDB"));
|
|
var password = AppSettings.Get<string>("Redis:Password");
|
|
var timeout = Convert.ToInt32(AppSettings.GetConfig("Redis:AsyncTimeout"));
|
|
|
|
// 添加连接地址
|
|
EndPointCollection.Add(host, port);
|
|
|
|
// 初始化连接对象
|
|
var connect = ConnectionMultiplexer.Connect(new ConfigurationOptions
|
|
{
|
|
EndPoints = EndPointCollection,
|
|
Password = password,
|
|
DefaultDatabase = defaultDb,
|
|
AsyncTimeout = timeout
|
|
});
|
|
|
|
// 创建连接对象
|
|
RedisDatabase = connect.GetDatabase();
|
|
|
|
Console.WriteLine("Redis 初始化...");
|
|
});
|
|
}
|
|
} |