添加缓存
This commit is contained in:
parent
5263badb1a
commit
af016b57e5
|
|
@ -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;
|
||||
/// <summary>
|
||||
/// 妙语实现类
|
||||
/// </summary>
|
||||
public MiaoYuCache MiaoYuCache
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_miaoYuCache == null)
|
||||
{
|
||||
//new MiaoYuDataEntityCache<T_Character>(Dao).DataList
|
||||
_miaoYuCache = new MiaoYuCache(Dao);
|
||||
|
||||
}
|
||||
return _miaoYuCache;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public abstract class CommonDataEntityCache<T>(object lockObj, int cacheTime = 36000) where T : class
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public abstract string key { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 缓存数据
|
||||
/// </summary>
|
||||
protected List<T>? _dataList;
|
||||
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public List<T> DataList
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dataList == null)
|
||||
{
|
||||
|
||||
_dataList = MemoryCacheHelper.GetCache<List<T>>(key);
|
||||
if (_dataList == null)
|
||||
{
|
||||
lock (lockObj)
|
||||
{
|
||||
if (_dataList == null)
|
||||
{
|
||||
_dataList = GetDataList();
|
||||
MemoryCacheHelper.SetCache(key, _dataList, cacheTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _dataList ?? new List<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存数据
|
||||
/// </summary>
|
||||
public abstract List<T> GetDataList();
|
||||
}
|
||||
}
|
||||
38
src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs
Normal file
38
src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存类
|
||||
/// </summary>
|
||||
/// <param name="dao"></param>
|
||||
public partial class MiaoYuCache(DAO dao)
|
||||
{
|
||||
#region 角色缓存表
|
||||
/// <summary>
|
||||
/// 对话角色缓存表
|
||||
/// </summary>
|
||||
private static object CharacterLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 对话角色缓存表
|
||||
/// </summary>
|
||||
public MiaoYuDataEntityCache<T_Character>? Character { get; set; }
|
||||
/// <summary>
|
||||
/// 角色数据表
|
||||
/// </summary>
|
||||
public List<T_Character> CharactersList
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Character == null)
|
||||
{
|
||||
Character = new MiaoYuDataEntityCache<T_Character>(dao, CharacterLock);
|
||||
}
|
||||
return Character.DataList ?? new List<T_Character>();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 妙语数据库实体类缓存
|
||||
/// 妙语数据库实体类缓存
|
||||
/// </summary>
|
||||
public class MiaoYuDataEntityCache<T>(DAO _dAO) where T : class
|
||||
public class MiaoYuDataEntityCache<T>(DAO _dao, object lockObj, int cacheTime = 36000)
|
||||
: CommonDataEntityCache<T>(lockObj, cacheTime) where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存的key
|
||||
/// </summary>
|
||||
public override string key
|
||||
{
|
||||
get
|
||||
{
|
||||
return $"{_dao.daoDbMiaoYu.context.TenantInfo?.TenantId}:MiaoYu:{typeof(T).Name}";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override List<T> GetDataList()
|
||||
{
|
||||
var dbSet = _dao.daoDbMiaoYu.context.Set<T>();
|
||||
if (dbSet == null)
|
||||
{
|
||||
return new List<T>();
|
||||
}
|
||||
return dbSet.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
|||
/// </summary>
|
||||
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<CharacterInfoDto> GetCharacterInfo()
|
||||
|
|
@ -34,19 +36,19 @@ namespace HuanMeng.MiaoYu.Code.Chat
|
|||
/// <param name="id">聊天id</param>
|
||||
/// <param name="characterId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> DelChatByIds(List<int> id,int characterId)
|
||||
public async Task<bool> DelChatByIds(List<int> id, int characterId)
|
||||
{
|
||||
|
||||
List<T_Character> list = _characterCache.GetCharacterFromCache(characterId);
|
||||
//var charactersList = MiaoYuCache.CharactersList;
|
||||
//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)
|
||||
{
|
||||
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
|
|||
/// <returns></returns>
|
||||
public async Task<bool> 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);
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
global using HuanMeng.MiaoYu.Code.Base;
|
||||
global using HuanMeng.MiaoYu.Code.Base;
|
||||
global using HuanMeng.MiaoYu.Code.DataAccess;
|
||||
global using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
|
|
|
|||
|
|
@ -18,14 +18,14 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
[ApiController]
|
||||
public class ChatController : MiaoYuControllerBase
|
||||
{
|
||||
private readonly ChatBLL _chatBLL;
|
||||
private ChatBLL _chatBLL;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="_serviceProvider"></param>
|
||||
public ChatController(IServiceProvider _serviceProvider, ChatBLL chatBLL) : base(_serviceProvider)
|
||||
public ChatController(IServiceProvider _serviceProvider) : base(_serviceProvider)
|
||||
{
|
||||
_chatBLL= chatBLL;
|
||||
_chatBLL = new ChatBLL(_serviceProvider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user