.net 6 简单使用redis
前言:要使用redis,首先电脑要先下载安装redis,下载安装可以看我上一篇文章 https://blog.csdn.net/weixin_44442366/article/details/124205310我们用StackExchange.Redis包在.NET Core中使用Redis实现缓存,如果想可视化查看redis缓存里面的数据可以安装RedisDesktopManager。一、在Nu
·
前言:要使用redis,首先电脑要先下载安装redis,下载安装可以看我上一篇文章 https://blog.csdn.net/weixin_44442366/article/details/124205310
我们用StackExchange.Redis包在.NET Core中使用Redis实现缓存,如果想可视化查看redis缓存里面的数据可以安装RedisDesktopManager。
一、在NuGet搜索安装StackExchange.Redis包
二、在appsettings.json文件里面添加Redis相关配置信息:
"Redis": {
"Default": {
"Connection": "127.0.0.1:6379,password=888", //redis连接地址,端口号,密码
"InstanceName": "local", //实例名
"DefaultDB": "8", //Db8数据库
}
三、创建Redis帮助类
创建RedisHelper类,代码如下:
using StackExchange.Redis;
using System.Collections.Concurrent;
namespace RedisDemo.redis
{
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();
}
}
}
}
}
四、添加服务依赖项
在Program.cs类添加服务依赖
//redis缓存
var section =builder.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");
builder.Services.AddSingleton(new RedisHelper(_connectionString, _instanceName, _defaultDB));
五、测试使用, 在控制器接口中使用
using Microsoft.AspNetCore.Mvc;
using RedisDemo.redis;
using StackExchange.Redis;
namespace RedisDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RedisTestController : ControllerBase
{
private readonly IDatabase _redis;
public RedisTestController(RedisHelper client)
{
_redis = client.GetDatabase();
}
[HttpGet("test")]
public string Get()
{
// 往Redis里面存入数据
_redis.StringSet("Name", "hehe");
// 从Redis里面取数据
string name = _redis.StringGet("Name");
return name;
}
}
}
api测试返回成功
用命令查看redis数据库正常存入,select 8 进入第8个库,keys *: 列出所有key,get Name:查看Name键的值
用可视化工具RedisDesktopManager查看
更多推荐
已为社区贡献6条内容
所有评论(0)