using AutoMapper; using HuanMeng.MiaoYu.Code.Cache.Contract; using HuanMeng.MiaoYu.Code.Cache.Special; using HuanMeng.MiaoYu.Code.DataAccess; using HuanMeng.MiaoYu.Model.Dto.Character; using System.Linq.Expressions; namespace HuanMeng.MiaoYu.Code.Cache { /// /// 缓存类 /// /// public partial class MiaoYuCache(CacheBase cacheBase) { #region 角色缓存表 /// /// 对话角色缓存表 /// public CommonDataEntityCache? Character { get; set; } /// /// 角色数据表 /// public List CharacterInfoList { get { if (Character == null) { //Character = new MiaoYuDataEntityCache(dao, MiaoYuCacheExtend.CharacterLock); Character = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); } return Character.DataList ?? new List(); } } #endregion #region 模型缓存表 /// /// 模型缓存表 /// public CommonDataEntityCache? ModelConfig { get; set; } /// /// 模型 /// public List ModelConfigList { get { if (ModelConfig == null) { ModelConfig = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); } return ModelConfig.DataList ?? new List(); } } #endregion #region 角色信息 /// /// 角色缓存表 /// public CommonDataEntityCache? CharacterCache { get; set; } /// /// 角色 /// public List CharacterList { get { if (CharacterCache == null) { CharacterCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); //CharacterCache = new CharacterEntityCache(cacheBase); } return CharacterCache.DataList ?? new List(); } } #endregion #region 图片缓存表 /// /// 图片缓存表 /// public CommonDataEntityCache? ImageConfigCache { get; set; } /// /// 图片 /// public List ImageConfigList { get { if (ImageConfigCache == null) { //ImageConfigCache = new MiaoYuDataEntityCache(dao, MiaoYuCacheExtend.ImageConfigLock); ImageConfigCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); } return ImageConfigCache.DataList ?? new List(); } } #endregion #region 角色类型缓存表 /// /// 角色类型缓存表 /// public CommonDataEntityCache? CharacterTypeCache { get; set; } /// /// 角色类型 /// public List CharacterTypeList { get { if (CharacterTypeCache == null) { //CharacterTypeCache = new MiaoYuDataEntityCache(dao, MiaoYuCacheExtend.CharacterTypeLock); CharacterTypeCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); } return CharacterTypeCache.DataList ?? new List(); } } #endregion #region 角色类型缓存表 /// /// 类型中的角色缓存表 /// public CommonDataEntityCache? CharacterTypeIntimacyCache { get; set; } /// /// 类型中的角色 /// public List CharacterTypeIntimacyList { get { if (CharacterTypeIntimacyCache == null) { //CharacterTypeIntimacyCache = new MiaoYuDataEntityCache(dao, MiaoYuCacheExtend.CharacterTypeIntimacyLock); CharacterTypeIntimacyCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache(cacheBase); } return CharacterTypeIntimacyCache.DataList ?? new List(); } } #endregion } /// /// 缓存扩展类 /// public static class MiaoYuCacheExtend { #region 缓存锁 /// /// 对话角色缓存表 /// public static object CharacterLock = new object(); /// /// 模型缓存表 /// public static object ModelConfigLock = new object(); /// /// 图片缓存表 /// public static object ImageConfigLock = new object(); /// ///角色类型缓存表 /// public static object CharacterTypeLock = new object(); /// /// 类型中的角色缓存表 /// public static object CharacterTypeIntimacyLock = new object(); /// /// 缓存锁对象 /// public static Dictionary CacheLockList { get; set; } = new Dictionary { { typeof(T_Character), CharacterLock }, { typeof(T_Model_Config), ModelConfigLock }, { typeof(T_Image_Config), ImageConfigLock }, { typeof(T_Character_Type), CharacterTypeLock }, { typeof(T_Character_Type_Intimacy), CharacterTypeIntimacyLock }, }; /// /// 特殊的缓存锁对象 /// public static Dictionary SpecialCacheLockList { get; set; } = new Dictionary { { typeof(CharacterCache), CharacterLock }, }; #endregion #region 缓存扩展 /// /// 获取对应的缓存 /// /// /// /// public static CommonDataEntityCache GetMiaoYuDataEntityCache(CacheBase cacheBase) where T : class { object cacheLock; var typeo = typeof(T); if (SpecialCacheLockList.TryGetValue(typeo, out cacheLock)) { var o = new CharacterEntityCache(cacheBase, cacheLock) as CommonDataEntityCache; return o; } if (!CacheLockList.TryGetValue(typeo, out cacheLock)) { Console.WriteLine("没有找到锁对象==>" + typeo.Name); // 创建一个新的锁对象,并添加到 CacheLockList 中 cacheLock = new object(); CacheLockList[typeo] = cacheLock; } return new MiaoYuDataEntityCache(cacheBase, cacheLock); } /// /// 获取缓存数据 /// /// /// /// public static List GetMiaoYuDataEntityCacheList(CacheBase cacheBase) where T : class { var cache = GetMiaoYuDataEntityCache(cacheBase); return cache?.DataList ?? new List(); } /// /// 清除全部缓存 /// /// public static void ClareMiaoYuDataEntityCache(CacheBase cacheBase) { foreach (var item in CacheLockList) { var t = item.Key; Type cacheType = typeof(MiaoYuDataEntityCache<>).MakeGenericType(t); var shujuduixiang = Activator.CreateInstance(cacheType, cacheBase, item.Value, 36000); var x = shujuduixiang as ICacheClearData; if (x != null) { x.ClearData(); } } var obj = SpecialCacheLockList[typeof(CharacterCache)]; CharacterEntityCache characterEntityCache = new CharacterEntityCache(cacheBase, obj); characterEntityCache.ClearData(); } /// /// 刷新部缓存 /// /// public static void ReloadMiaoYuDataEntityCache(CacheBase cacheBase) { foreach (var item in CacheLockList) { var t = item.Key; Type cacheType = typeof(MiaoYuDataEntityCache<>).MakeGenericType(t); var shujuduixiang = Activator.CreateInstance(cacheType, cacheBase, item.Value, 36000); var x = shujuduixiang as ICacheReloadData; if (x != null) { x.ReloadData(); } } var obj = SpecialCacheLockList[typeof(CharacterCache)]; CharacterEntityCache characterEntityCache = new CharacterEntityCache(cacheBase, obj); characterEntityCache.ReloadData(); } #endregion #region 图片扩展 /// /// 获取图片 /// /// /// /// public static string GetImageUrl(this int imageId, MiaoYuCache miaoYuCache) { if (imageId == 0 || miaoYuCache == null) { return ""; } var _m = miaoYuCache.ImageConfigList.FirstOrDefault(it => it.ImageId == imageId); if (_m != null) { return _m.Url; } return ""; } /// /// 获取图片 /// /// /// /// public static string GetImageUrl(this int imageId, CacheBase cacheBase) { if (imageId == 0 || cacheBase == null) { return ""; } MiaoYuCache miaoYuCache = new MiaoYuCache(cacheBase); return GetImageUrl(imageId, miaoYuCache); } #endregion } }