diff --git a/src/0-core/HuanMeng.DotNetCore/CacheHelper/MemoryCacheHelper.cs b/src/0-core/HuanMeng.DotNetCore/CacheHelper/MemoryCacheHelper.cs
new file mode 100644
index 0000000..8b9758e
--- /dev/null
+++ b/src/0-core/HuanMeng.DotNetCore/CacheHelper/MemoryCacheHelper.cs
@@ -0,0 +1,55 @@
+using Microsoft.Extensions.Caching.Memory;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace XLib.DotNetCore.CacheHelper
+{
+ ///
+ /// 内存缓存帮助类
+ ///
+ public class MemoryCacheHelper
+ {
+ private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
+
+ ///
+ /// 获取缓存
+ ///
+ ///
+ ///
+ ///
+ public static T? GetCache(string cacheName) where T : class, new()
+ {
+
+ return cache.TryGetValue(cacheName, out var value) ? value as T : null;
+ }
+
+ ///
+ /// 设置缓存
+ ///
+ ///
+ ///
+ /// 单位秒,默认1小时
+ public static void SetCache(string cacheName, object val, int cacheTime = 21000)
+ {
+ //数据量渐渐大了,每次因为很多都是同时缓存 所以在这里分流一下
+ if (cacheTime == 21000)
+ cacheTime = new Random().Next(21000, 43200);
+ cache.Set(cacheName, val, TimeSpan.FromSeconds(cacheTime));
+ }
+
+ ///
+ /// 删除缓存
+ ///
+ ///
+ public static void DelCache(string? cacheName = null)
+ {
+ if (!string.IsNullOrEmpty(cacheName))
+ cache.Remove(cacheName);
+ else
+ cache.Dispose();
+ }
+ }
+}
diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/CharacterBaseCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/CharacterBaseCache.cs
new file mode 100644
index 0000000..44b6d31
--- /dev/null
+++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/CharacterBaseCache.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using HuanMeng.MiaoYu.Code.DataAccess;
+using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
+using Microsoft.Extensions.Caching.Memory;
+
+namespace HuanMeng.MiaoYu.Code.Cache
+{
+ ///
+ /// 人物信息缓存
+ ///
+ public class CharacterInfoBaseCache
+ {
+ private IMemoryCache _memoryCache;
+ private object _cacheLock = new object(); // 用于线程安全的锁
+ private const string CharactersCacheKey = "Characters";
+ private DAO _dao;
+
+ public CharacterInfoBaseCache(IMemoryCache memoryCache, DAO dao)
+ {
+ this._memoryCache = memoryCache;
+ this._dao = dao;
+ }
+
+ public List GetCharacterFromCache(int userId)
+ {
+ lock (_cacheLock) // 加锁以防止并发访问
+ {
+ if (!_memoryCache.TryGetValue(CharactersCacheKey , out List cachedCharacter))
+ {
+ // 如果缓存中没有数据,则从数据库中获取
+ //var db = new YourDbContext();
+ var character = _dao.daoDbMiaoYu.context.T_Character.ToList();
+ if (character != null && character.Count > 0)
+ {
+ // 将数据放入缓存
+ var cacheEntryOptions = new MemoryCacheEntryOptions()
+ .SetAbsoluteExpiration(TimeSpan.FromMinutes(30)) // 设置缓存过期时间
+ .SetSlidingExpiration(TimeSpan.FromMinutes(5)); // 设置滑动过期时间
+ _memoryCache.Set(CharactersCacheKey , character, cacheEntryOptions);
+
+ cachedCharacter = character;
+ }
+ else
+ {
+ cachedCharacter = new List();
+ }
+ }
+ return cachedCharacter;
+ }
+ }
+ }
+}
diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs
new file mode 100644
index 0000000..7997119
--- /dev/null
+++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs
@@ -0,0 +1,16 @@
+using HuanMeng.MiaoYu.Code.DataAccess;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HuanMeng.MiaoYu.Code.Cache
+{
+ ///
+ /// 妙语数据库实体类缓存
+ ///
+ public class MiaoYuDataEntityCache(DAO _dAO) where T : class
+ {
+ }
+}
diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs b/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs
index a6049eb..3b02d69 100644
--- a/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs
+++ b/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs
@@ -1,4 +1,7 @@
using HuanMeng.DotNetCore.Base;
+using HuanMeng.MiaoYu.Code.Cache;
+using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
+using HuanMeng.MiaoYu.Model.Dto.Home;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
@@ -14,10 +17,17 @@ namespace HuanMeng.MiaoYu.Code.Chat
///
public class ChatBLL : MiaoYuBase
{
- public ChatBLL(IServiceProvider serviceProvider) : base(serviceProvider)
- {
+ private readonly CharacterInfoBaseCache _characterCache;
+ public ChatBLL(IServiceProvider serviceProvider, CharacterInfoBaseCache characterCache) : base(serviceProvider)
+ {
+ _characterCache = characterCache;
}
+
+ //public async Task GetCharacterInfo()
+ //{
+
+ //}
///
/// 删除聊天记录
///
@@ -26,6 +36,8 @@ namespace HuanMeng.MiaoYu.Code.Chat
///
public async Task DelChatByIds(List id,int characterId)
{
+
+ List list = _characterCache.GetCharacterFromCache(characterId);
var chatsToDelete = await Dao.daoDbMiaoYu.context.T_Chat.Where(t => id.Contains(t.Id)).ToListAsync();
if (chatsToDelete.Count <= 0)
{
diff --git a/src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/CharacterInfoDto.cs b/src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/CharacterInfoDto.cs
index bbcade7..d0c4bac 100644
--- a/src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/CharacterInfoDto.cs
+++ b/src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/CharacterInfoDto.cs
@@ -6,6 +6,10 @@ using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Model.Dto.Home
{
+ public class CharacterInfo
+ {
+ public List CharacterInfos { get; set; }
+ }
///
/// 用户和人物信息
///
@@ -94,6 +98,9 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
///
public class ChatListDto
{
+ ///
+ /// 聊天列表
+ ///
public List ChatList { get; set; }
}
@@ -105,12 +112,12 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
///
/// 聊天记录id
///
- public List id { get; set; }
+ public List Id { get; set; }
///
/// 人物id
///
- public int characterId { get; set; }
+ public int CharacterId { get; set; }
}
///
@@ -121,6 +128,6 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
///
/// 人物id
///
- public int characterId { get; set; }
+ public int CharacterId { get; set; }
}
}
diff --git a/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs b/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs
index d2a80d3..3c20bd2 100644
--- a/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs
+++ b/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs
@@ -18,12 +18,14 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
[ApiController]
public class ChatController : MiaoYuControllerBase
{
+ private readonly ChatBLL _chatBLL;
///
///
///
///
- public ChatController(IServiceProvider _serviceProvider) : base(_serviceProvider)
+ public ChatController(IServiceProvider _serviceProvider, ChatBLL chatBLL) : base(_serviceProvider)
{
+ _chatBLL= chatBLL;
}
///
@@ -43,11 +45,11 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
///
/// 获取聊天列表信息
///
- ///
+ ///
///
[HttpGet]
[AllowAnonymous]
- public async Task> GetChatInfo()
+ public async Task> GetChatInfo(int characterId)
{
var obj = JsonConvert.DeserializeObject("{\"ChatList\":[{\"Id\":\"1\",\"Role\":\"user\",\"Content\":\"Hello, how are you?\",\"Timestamp\":\"2022-03-01 12:00:00 \",\"MessageType\":0,\"UserIcon\":\"\"},{\"Id\":\"2\",\"Role\":\"assistant\",\"Content\":\"I'm fine, thanks!\",\"Timestamp\":\"2022-03-01 12:05:00 \",\"UserIcon\":\"\"}]}");
return new BaseResponse(ResonseCode.Success, "", obj);
@@ -63,12 +65,12 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
[AllowAnonymous]
public async Task> DelChatByIds([FromBody] DelChat delChat)
{
- if (delChat.id == null || delChat.characterId == 0)
+ if (delChat.Id == null || delChat.CharacterId == 0)
{
throw new ArgumentNullException();
}
- ChatBLL chatBLL = new ChatBLL(ServiceProvider);
- var obj = await chatBLL.DelChatByIds(delChat.id, delChat.characterId);
+ //ChatBLL chatBLL = new ChatBLL(ServiceProvider);
+ var obj = await _chatBLL.DelChatByIds(delChat.Id, delChat.CharacterId);
return new BaseResponse(ResonseCode.Success, "", obj);
}
@@ -81,8 +83,8 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
[AllowAnonymous]
public async Task> DelChat([FromBody] DelChatList delChatList)
{
- ChatBLL chatBLL = new ChatBLL(ServiceProvider);
- var obj = await chatBLL.DelChat(delChatList.characterId);
+ //ChatBLL chatBLL = new ChatBLL(ServiceProvider);
+ var obj = await _chatBLL.DelChat(delChatList.CharacterId);
return new BaseResponse(ResonseCode.Success, "", obj);
}
}
diff --git a/src/2-api/HuanMeng.MiaoYu.WebApi/Program.cs b/src/2-api/HuanMeng.MiaoYu.WebApi/Program.cs
index 28014e2..12bafeb 100644
--- a/src/2-api/HuanMeng.MiaoYu.WebApi/Program.cs
+++ b/src/2-api/HuanMeng.MiaoYu.WebApi/Program.cs
@@ -10,6 +10,8 @@ using HuanMeng.MiaoYu.Code.JwtUtil;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using HuanMeng.Utility.AssemblyHelper;
using HuanMeng.DotNetCore.CustomExtension;
+using HuanMeng.MiaoYu.Code.Cache;
+using HuanMeng.MiaoYu.Code.Chat;
var builder = WebApplication.CreateBuilder(args);
// 检索程序集信息
@@ -71,6 +73,9 @@ builder.AddTencent();
builder.AddMemoryVerificationCode();
//添加jwt验证
builder.AddJwtConfig();
+//builder.Services.AddMemoryCache();
+//builder.Services.AddScoped();
+////builder.Services.AddScoped();
var app = builder.Build();
// Configure the HTTP request pipeline.