CSharp-Single-EFCore/Bunny.WebApi/Controllers/RedisController.cs

114 lines
3.0 KiB
C#
Raw Normal View History

2024-09-02 09:00:50 +08:00
using Bunny.Dao.Model.Result;
using Bunny.Service.IService;
using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers;
/// <summary>
/// 操作Redis相关内容
/// </summary>
[Route("/api/[controller]/[action]")]
public class RedisOptionController : ControllerBase
{
private readonly IRedisOptionService _redisOptionService;
public RedisOptionController(IRedisOptionService redisOptionService)
{
_redisOptionService = redisOptionService;
}
/// <summary>
/// 添加Redis中一个值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpPost]
public Result<string> AddStringValue(string key, string value)
{
_redisOptionService.AddStringValue(key, value);
return Result<string>.Success();
}
/// <summary>
/// 查询字符串Key
/// </summary>
/// <returns></returns>
[HttpPost]
public Result<string> QueryStringKey(string key)
{
var queryStringKey = _redisOptionService.QueryStringKey(key);
return Result<string>.Success(queryStringKey);
}
/// <summary>
/// 添加时间限制的key
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpPost]
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]
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();
}
}