1、Redis简介
Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。
2、Redis主要特点
Redis支持数据的持久化 Redis支持多种数据结构 Redis支持数据的备份
3、Redis的下载安装
windows版本Redis下载链接:https://github.com/tporadowski/redis/releases
选择.zip免安装版本可直接通过命令使用。
![](https://img-blog.csdnimg.cn/17c2e0e1afa84040b74f1d1bf292ba5c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_20,color_FFFFFF,t_70,g_se,x_16)
在Windows终端(可直接在解压文件夹右键选择[在Windows终端打开])。
![](https://img-blog.csdnimg.cn/027684dfe14349729bfcbd827db60a54.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_17,color_FFFFFF,t_70,g_se,x_16)
或者cmd控制台进入到Redis的解压文件夹运行指令![](https://img-blog.csdnimg.cn/cfe8f4d3cec1453d913532a9f4638ee2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_18,color_FFFFFF,t_70,g_se,x_16)
4、Redis的常用指令
安装服务:redis-server --service-install
卸载服务:redis-server --service-uninstall
开启服务:redis-server --service-start
停止服务:redis-server --service-stop
服务端启动时重命名:redis-server --service-start --service-name Redis1
启动客户端:redis-cli
推出客户端:quit
1)启动Redis服务
![](https://img-blog.csdnimg.cn/069f5aa1f77c450a8c8c8bc14ce349f6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_20,color_FFFFFF,t_70,g_se,x_16)
2)安装Redis服务为本地服务
![](https://img-blog.csdnimg.cn/7f3b9fad25c94368a429d0b42309236a.png)
3)安装完成,可以设置为自启动也可以手动启动
5、Redis在.NET Core的简单使用
1)新建ASP.NET Core Web API项目
打开NuGet包管理器安装StackExchange.Redis(注:本demo为VS2022新建,默认提供的天气预报的类和控制器)
![](https://img-blog.csdnimg.cn/255e486c853d4942930c012739bf719d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_20,color_FFFFFF,t_70,g_se,x_16)
2)新建Redis帮助类
///
/// Redis帮助类
///
public class RedisHelper
{
public ConnectionMultiplexer redis { get; set; }
public IDatabase db { get; set; }
public RedisHelper(string connection)
{
redis = ConnectionMultiplexer.Connect(connection);
db = redis.GetDatabase();
}
///
/// 增加/修改
///
///
///
///
public bool SetValue(string key, string value)
{
return db.StringSet(key, value);
}
///
/// 查询
///
///
///
public string GetValue(string key)
{
return db.StringGet(key);
}
///
/// 删除
///
///
///
public bool DeleteKey(string key)
{
return db.KeyDelete(key);
}
}
3)新建返回结果类
///
/// 返回结果类
///
public class Result
{
///
/// 执行结果
///
public bool ImplementationResults { get; set; }
///
/// Value的实时值
///
public string Value { get; set; }
///
/// 缓存的Value值
///
public string CacheValue { get; set; }
///
/// 错误信息
///
public string Error { get; set; }
}
4)控制器类
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
{
public WeatherForecastController()
{
}
// 连接Redis客户端
RedisHelper redisHelper = new RedisHelper("127.0.0.1:6379");
string value = "this is a test redis string";
Result result = null;
///
/// 查询
///
/// key
///
[HttpGet, HttpPost]
public string Search(string key)
{
string returnStr = "";
if (!string.IsNullOrWhiteSpace(key))
{
string value = redisHelper.GetValue(key);
if (!string.IsNullOrWhiteSpace(value))
returnStr = value;
else
returnStr = "key的值不存在!";
} else
returnStr = "key的值不能为空!";
return returnStr;
}
///
/// 新增
///
/// key
///
[HttpGet, HttpPost]
public Result Insert(string key)
{
result = new Result();
if (!string.IsNullOrWhiteSpace(key))
{
bool isInsertSuccess = redisHelper.SetValue(key, value);
result.ImplementationResults = isInsertSuccess;
if (isInsertSuccess)
{
// 查询mytestkey的实时值
var info = Search("mytestkey");
if (!string.IsNullOrWhiteSpace(info))
result.Value = info;
}
} else
result.Error = "key的值不能为空!";
return result;
}
///
/// 修改
///
/// key
/// 新的value值
///
[HttpGet, HttpPost]
public Result Update(string key, string newValue)
{
result = new Result();
if (!string.IsNullOrWhiteSpace(key))
{
bool isUpdateSuccess = redisHelper.SetValue(key, newValue);
result.ImplementationResults = isUpdateSuccess;
// 查询mytestkey的实时值
var info = Search(key);
if (!string.IsNullOrWhiteSpace(info))
result.Value = info;
} else
result.Error = "key的值不能为空!";
return result;
}
///
/// 删除
///
///
///
[HttpGet, HttpPost]
public Result Delete(string key)
{
result = new Result();
if (!string.IsNullOrWhiteSpace(key))
{
// 查询mytestkey的实时值
var info = Search(key);
if (!string.IsNullOrWhiteSpace(info))
result.Value = info;
bool isDeleteSuccess = redisHelper.DeleteKey(key);
result.ImplementationResults = isDeleteSuccess;
if (isDeleteSuccess)
{
// 查询mytestkey的缓存值
var infoCache = Search(key);
if (!string.IsNullOrWhiteSpace(infoCache))
result.CacheValue = "缓存值查询结果:" + infoCache;
}
} else
result.Error = "key的值不能为空!";
return result;
}
}
5)Startup.cs中添加swagger文档注释
生成带有注释的swagger文档
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "RedisDemo", Version = "v1" });
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, "APIHelp.xml");
c.IncludeXmlComments(xmlPath, true);
});
}
直接在swagger中测试执行
![](https://img-blog.csdnimg.cn/c67f0b2e5f2e488a8f71985ad860fe70.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_20,color_FFFFFF,t_70,g_se,x_16)
测试之前要在Redis中添加key的值,下面简单介绍一下Redis可视化软件
6、Redis可视化软件
1)RedisDesktopManager
官网:https://rdm.dev/
官方GitHub:https://github.com/uglide/RedisDesktopManager
Windows版本下载链接:https://github.com/lework/RedisDesktopManager-Windows/releases
1、输入实例名,连接地址和端口号连接到Redis
![](https://img-blog.csdnimg.cn/a1d3bf37c6f04f0f9a8649af1146606c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_11,color_FFFFFF,t_70,g_se,x_16)
2、新建key,可随时Reload刷新查看key及其value的变化
![](https://img-blog.csdnimg.cn/be2881162955480ea25e91d871e5bc34.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc3Vuc2hpbmVHR0I=,size_20,color_FFFFFF,t_70,g_se,x_16)
2) QuickRedis
下载链接:QuickRedis 介绍 - redis 可视化管理工具 | QuickOfficial - QuickRedis (quick123.net)
使用方法与RedisDesktopManager相同,使用截图如下
1、输入实例名,连接地址和端口号连接到Redis
![](https://i-blog.csdnimg.cn/direct/8fe715522ab840e1917fee02818c84fb.png)
7、附录
Redis官网链接:https://redis.io/ Redis官方GitHub:https://github.com/redis/redis/releases Redis下载链接:http://download.redis.io/releases/
以上就是.NET Core Redis的简单使用的介绍,做此记录,如有帮助,欢迎点赞关注收藏!
|