1、Redis简介
Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。
2、Redis主要特点
- Redis支持数据的持久化
- Redis支持多种数据结构
- Redis支持数据的备份
3、Redis的下载安装
windows版本Redis下载链接:https://github.com/tporadowski/redis/releases
选择.zip免安装版本可直接通过命令使用。
在Windows终端(可直接在解压文件夹右键选择[在Windows终端打开])。
或者cmd控制台进入到Redis的解压文件夹运行指令
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服务
2)安装Redis服务为本地服务
3)安装完成,可以设置为自启动也可以手动启动
5、Redis在.NET Core的简单使用
1)新建ASP.NET Core Web API项目
打开NuGet包管理器安装StackExchange.Redis(注:本demo为VS2022新建,默认提供的天气预报的类和控制器)
2)新建Redis帮助类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
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)新建返回结果类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class Result { public bool ImplementationResults { get; set; } public string Value { get; set; } public string CacheValue { get; set; } public string Error { get; set; } }
|
4)控制器类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| [ApiController] [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase { public WeatherForecastController() { } RedisHelper redisHelper = new RedisHelper("127.0.0.1:6379"); string value = "this is a test redis string"; Result result = null; [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; } [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) { var info = Search("mytestkey"); if (!string.IsNullOrWhiteSpace(info)) result.Value = info; } } else result.Error = "key的值不能为空!"; return result; } [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; 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)) { var info = Search(key); if (!string.IsNullOrWhiteSpace(info)) result.Value = info; bool isDeleteSuccess = redisHelper.DeleteKey(key); result.ImplementationResults = isDeleteSuccess; if (isDeleteSuccess) { var infoCache = Search(key); if (!string.IsNullOrWhiteSpace(infoCache)) result.CacheValue = "缓存值查询结果:" + infoCache; } } else result.Error = "key的值不能为空!"; return result; } }
|
5)Startup.cs中添加swagger文档注释
生成带有注释的swagger文档
1 2 3 4 5 6 7 8 9 10 11
| 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中测试执行
测试之前要在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
2、新建key,可随时Reload刷新查看key及其value的变化
2) Redis Assistant(免费,推荐)
下载链接:http://www.redisant.cn/
使用方法与RedisDesktopManager相同,使用截图如下
1、输入实例名,连接地址和端口号连接到Redis
2、新建key,可随时点击左上角工具栏的刷新按钮查看key及其value的变化
7、附录
Redis官网链接:https://redis.io/
Redis官方GitHub:https://github.com/redis/redis/releases
Redis下载链接:http://download.redis.io/releases/
以上就是.NET Core Redis的简单使用的介绍,做此记录,如有帮助,欢迎点赞关注收藏!