首先安装Redis,略。
项目NuGet包安装:StackExchange.Redis;
appsetting.json配置redis信息:
//Redis配置信息
"Redis": {
"Default": {
"Connection": "127.0.0.1:6379",
"InstanceName": "local",
"DefaultDB": 8
}
}
RedisHelper类
public class RedisHelper : IDisposable
{
//连接字符串
private string _connectionString;
//实例名称
private string _instanceName;
//默认数据库
private int _defaultDB;
private ConcurrentDictionary<string, ConnectionMultiplexer> _connections;
public RedisHelper(string connectionString, string instanceName, int defaultDB = 0)
{
_connectionString = connectionString;
_instanceName = instanceName;
_defaultDB = defaultDB;
_connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
}
/// <summary>
/// 获取ConnectionMultiplexer
/// </summary>
/// <returns></returns>
private ConnectionMultiplexer GetConnect()
{
return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString));
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="configName"></param>
/// <param name="db">默认为0:优先代码的db配置,其次config中的配置</param>
/// <returns></returns>
public IDatabase GetDatabase()
{
return GetConnect().GetDatabase(_defaultDB);
}
public IServer GetServer(string configName = null, int endPointsIndex = 0)
{
var confOption = ConfigurationOptions.Parse(_connectionString);
return GetConnect().GetServer(confOption.EndPoints[endPointsIndex]);
}
public ISubscriber GetSubscriber(string configName = null)
{
return GetConnect().GetSubscriber();
}
public void Dispose()
{
if (_connections != null && _connections.Count > 0)
{
foreach (var item in _connections.Values)
{
item.Close();
}
}
}
}
```csharp
Startup.cs中ConfigureServices配置:
#region 添加服务依赖
//redis缓存
var section = Configuration.GetSection("Redis:Default");
//连接字符串
string _connectionString = section.GetSection("Connection").Value;
//实例名称
string _instanceName = section.GetSection("InstanceName").Value;
//默认数据库
int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
services.AddSingleton(new RedisHelper(_connectionString, _instanceName, _defaultDB));
#endregion
测试:
/// <summary>
/// Redis缓存控制器
/// </summary>
[Produces("application/json")]
[Route("api/[controller]")]
public class RedisController : Controller
{
/// <summary>
/// IDatabase对象
/// </summary>
public readonly IDatabase _redis;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="redisHelper"></param>
public RedisController(RedisHelper redisHelper)
{
_redis = redisHelper.GetDatabase();
}
/// <summary>
/// 根据key获取值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[HttpGet]
public string Get(string key)
{
string result = string.Empty;
result = _redis.StringGet(key);
return result;
}
/// <summary>
/// 存入redis
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpPost]
public string Set(string key,string value)
{
_redis.StringSet(key,value);
return "OK";
}
}
``使用可视化管理工具查看:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210422115431330.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoZW5nUjY2Ng==,size_16,color_FFFFFF,t_70)