306 lines
9.4 KiB
C#
306 lines
9.4 KiB
C#
|
|
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// 缓存类
|
|
/// </summary>
|
|
/// <param name="dao"></param>
|
|
public partial class MiaoYuCache(DAO dao, IMapper mapper)
|
|
{
|
|
#region 角色缓存表
|
|
|
|
/// <summary>
|
|
/// 对话角色缓存表
|
|
/// </summary>
|
|
public CommonDataEntityCache<T_Character>? Character { get; set; }
|
|
/// <summary>
|
|
/// 角色数据表
|
|
/// </summary>
|
|
public List<T_Character> CharacterInfoList
|
|
{
|
|
get
|
|
{
|
|
if (Character == null)
|
|
{
|
|
//Character = new MiaoYuDataEntityCache<T_Character>(dao, MiaoYuCacheExtend.CharacterLock);
|
|
Character = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Character>(dao);
|
|
}
|
|
return Character.DataList ?? new List<T_Character>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 模型缓存表
|
|
|
|
|
|
/// <summary>
|
|
/// 模型缓存表
|
|
/// </summary>
|
|
public CommonDataEntityCache<T_Model_Config>? ModelConfig { get; set; }
|
|
/// <summary>
|
|
/// 模型
|
|
/// </summary>
|
|
public List<T_Model_Config> ModelConfigList
|
|
{
|
|
get
|
|
{
|
|
if (ModelConfig == null)
|
|
{
|
|
//ModelConfig = new MiaoYuDataEntityCache<T_Model_Config>(dao, MiaoYuCacheExtend.ModelConfigLock);
|
|
ModelConfig = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Model_Config>(dao);
|
|
}
|
|
return ModelConfig.DataList ?? new List<T_Model_Config>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 角色信息
|
|
/// <summary>
|
|
/// 角色缓存表
|
|
/// </summary>
|
|
public CharacterEntityCache? CharacterCache { get; set; }
|
|
/// <summary>
|
|
/// 角色
|
|
/// </summary>
|
|
public List<CharacterCache> CharacterList
|
|
{
|
|
get
|
|
{
|
|
if (CharacterCache == null)
|
|
{
|
|
|
|
CharacterCache = new CharacterEntityCache(dao, mapper);
|
|
}
|
|
return CharacterCache.DataList ?? new List<CharacterCache>();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 图片缓存表
|
|
|
|
|
|
/// <summary>
|
|
/// 图片缓存表
|
|
/// </summary>
|
|
public CommonDataEntityCache<T_Image_Config>? ImageConfigCache { get; set; }
|
|
/// <summary>
|
|
/// 图片
|
|
/// </summary>
|
|
public List<T_Image_Config> ImageConfigList
|
|
{
|
|
get
|
|
{
|
|
if (ImageConfigCache == null)
|
|
{
|
|
//ImageConfigCache = new MiaoYuDataEntityCache<T_Image_Config>(dao, MiaoYuCacheExtend.ImageConfigLock);
|
|
ImageConfigCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Image_Config>(dao);
|
|
}
|
|
return ImageConfigCache.DataList ?? new List<T_Image_Config>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 角色类型缓存表
|
|
|
|
|
|
/// <summary>
|
|
/// 角色类型缓存表
|
|
/// </summary>
|
|
public CommonDataEntityCache<T_Character_Type>? CharacterTypeCache { get; set; }
|
|
/// <summary>
|
|
/// 角色类型
|
|
/// </summary>
|
|
public List<T_Character_Type> CharacterTypeList
|
|
{
|
|
get
|
|
{
|
|
if (CharacterTypeCache == null)
|
|
{
|
|
//CharacterTypeCache = new MiaoYuDataEntityCache<T_Character_Type>(dao, MiaoYuCacheExtend.CharacterTypeLock);
|
|
CharacterTypeCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Character_Type>(dao);
|
|
}
|
|
return CharacterTypeCache.DataList ?? new List<T_Character_Type>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 角色类型缓存表
|
|
|
|
|
|
/// <summary>
|
|
/// 类型中的角色缓存表
|
|
/// </summary>
|
|
public CommonDataEntityCache<T_Character_Type_Intimacy>? CharacterTypeIntimacyCache { get; set; }
|
|
/// <summary>
|
|
/// 类型中的角色
|
|
/// </summary>
|
|
public List<T_Character_Type_Intimacy> CharacterTypeIntimacyList
|
|
{
|
|
get
|
|
{
|
|
if (CharacterTypeIntimacyCache == null)
|
|
{
|
|
//CharacterTypeIntimacyCache = new MiaoYuDataEntityCache<T_Character_Type_Intimacy>(dao, MiaoYuCacheExtend.CharacterTypeIntimacyLock);
|
|
CharacterTypeIntimacyCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Character_Type_Intimacy>(dao);
|
|
}
|
|
return CharacterTypeIntimacyCache.DataList ?? new List<T_Character_Type_Intimacy>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// 缓存扩展类
|
|
/// </summary>
|
|
|
|
public static class MiaoYuCacheExtend
|
|
{
|
|
#region 缓存锁
|
|
/// <summary>
|
|
/// 对话角色缓存表
|
|
/// </summary>
|
|
public static object CharacterLock = new object();
|
|
|
|
/// <summary>
|
|
/// 模型缓存表
|
|
/// </summary>
|
|
public static object ModelConfigLock = new object();
|
|
|
|
/// <summary>
|
|
/// 图片缓存表
|
|
/// </summary>
|
|
public static object ImageConfigLock = new object();
|
|
/// <summary>
|
|
///角色类型缓存表
|
|
/// </summary>
|
|
public static object CharacterTypeLock = new object();
|
|
|
|
/// <summary>
|
|
/// 类型中的角色缓存表
|
|
/// </summary>
|
|
public static object CharacterTypeIntimacyLock = new object();
|
|
|
|
/// <summary>
|
|
/// 缓存锁对象
|
|
/// </summary>
|
|
public static Dictionary<Type, object> CacheLockList { get; set; } = new Dictionary<Type, object>
|
|
{
|
|
{ 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 },
|
|
|
|
};
|
|
#endregion
|
|
|
|
#region 缓存扩展
|
|
/// <summary>
|
|
/// 获取对应的缓存
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static CommonDataEntityCache<T> GetMiaoYuDataEntityCache<T>(DAO dao) where T : class
|
|
{
|
|
object cacheLock;
|
|
if (!CacheLockList.TryGetValue(typeof(T), out cacheLock))
|
|
{
|
|
Console.WriteLine("没有找到锁对象==>" + typeof(T).Name);
|
|
// 创建一个新的锁对象,并添加到 CacheLockList 中
|
|
cacheLock = new object();
|
|
CacheLockList[typeof(T)] = cacheLock;
|
|
}
|
|
return new MiaoYuDataEntityCache<T>(dao, cacheLock);
|
|
}
|
|
/// <summary>
|
|
/// 获取缓存数据
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static List<T> GetMiaoYuDataEntityCacheList<T>(DAO dao) where T : class
|
|
{
|
|
var cache = GetMiaoYuDataEntityCache<T>(dao);
|
|
return cache?.DataList ?? new List<T>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除全部缓存
|
|
/// </summary>
|
|
/// <param name="dao"></param>
|
|
public static void ClareMiaoYuDataEntityCache(DAO dao)
|
|
{
|
|
foreach (var item in CacheLockList)
|
|
{
|
|
var t = item.Key;
|
|
Type cacheType = typeof(MiaoYuDataEntityCache<>).MakeGenericType(t);
|
|
var shujuduixiang = Activator.CreateInstance(cacheType, dao, item.Value,36000);
|
|
var x = shujuduixiang as ICacheClearData;
|
|
if (x != null)
|
|
{
|
|
x.ClearData();
|
|
}
|
|
//new MiaoYuDataEntityCache<T>(dao, cacheLock);
|
|
//Activator.CreateInstance(t);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 图片扩展
|
|
/// <summary>
|
|
/// 获取图片
|
|
/// </summary>
|
|
/// <param name="imageId"></param>
|
|
/// <param name="miaoYuCache"></param>
|
|
/// <returns></returns>
|
|
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 "";
|
|
}
|
|
/// <summary>
|
|
/// 获取图片
|
|
/// </summary>
|
|
/// <param name="imageId"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static string GetImageUrl(this int imageId, DAO dao)
|
|
{
|
|
if (imageId == 0 || dao == null)
|
|
{
|
|
return "";
|
|
}
|
|
MiaoYuCache miaoYuCache = new MiaoYuCache(dao, null);
|
|
|
|
return GetImageUrl(imageId, miaoYuCache);
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
|
|
}
|