在.Net中操作redis
一、环境
- .Net 7
- redis 7.2.4
二、所需类包
- StackExchange.Redis
三、连接redis信息
appsettings.json
配置redis连接信息
// Redis配置
//"Redis": {
// "Default": {
// "Connection": "", // 连接地址,端口号,密码
// "InstanceName": "", // 实例名
// "DefaultDB": "1" // 数据库
// }
//},
"Redis": {
"Host": "120.xxx.xx.xx", // ip地址
"Port": "6379", // 端口
"password": "123456", // redis密码
"InstanceName": "local", // 实例名称
"DefaultDb": 1, // 连接的库
"maxWritePoolSize": 60,
"maxReadPoolSize": 60,
"autoStart": true,
"ConnectTimeout": 60, // 超时
"KeepAlive": 5, // 保存x秒的活动连接
"recordeLog": false,
"preName": "test_" // key的前缀
},
四、实例
using AspNetFrame.Domain.Entity.User;
using AspNetFrame.Domain.Service;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using StackExchange.Redis;
using System.Security.Policy;
using System;
namespace AspNetFrame.Api.Controller.Redis
{
[ApiController]
[Route("[controller]/[action]")]
public class RedisCnnTestController : ControllerBase
{
private readonly IConfiguration _configuration;
private IDatabase cache;
private ConnectionMultiplexer connection;
public RedisCnnTestController(IConfiguration configuration)
{
_configuration = configuration;
string redisHost = _configuration["Redis:host"]; // 连接的url
int redisPort = int.Parse(_configuration["Redis:port"]); // 端口
string Password =_configuration["Redis:Password"]; // 连接密码
int DefaultDatabase = int.Parse(_configuration["Redis:DefaultDb"]); // 数据库
int ConnectTimeout = int.Parse(_configuration["Redis:ConnectTimeout"]); // 超时时间
int KeepAlive = int.Parse(_configuration["Redis:KeepAlive"]); // 保存x秒的活动连接
// 连接配置
var configurationOptions = new ConfigurationOptions
{
EndPoints =
{
{ redisHost, redisPort },
},
DefaultDatabase = DefaultDatabase,
KeepAlive = KeepAlive, // 保存x秒的活动连接
Password = Password, // 连接密码
ConnectTimeout = ConnectTimeout, //超时时间
AllowAdmin = true,
};
connection = ConnectionMultiplexer.Connect(configurationOptions);
cache = connection.GetDatabase();
}
public JsonResult Test()
{
//string redisHost = _configuration["Redis:host"]; // 连接的url
//int redisPort = int.Parse(_configuration["Redis:port"]); // 端口
//string pwd = _configuration["Redis:Password"];
cache.StringSet("test", "hello");
string test = cache.StringGet("test");
JsonResult json = new JsonResult(new
{
code = 200,
msg = test
});
return json;
}
[HttpPost]
public JsonResult Test2()
{
LoginUser loginUser = new LoginUser()
{
UserName = "admin",
Password = "admin",
AuthorityId = 1,
};
LoginUser loginUser2 = new LoginUser()
{
UserName = "admin2",
Password = "admin2",
AuthorityId = 1,
};
List<LoginUser> loginUsers = new List<LoginUser>() { loginUser, loginUser2 };
cache.StringSet("loginUserList", JsonConvert.SerializeObject(loginUsers), TimeSpan.FromMinutes(10)); // 过期时间为10分钟
cache.StringSet("loginUser_admin", JsonConvert.SerializeObject(loginUser), TimeSpan.FromMinutes(10));
cache.StringSet("test", "hello");
LoginUser redisLoginUser = JsonConvert.DeserializeObject<LoginUser>(cache.StringGet("loginUser_admin"));
string test = cache.StringGet("test");
JsonResult json = new JsonResult(new
{
code = 200,
msg = test,
result= redisLoginUser
});
return json;
}
}
}
五、调用api结果
可视化工具:Another Redis Desktop Manager
redis中数据
这样就把list数据存入了redis中
标签:int,Redis,cache,redis,test,操作,Net,configuration From: https://www.cnblogs.com/zbfoot/p/18108840