🚀 feat(新增): Redis相关操作完成
This commit is contained in:
parent
ad50dcf6b6
commit
8e3c07a360
|
@ -14,4 +14,42 @@ public interface IRedisOptionService
|
|||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
string QueryStringKey(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间限制的key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
void AddTimeRedisKey(string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// var keepTtl = false: 可选参数,表示是否保留已存在键的过期时间。如果设置为true,并且键已经设置了过期时间,那么新设置的键将保留原有的过期时间。
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
void AddTimeRedisKeyTtl(string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// Redis存入JSON内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string AddJson();
|
||||
|
||||
/// <summary>
|
||||
/// 删除Redis中key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
void DeleteKey(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Redis中的事务
|
||||
/// </summary>
|
||||
string SetRedisCreateTransaction(string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// Redis设置Hash值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="keyExpire"></param>
|
||||
void AddHashWithRedis(string key, double keyExpire);
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
using Bunny.Common.Connect;
|
||||
using Bunny.Dao.Models.System;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Bunny.Service.IService.Service;
|
||||
|
@ -26,4 +28,83 @@ public class RedisOptionService : IRedisOptionService
|
|||
{
|
||||
return _redisDatabase.StringGet(key).ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间限制的key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public void AddTimeRedisKey(string key, string value)
|
||||
{
|
||||
// 时间限制为 6 秒
|
||||
_redisDatabase.StringSet(key, value, TimeSpan.FromSeconds(6.0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// var keepTtl = false: 可选参数,表示是否保留已存在键的过期时间。如果设置为true,并且键已经设置了过期时间,那么新设置的键将保留原有的过期时间。
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public void AddTimeRedisKeyTtl(string key, string value)
|
||||
{
|
||||
// var keepTtl = false: 可选参数,表示是否保留已存在键的过期时间。如果设置为true,并且键已经设置了过期时间,那么新设置的键将保留原有的过期时间。throw new NotImplementedException();
|
||||
// keepTtl 默认是false 如果当前给这个key设置了过期时间,但是这个key之前就有过期时间,如果是false会覆盖原有的时间,如果为true不覆盖原有的时间
|
||||
_redisDatabase.StringSet(key, value, TimeSpan.FromSeconds(6.0), true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis存入JSON内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string AddJson()
|
||||
{
|
||||
var post = new Post
|
||||
{
|
||||
PostId = 1,
|
||||
Title = "存入JSON内容",
|
||||
Content = "正在存入JSON内容"
|
||||
};
|
||||
var json = JsonConvert.SerializeObject(post);
|
||||
_redisDatabase.StringSet("postKey", json);
|
||||
return json;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除Redis中key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public void DeleteKey(string key)
|
||||
{
|
||||
_redisDatabase.StringGetDelete(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis中的事务
|
||||
/// </summary>
|
||||
public string SetRedisCreateTransaction(string key, string value)
|
||||
{
|
||||
// 创建事务
|
||||
var transaction = _redisDatabase.CreateTransaction();
|
||||
|
||||
// 向事务中添加操作
|
||||
transaction.StringSetAsync(key, value, TimeSpan.FromSeconds(6.0));
|
||||
transaction.StringSetAsync("key1", "value1", TimeSpan.FromSeconds(6.0));
|
||||
|
||||
// 执行事务
|
||||
var committed = transaction.Execute();
|
||||
|
||||
return committed ? "事务执行成功!" : "事务执行失败!";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis设置Hash值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="keyExpire"></param>
|
||||
public void AddHashWithRedis(string key, double keyExpire)
|
||||
{
|
||||
_redisDatabase.HashSet(key, "key", "value");
|
||||
// 设置过期时间为6秒
|
||||
_redisDatabase.KeyExpire(key, TimeSpan.FromSeconds(keyExpire));
|
||||
}
|
||||
}
|
|
@ -37,9 +37,9 @@ public class BaseConfig
|
|||
private static void UseConfiguration(WebApplicationBuilder builder)
|
||||
{
|
||||
// 配置日志相关
|
||||
builder.Logging.AddLog4Net("Config/log4net.config");
|
||||
// builder.Logging.AddLog4Net("Config/log4net.config");
|
||||
|
||||
//配置文件
|
||||
//配置文件
|
||||
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
||||
|
||||
// 添加 SignalR
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Bunny.WebApi.Controllers;
|
|||
/// <summary>
|
||||
/// 操作Redis相关内容
|
||||
/// </summary>
|
||||
[Route("/api/[controller]")]
|
||||
[Route("/api/[controller]/[action]")]
|
||||
public class RedisOptionController : ControllerBase
|
||||
{
|
||||
private readonly IRedisOptionService _redisOptionService;
|
||||
|
@ -40,4 +40,75 @@ public class RedisOptionController : ControllerBase
|
|||
var queryStringKey = _redisOptionService.QueryStringKey(key);
|
||||
return Result<string>.Success(queryStringKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加时间限制的key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("AddTimeRedisKey")]
|
||||
public Result<string> AddTimeRedisKey(string key, string value)
|
||||
{
|
||||
_redisOptionService.AddTimeRedisKey(key, value);
|
||||
return Result<string>.Success(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前存在时
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result<string> AddTimeRedisKeyTtl(string key, string value)
|
||||
{
|
||||
_redisOptionService.AddTimeRedisKeyTtl(key, value);
|
||||
return Result<string>.Success(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis存入JSON内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("AddJson")]
|
||||
public Result<string> AddJson()
|
||||
{
|
||||
var json = _redisOptionService.AddJson();
|
||||
return Result<string>.Success(json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除Redis中key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
public Result<string> DeleteKey(string key)
|
||||
{
|
||||
_redisOptionService.DeleteKey(key);
|
||||
return Result<string>.Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis中的事务
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result<string> SetRedisCreateTransaction(string key, string value)
|
||||
{
|
||||
var redisCreateTransaction = _redisOptionService.SetRedisCreateTransaction(key, value);
|
||||
return Result<string>.Success(redisCreateTransaction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redis设置Hash值,
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result<string> AddHashWithRedis(string key, double keyExpire = 6.0)
|
||||
{
|
||||
_redisOptionService.AddHashWithRedis(key, keyExpire);
|
||||
return Result<string>.Success();
|
||||
}
|
||||
}
|
|
@ -16,10 +16,10 @@
|
|||
"Audience": "Audience"
|
||||
},
|
||||
"Redis": {
|
||||
"Host": "192.168.3.98",
|
||||
"Host": "47.120.65.66",
|
||||
"Port": "6379",
|
||||
"Password": "123456",
|
||||
"DefaultDB": 0,
|
||||
"Password": "02120212",
|
||||
"DefaultDB": 6,
|
||||
"AsyncTimeout": 300
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue