From af016b57e52c7ea936f708f220b95b0d5d27880d Mon Sep 17 00:00:00 2001 From: zpc Date: Sat, 13 Jul 2024 16:29:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs | 21 +++++++ .../Cache/CommonDataEntityCache.cs | 61 +++++++++++++++++++ .../HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs | 38 ++++++++++++ .../Cache/MiaoYuDataEntityCache.cs | 35 ++++++++--- .../HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs | 20 +++--- .../HuanMeng.MiaoYu.Code/GlobalUsings.cs | 4 +- .../Controllers/ChatController.cs | 6 +- 7 files changed, 164 insertions(+), 21 deletions(-) create mode 100644 src/0-core/HuanMeng.MiaoYu.Code/Cache/CommonDataEntityCache.cs create mode 100644 src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs index 00d8cdf..674a6f4 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs @@ -4,6 +4,7 @@ using HuanMeng.DotNetCore.Base; using HuanMeng.DotNetCore.JwtInfrastructure; using HuanMeng.DotNetCore.JwtInfrastructure.Interface; using HuanMeng.DotNetCore.MultiTenant; +using HuanMeng.MiaoYu.Code.Cache; using HuanMeng.MiaoYu.Code.DataAccess; using HuanMeng.MiaoYu.Code.TencentUtile; using HuanMeng.MiaoYu.Code.Users.UserAccount; @@ -216,5 +217,25 @@ namespace HuanMeng.MiaoYu.Code.Base } } #endregion + + #region 缓存实现类 + private MiaoYuCache? _miaoYuCache; + /// + /// 妙语实现类 + /// + public MiaoYuCache MiaoYuCache + { + get + { + if (_miaoYuCache == null) + { + //new MiaoYuDataEntityCache(Dao).DataList + _miaoYuCache = new MiaoYuCache(Dao); + + } + return _miaoYuCache; + } + } + #endregion } } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/CommonDataEntityCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/CommonDataEntityCache.cs new file mode 100644 index 0000000..2a22092 --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/CommonDataEntityCache.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using XLib.DotNetCore.CacheHelper; + +namespace HuanMeng.MiaoYu.Code.Cache +{ + /// + /// + /// + public abstract class CommonDataEntityCache(object lockObj, int cacheTime = 36000) where T : class + { + /// + /// + /// + public abstract string key { get; } + + /// + /// 缓存数据 + /// + protected List? _dataList; + + /// + /// 数据 + /// + public List DataList + { + get + { + if (_dataList == null) + { + + _dataList = MemoryCacheHelper.GetCache>(key); + if (_dataList == null) + { + lock (lockObj) + { + if (_dataList == null) + { + _dataList = GetDataList(); + MemoryCacheHelper.SetCache(key, _dataList, cacheTime); + } + } + } + } + return _dataList ?? new List(); + } + } + + + /// + /// 获取缓存数据 + /// + public abstract List GetDataList(); + } +} diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs new file mode 100644 index 0000000..650a431 --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs @@ -0,0 +1,38 @@ + + +namespace HuanMeng.MiaoYu.Code.Cache +{ + /// + /// 缓存类 + /// + /// + public partial class MiaoYuCache(DAO dao) + { + #region 角色缓存表 + /// + /// 对话角色缓存表 + /// + private static object CharacterLock = new object(); + + /// + /// 对话角色缓存表 + /// + public MiaoYuDataEntityCache? Character { get; set; } + /// + /// 角色数据表 + /// + public List CharactersList + { + get + { + if (Character == null) + { + Character = new MiaoYuDataEntityCache(dao, CharacterLock); + } + return Character.DataList ?? new List(); + } + } + + #endregion + } +} diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs index 7997119..95d2cbd 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs @@ -1,16 +1,35 @@ -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 + public class MiaoYuDataEntityCache(DAO _dao, object lockObj, int cacheTime = 36000) + : CommonDataEntityCache(lockObj, cacheTime) where T : class { + /// + /// 缓存的key + /// + public override string key + { + get + { + return $"{_dao.daoDbMiaoYu.context.TenantInfo?.TenantId}:MiaoYu:{typeof(T).Name}"; + } + } + /// + /// + /// + /// + public override List GetDataList() + { + var dbSet = _dao.daoDbMiaoYu.context.Set(); + if (dbSet == null) + { + return new List(); + } + return dbSet.ToList(); + } } } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs b/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs index 3b02d69..26646a3 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Chat/ChatBLL.cs @@ -2,8 +2,10 @@ 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; using System.Collections.Generic; using System.Linq; @@ -17,11 +19,11 @@ namespace HuanMeng.MiaoYu.Code.Chat /// public class ChatBLL : MiaoYuBase { - private readonly CharacterInfoBaseCache _characterCache; - public ChatBLL(IServiceProvider serviceProvider, CharacterInfoBaseCache characterCache) : base(serviceProvider) + + public ChatBLL(IServiceProvider serviceProvider) : base(serviceProvider) { - _characterCache = characterCache; + } //public async Task GetCharacterInfo() @@ -34,19 +36,19 @@ namespace HuanMeng.MiaoYu.Code.Chat /// 聊天id /// /// - public async Task DelChatByIds(List id,int characterId) + public async Task DelChatByIds(List id, int characterId) { - - List list = _characterCache.GetCharacterFromCache(characterId); + //var charactersList = MiaoYuCache.CharactersList; + //List list = _characterCache.GetCharacterFromCache(characterId); var chatsToDelete = await Dao.daoDbMiaoYu.context.T_Chat.Where(t => id.Contains(t.Id)).ToListAsync(); if (chatsToDelete.Count <= 0) { throw new Exception(""); } - var chatList = chatsToDelete.Where(t => t.UserId == _UserId && t.CharacterId == characterId && t.IsDel == false).ToList(); + var chatList = chatsToDelete.Where(t => t.UserId == _UserId && t.CharacterId == characterId && t.IsDel == false).ToList(); if (chatList.Count > 0) { - chatList.ForEach(t=>t.IsDel = true); + chatList.ForEach(t => t.IsDel = true); } Dao.daoDbMiaoYu.context.SaveChanges(); return true; @@ -59,7 +61,7 @@ namespace HuanMeng.MiaoYu.Code.Chat /// public async Task DelChat(int characterId) { - var tempList = await Dao.daoDbMiaoYu.context.T_Chat.Where(t=>t.UserId == _UserId && t.CharacterId==characterId && t.IsDel == false).ToListAsync(); + var tempList = await Dao.daoDbMiaoYu.context.T_Chat.Where(t => t.UserId == _UserId && t.CharacterId == characterId && t.IsDel == false).ToListAsync(); if (tempList.Count > 0) { tempList.ForEach(t => t.IsDel = true); diff --git a/src/0-core/HuanMeng.MiaoYu.Code/GlobalUsings.cs b/src/0-core/HuanMeng.MiaoYu.Code/GlobalUsings.cs index 88e3fa4..2110fe8 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/GlobalUsings.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/GlobalUsings.cs @@ -1 +1,3 @@ -global using HuanMeng.MiaoYu.Code.Base; \ No newline at end of file +global using HuanMeng.MiaoYu.Code.Base; +global using HuanMeng.MiaoYu.Code.DataAccess; +global using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu; diff --git a/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs b/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs index 3c20bd2..1bfcbe2 100644 --- a/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs +++ b/src/2-api/HuanMeng.MiaoYu.WebApi/Controllers/ChatController.cs @@ -18,14 +18,14 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers [ApiController] public class ChatController : MiaoYuControllerBase { - private readonly ChatBLL _chatBLL; + private ChatBLL _chatBLL; /// /// /// /// - public ChatController(IServiceProvider _serviceProvider, ChatBLL chatBLL) : base(_serviceProvider) + public ChatController(IServiceProvider _serviceProvider) : base(_serviceProvider) { - _chatBLL= chatBLL; + _chatBLL = new ChatBLL(_serviceProvider); } ///