🚀 feat(新增): Redis字符串增加和查询

This commit is contained in:
bunny 2024-08-09 10:06:17 +08:00
parent 1c2ce54804
commit ad50dcf6b6
17 changed files with 164 additions and 24 deletions

View File

@ -7,24 +7,25 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="StackExchange.Redis" Version="2.8.0"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bunny.Dao\Bunny.Dao.csproj" />
<ProjectReference Include="..\Bunny.Dao\Bunny.Dao.csproj"/>
</ItemGroup>
</Project>

View File

@ -1,23 +1,24 @@
using Bunny.Dao.Models.System;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Common.EFCore;
namespace Bunny.Common.Connect;
public class EfCoreContext : DbContext
{
public EfCoreContext()
{
DbPath = @"D:\MyFolder\Bunny\Bunny-cli\Template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
// var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
// DbPath = dataBaseConnection;
// DbPath = @"D:\MyFolder\Bunny\Bunny-cli\Template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
DbPath = dataBaseConnection;
}
public string DbPath { get; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
public string DbPath { get; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{

View File

@ -0,0 +1,36 @@
using StackExchange.Redis;
namespace Bunny.Common.Connect;
public class RedisContext
{
public static IDatabase RedisDatabase;
private static readonly EndPointCollection EndPointCollection = new();
public static void Initial()
{
// 获取端口等配置信息
var host = AppSettings.GetConfig("Redis:Host");
var port = Convert.ToInt32(AppSettings.GetConfig("Redis:Port"));
var defaultDb = Convert.ToInt32(AppSettings.GetConfig("Redis:DefaultDB"));
var password = AppSettings.Get<string>("Redis:Password");
var timeout = Convert.ToInt32(AppSettings.GetConfig("Redis:AsyncTimeout"));
// 添加连接地址
EndPointCollection.Add(host, port);
// 初始化连接对象
var connect = ConnectionMultiplexer.Connect(new ConfigurationOptions
{
EndPoints = EndPointCollection,
Password = password,
DefaultDatabase = defaultDb,
AsyncTimeout = timeout
});
// 创建连接对象
RedisDatabase = connect.GetDatabase();
Console.WriteLine("Redis 连接成功...");
}
}

View File

@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Bunny.Common.EFCore;
using Bunny.Common.Connect;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

View File

@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Bunny.Common.EFCore;
using Bunny.Common.Connect;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

View File

@ -0,0 +1,17 @@
namespace Bunny.Service.IService;
public interface IRedisOptionService
{
/// <summary>
/// 添加Redis中一个值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
void AddStringValue(string key, string value);
/// <summary>
/// 查询字符串Key
/// </summary>
/// <param name="key"></param>
string QueryStringKey(string key);
}

View File

@ -1,4 +1,4 @@
using Bunny.Common.EFCore;
using Bunny.Common.Connect;
using Bunny.Dao.Models.System;
namespace Bunny.Service.IService.Service;

View File

@ -0,0 +1,29 @@
using Bunny.Common.Connect;
using StackExchange.Redis;
namespace Bunny.Service.IService.Service;
public class RedisOptionService : IRedisOptionService
{
private readonly IDatabase _redisDatabase = RedisContext.RedisDatabase;
/// <summary>
/// 添加Redis中一个值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddStringValue(string key, string value)
{
_redisDatabase.StringSet(key, value);
}
/// <summary>
/// 查询字符串Key
/// </summary>
/// <param name="key"></param>
public string QueryStringKey(string key)
{
return _redisDatabase.StringGet(key).ToString();
}
}

View File

@ -1,4 +1,4 @@
using Bunny.Common.EFCore;
using Bunny.Common.Connect;
using Bunny.Dao.Models.System;
using NUnit.Framework;

View File

@ -1,4 +1,5 @@
using Bunny.Common;
using Bunny.Common.Connect;
using Bunny.Service.WebSocket;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
@ -63,7 +64,7 @@ public class BaseConfig
FilterConfig.Initialize(builder);
// 初始化Redis
// RedisTemplate.Initial();
RedisContext.Initial();
// 初始化 Knife4Net文档
Knife4Net.Initialize(builder);

View File

@ -1,4 +1,4 @@
using Bunny.Common.EFCore;
using Bunny.Common.Connect;
using Bunny.Service.IService;
using Bunny.Service.IService.Service;
using Bunny.WebApi.Controllers;
@ -12,13 +12,17 @@ public static class ServiceRegistration
builder.Services.AddScoped<IndexController>();
builder.Services.AddScoped<TemplateController>();
builder.Services.AddScoped<BlogController>();
builder.Services.AddScoped<RedisOptionController>();
}
public static void AddApplicationServices(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<EfCoreContext>();
// 注入Service服务
builder.Services.AddScoped<IBaseService, BaseService>();
builder.Services.AddScoped<IBlogService, BlogService>();
builder.Services.AddScoped<IRedisOptionService, RedisOptionService>();
}
/// <summary>

View File

@ -8,6 +8,7 @@ namespace Bunny.WebApi.Controllers;
/// <summary>
/// BLog相关接口
/// </summary>
[ApiController]
[Route("/api/[controller]")]
public class BlogController : ControllerBase
{
@ -23,7 +24,7 @@ public class BlogController : ControllerBase
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpGet("AddBlog")]
[HttpPost("AddBlog")]
public Result<string> AddBlog(Blog dto)
{
_blogService.AddBlog(dto);
@ -46,7 +47,7 @@ public class BlogController : ControllerBase
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpGet("UpdateBlog")]
[HttpPost("UpdateBlog")]
public Result<string> UpdateBlog(Blog dto)
{
_blogService.UpdateBlog(dto);

View File

@ -0,0 +1,43 @@
using Bunny.Dao.Result;
using Bunny.Service.IService;
using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers;
/// <summary>
/// 操作Redis相关内容
/// </summary>
[Route("/api/[controller]")]
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("AddStringValue")]
public Result<string> AddStringValue(string key, string value)
{
_redisOptionService.AddStringValue(key, value);
return Result<string>.Success();
}
/// <summary>
/// 查询字符串Key
/// </summary>
/// <returns></returns>
[HttpPost("QueryStringKey")]
public Result<string> QueryStringKey(string key)
{
var queryStringKey = _redisOptionService.QueryStringKey(key);
return Result<string>.Success(queryStringKey);
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -14,5 +14,12 @@
"JWT": {
"Issuer": "Issuer",
"Audience": "Audience"
},
"Redis": {
"Host": "192.168.3.98",
"Port": "6379",
"Password": "123456",
"DefaultDB": 0,
"AsyncTimeout": 300
}
}