Merge branch 'dev' of 123.207.203.228:server/HuanMengProject into dev
This commit is contained in:
commit
5263badb1a
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存缓存帮助类
|
||||
/// </summary>
|
||||
public class MemoryCacheHelper
|
||||
{
|
||||
private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <returns></returns>
|
||||
public static T? GetCache<T>(string cacheName) where T : class, new()
|
||||
{
|
||||
|
||||
return cache.TryGetValue(cacheName, out var value) ? value as T : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置缓存
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
/// <param name="val"></param>
|
||||
/// <param name="cacheTime">单位秒,默认1小时</param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="cacheName"></param>
|
||||
public static void DelCache(string? cacheName = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(cacheName))
|
||||
cache.Remove(cacheName);
|
||||
else
|
||||
cache.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
56
src/0-core/HuanMeng.MiaoYu.Code/Cache/CharacterBaseCache.cs
Normal file
56
src/0-core/HuanMeng.MiaoYu.Code/Cache/CharacterBaseCache.cs
Normal file
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 人物信息缓存
|
||||
/// </summary>
|
||||
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<T_Character> GetCharacterFromCache(int userId)
|
||||
{
|
||||
lock (_cacheLock) // 加锁以防止并发访问
|
||||
{
|
||||
if (!_memoryCache.TryGetValue(CharactersCacheKey , out List<T_Character> 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<T_Character>();
|
||||
}
|
||||
}
|
||||
return cachedCharacter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 妙语数据库实体类缓存
|
||||
/// </summary>
|
||||
public class MiaoYuDataEntityCache<T>(DAO _dAO) where T : class
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
|||
/// </summary>
|
||||
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<CharacterInfoDto> GetCharacterInfo()
|
||||
//{
|
||||
|
||||
//}
|
||||
/// <summary>
|
||||
/// 删除聊天记录
|
||||
/// </summary>
|
||||
|
|
@ -26,6 +36,8 @@ namespace HuanMeng.MiaoYu.Code.Chat
|
|||
/// <returns></returns>
|
||||
public async Task<bool> DelChatByIds(List<int> id,int characterId)
|
||||
{
|
||||
|
||||
List<T_Character> list = _characterCache.GetCharacterFromCache(characterId);
|
||||
var chatsToDelete = await Dao.daoDbMiaoYu.context.T_Chat.Where(t => id.Contains(t.Id)).ToListAsync();
|
||||
if (chatsToDelete.Count <= 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace HuanMeng.MiaoYu.Model.Dto.Home
|
||||
{
|
||||
public class CharacterInfo
|
||||
{
|
||||
public List<CharacterInfoDto> CharacterInfos { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 用户和人物信息
|
||||
/// </summary>
|
||||
|
|
@ -94,6 +98,9 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
|
|||
/// </summary>
|
||||
public class ChatListDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天列表
|
||||
/// </summary>
|
||||
public List<ChatMessageDto> ChatList { get; set; }
|
||||
}
|
||||
|
||||
|
|
@ -105,12 +112,12 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
|
|||
/// <summary>
|
||||
/// 聊天记录id
|
||||
/// </summary>
|
||||
public List<int> id { get; set; }
|
||||
public List<int> Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 人物id
|
||||
/// </summary>
|
||||
public int characterId { get; set; }
|
||||
public int CharacterId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -121,6 +128,6 @@ namespace HuanMeng.MiaoYu.Model.Dto.Home
|
|||
/// <summary>
|
||||
/// 人物id
|
||||
/// </summary>
|
||||
public int characterId { get; set; }
|
||||
public int CharacterId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,14 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
[ApiController]
|
||||
public class ChatController : MiaoYuControllerBase
|
||||
{
|
||||
private readonly ChatBLL _chatBLL;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="_serviceProvider"></param>
|
||||
public ChatController(IServiceProvider _serviceProvider) : base(_serviceProvider)
|
||||
public ChatController(IServiceProvider _serviceProvider, ChatBLL chatBLL) : base(_serviceProvider)
|
||||
{
|
||||
_chatBLL= chatBLL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -43,11 +45,11 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
/// <summary>
|
||||
/// 获取聊天列表信息
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="characterId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<BaseResponse<ChatListDto>> GetChatInfo()
|
||||
public async Task<BaseResponse<ChatListDto>> GetChatInfo(int characterId)
|
||||
{
|
||||
var obj = JsonConvert.DeserializeObject<ChatListDto>("{\"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<ChatListDto>(ResonseCode.Success, "", obj);
|
||||
|
|
@ -63,12 +65,12 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
[AllowAnonymous]
|
||||
public async Task<BaseResponse<bool>> 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<bool>(ResonseCode.Success, "", obj);
|
||||
}
|
||||
|
||||
|
|
@ -81,8 +83,8 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
[AllowAnonymous]
|
||||
public async Task<BaseResponse<bool>> 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<bool>(ResonseCode.Success, "", obj);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<CharacterInfoBaseCache>();
|
||||
////builder.Services.AddScoped<ChatBLL>();
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user