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帮助类
/// <summary> /// Redis帮助类 /// </summary> public class RedisHelper { public ConnectionMultiplexer redis { get; set; } public IDatabase db { get; set; } public RedisHelper(string connection) { redis = ConnectionMultiplexer.Connect(connection); db = redis.GetDatabase(); } /// <summary> /// 增加/修改 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetValue(string key, string value) { return db.StringSet(key, value); } /// <summary> /// 查询 /// </summary> /// <param name="key"></param> /// <returns></returns> public string GetValue(string key) { return db.StringGet(key); } /// <summary> /// 删除 /// </summary> /// <param name="key"></param> /// <returns></returns> public bool DeleteKey(string key) { return db.KeyDelete(key); } }
3)新建返回结果类
/// <summary> /// 返回结果类 /// </summary> public class Result { /// <summary> /// 执行结果 /// </summary> public bool ImplementationResults { get; set; } /// <summary> /// Value的实时值 /// </summary> public string Value { get; set; } /// <summary> /// 缓存的Value值 /// </summary> public string CacheValue { get; set; } /// <summary> /// 错误信息 /// </summary> 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; /// <summary> /// 查询 /// </summary> /// <param name="key">key</param> /// <returns></returns> [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; } /// <summary> /// 新增 /// </summary> /// <param name="key">key</param> /// <returns></returns> [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; } /// <summary> /// 修改 /// </summary> /// <param name="key">key</param> /// <param name="newValue">新的value值</param> /// <returns></returns> [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; } /// <summary> /// 删除 /// </summary> /// <param name="key"></param> /// <returns></returns> [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中测试执行
测试之前要在Redis中添加key的值,下面简单介绍一下Redis可视化软件
6、Redis可视化软件
1)RedisDesktopManager(付费软件)
官方GitHub:https://github.com/uglide/RedisDesktopManager
Windows版本下载链接:https://github.com/lework/RedisDesktopManager-Windows/releases
1、输入实例名,连接地址和端口号连接到Redis
2、新建key,可随时Reload刷新查看key及其value的变化
2) Redis Assistant(免费,推荐)
使用方法与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/