live-forum/server/webapi/LiveForum/LiveForum.Code/Redis/RedisDatabaseProvider.cs
2026-03-24 11:27:37 +08:00

39 lines
1.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LiveForum.Code.Redis
{
public interface IRedisDatabaseProvider
{
IDatabase GetDatabase();
}
public class RedisDatabaseProvider : IRedisDatabaseProvider
{
private readonly ConnectionMultiplexer _redis;
// 可以通过构造函数传入连接字符串
public RedisDatabaseProvider(string redisConnectionString)
{
_redis = ConnectionMultiplexer.Connect(redisConnectionString);
}
// 也可以注入已经创建好的 ConnectionMultiplexer更灵活
public RedisDatabaseProvider(ConnectionMultiplexer redis)
{
_redis = redis ?? throw new ArgumentNullException(nameof(redis));
}
public IDatabase GetDatabase()
{
// 默认数据库是 db0你也可以指定其它 db比如 GetDatabase(1)
return _redis.GetDatabase();
}
}
}