修改缓存实例
This commit is contained in:
parent
840eaf6336
commit
37a7c95ada
|
|
@ -1,3 +1,5 @@
|
||||||
|
using HuanMeng.MiaoYu.Code.Cache.Contract;
|
||||||
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
@ -15,7 +17,7 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class CommonDataEntityCache<T>(object lockObj, int cacheTime = 36000) where T : class
|
public abstract class CommonDataEntityCache<T>(object lockObj, int cacheTime = 36000) : ICacheClearData, ICacheReloadData where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
|
@ -41,6 +43,7 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
lock (lockObj)
|
lock (lockObj)
|
||||||
{
|
{
|
||||||
|
tempDataList = MemoryCacheHelper.GetCache<List<T>>(key);
|
||||||
if (tempDataList == null)
|
if (tempDataList == null)
|
||||||
{
|
{
|
||||||
tempDataList = GetDataList();
|
tempDataList = GetDataList();
|
||||||
|
|
@ -59,5 +62,25 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
/// 获取缓存数据
|
/// 获取缓存数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract List<T> GetDataList();
|
public abstract List<T> GetDataList();
|
||||||
|
|
||||||
|
public bool ClearData()
|
||||||
|
{
|
||||||
|
lock (lockObj)
|
||||||
|
{
|
||||||
|
MemoryCacheHelper.DelCache(key);
|
||||||
|
_dataList = null;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ReloadData()
|
||||||
|
{
|
||||||
|
lock (lockObj)
|
||||||
|
{
|
||||||
|
var tempDataList = GetDataList();
|
||||||
|
MemoryCacheHelper.SetCache(key, tempDataList, cacheTime);
|
||||||
|
_dataList = tempDataList;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace HuanMeng.MiaoYu.Code.Cache.Contract
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 清除缓存
|
||||||
|
/// </summary>
|
||||||
|
public interface ICacheClearData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 清除数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
bool ClearData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace HuanMeng.MiaoYu.Code.Cache.Contract
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 重新加载数据
|
||||||
|
/// </summary>
|
||||||
|
public interface ICacheReloadData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 重新加载数据
|
||||||
|
/// </summary>
|
||||||
|
void ReloadData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,15 +14,11 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
public partial class MiaoYuCache(DAO dao, IMapper mapper)
|
public partial class MiaoYuCache(DAO dao, IMapper mapper)
|
||||||
{
|
{
|
||||||
#region 角色缓存表
|
#region 角色缓存表
|
||||||
/// <summary>
|
|
||||||
/// 对话角色缓存表
|
|
||||||
/// </summary>
|
|
||||||
private static object CharacterLock = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 对话角色缓存表
|
/// 对话角色缓存表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MiaoYuDataEntityCache<T_Character>? Character { get; set; }
|
public CommonDataEntityCache<T_Character>? Character { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 角色数据表
|
/// 角色数据表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -32,7 +28,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
if (Character == null)
|
if (Character == null)
|
||||||
{
|
{
|
||||||
Character = new MiaoYuDataEntityCache<T_Character>(dao, CharacterLock);
|
//Character = new MiaoYuDataEntityCache<T_Character>(dao, MiaoYuCacheExtend.CharacterLock);
|
||||||
|
Character = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Character>(dao);
|
||||||
}
|
}
|
||||||
return Character.DataList ?? new List<T_Character>();
|
return Character.DataList ?? new List<T_Character>();
|
||||||
}
|
}
|
||||||
|
|
@ -41,15 +38,12 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 模型缓存表
|
#region 模型缓存表
|
||||||
/// <summary>
|
|
||||||
/// 模型缓存表
|
|
||||||
/// </summary>
|
|
||||||
private static object ModelConfigLock = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 模型缓存表
|
/// 模型缓存表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MiaoYuDataEntityCache<T_Model_Config>? ModelConfig { get; set; }
|
public CommonDataEntityCache<T_Model_Config>? ModelConfig { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 模型
|
/// 模型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -59,7 +53,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
if (ModelConfig == null)
|
if (ModelConfig == null)
|
||||||
{
|
{
|
||||||
ModelConfig = new MiaoYuDataEntityCache<T_Model_Config>(dao, ModelConfigLock);
|
//ModelConfig = new MiaoYuDataEntityCache<T_Model_Config>(dao, MiaoYuCacheExtend.ModelConfigLock);
|
||||||
|
ModelConfig = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Model_Config>(dao);
|
||||||
}
|
}
|
||||||
return ModelConfig.DataList ?? new List<T_Model_Config>();
|
return ModelConfig.DataList ?? new List<T_Model_Config>();
|
||||||
}
|
}
|
||||||
|
|
@ -90,15 +85,12 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 图片缓存表
|
#region 图片缓存表
|
||||||
/// <summary>
|
|
||||||
/// 图片缓存表
|
|
||||||
/// </summary>
|
|
||||||
private static object ImageConfigLock = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 图片缓存表
|
/// 图片缓存表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MiaoYuDataEntityCache<T_Image_Config>? ImageConfigCache { get; set; }
|
public CommonDataEntityCache<T_Image_Config>? ImageConfigCache { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 图片
|
/// 图片
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -108,7 +100,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
if (ImageConfigCache == null)
|
if (ImageConfigCache == null)
|
||||||
{
|
{
|
||||||
ImageConfigCache = new MiaoYuDataEntityCache<T_Image_Config>(dao, ImageConfigLock);
|
//ImageConfigCache = new MiaoYuDataEntityCache<T_Image_Config>(dao, MiaoYuCacheExtend.ImageConfigLock);
|
||||||
|
ImageConfigCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Image_Config>(dao);
|
||||||
}
|
}
|
||||||
return ImageConfigCache.DataList ?? new List<T_Image_Config>();
|
return ImageConfigCache.DataList ?? new List<T_Image_Config>();
|
||||||
}
|
}
|
||||||
|
|
@ -118,15 +111,12 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
|
|
||||||
|
|
||||||
#region 角色类型缓存表
|
#region 角色类型缓存表
|
||||||
/// <summary>
|
|
||||||
///角色类型缓存表
|
|
||||||
/// </summary>
|
|
||||||
private static object CharacterTypeLock = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 角色类型缓存表
|
/// 角色类型缓存表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MiaoYuDataEntityCache<T_Character_Type>? CharacterTypeCache { get; set; }
|
public CommonDataEntityCache<T_Character_Type>? CharacterTypeCache { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 角色类型
|
/// 角色类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -136,7 +126,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
if (CharacterTypeCache == null)
|
if (CharacterTypeCache == null)
|
||||||
{
|
{
|
||||||
CharacterTypeCache = new MiaoYuDataEntityCache<T_Character_Type>(dao, CharacterTypeLock);
|
//CharacterTypeCache = new MiaoYuDataEntityCache<T_Character_Type>(dao, MiaoYuCacheExtend.CharacterTypeLock);
|
||||||
|
CharacterTypeCache = MiaoYuCacheExtend.GetMiaoYuDataEntityCache<T_Character_Type>(dao);
|
||||||
}
|
}
|
||||||
return CharacterTypeCache.DataList ?? new List<T_Character_Type>();
|
return CharacterTypeCache.DataList ?? new List<T_Character_Type>();
|
||||||
}
|
}
|
||||||
|
|
@ -145,15 +136,12 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 角色类型缓存表
|
#region 角色类型缓存表
|
||||||
/// <summary>
|
|
||||||
/// 类型中的角色缓存表
|
|
||||||
/// </summary>
|
|
||||||
private static object CharacterTypeIntimacyLock = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 类型中的角色缓存表
|
/// 类型中的角色缓存表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MiaoYuDataEntityCache<T_Character_Type_Intimacy>? CharacterTypeIntimacyCache { get; set; }
|
public CommonDataEntityCache<T_Character_Type_Intimacy>? CharacterTypeIntimacyCache { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 类型中的角色
|
/// 类型中的角色
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -163,7 +151,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
{
|
{
|
||||||
if (CharacterTypeIntimacyCache == null)
|
if (CharacterTypeIntimacyCache == null)
|
||||||
{
|
{
|
||||||
CharacterTypeIntimacyCache = new MiaoYuDataEntityCache<T_Character_Type_Intimacy>(dao, CharacterTypeIntimacyLock);
|
//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>();
|
return CharacterTypeIntimacyCache.DataList ?? new List<T_Character_Type_Intimacy>();
|
||||||
}
|
}
|
||||||
|
|
@ -177,6 +166,78 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
|
|
||||||
public static class MiaoYuCacheExtend
|
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>();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 图片扩展
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取图片
|
/// 获取图片
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -196,12 +257,11 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取图片
|
/// 获取图片
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="imageId"></param>
|
/// <param name="imageId"></param>
|
||||||
/// <param name="miaoYuCache"></param>
|
/// <param name="dao"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetImageUrl(this int imageId, DAO dao)
|
public static string GetImageUrl(this int imageId, DAO dao)
|
||||||
{
|
{
|
||||||
|
|
@ -213,5 +273,8 @@ namespace HuanMeng.MiaoYu.Code.Cache
|
||||||
|
|
||||||
return GetImageUrl(imageId, miaoYuCache);
|
return GetImageUrl(imageId, miaoYuCache);
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,7 @@ namespace HuanMeng.MiaoYu.Code.Cache.Special
|
||||||
/// 锁
|
/// 锁
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static object CharacterCacheLock = new object();
|
private static object CharacterCacheLock = new object();
|
||||||
private static object ImageLock = new object();
|
|
||||||
private static object T_Character_LabelLock = new object();
|
|
||||||
private static object T_Character_Label_RelationLock = new object();
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取缓存数据
|
/// 获取缓存数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -40,26 +38,21 @@ namespace HuanMeng.MiaoYu.Code.Cache.Special
|
||||||
var characteChatCounts = _dao.daoDbMiaoYu.SqlQueryList<CharacteChatCountModel>(sqlString);
|
var characteChatCounts = _dao.daoDbMiaoYu.SqlQueryList<CharacteChatCountModel>(sqlString);
|
||||||
//查询配置表
|
//查询配置表
|
||||||
var modelConfigs = _dao.daoDbMiaoYu.context.T_Model_Config.ToList();
|
var modelConfigs = _dao.daoDbMiaoYu.context.T_Model_Config.ToList();
|
||||||
var ImageConfigCache = new MiaoYuDataEntityCache<T_Image_Config>(_dao, ImageLock);
|
//图片
|
||||||
var images = ImageConfigCache.DataList;
|
var images = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Image_Config>(_dao);
|
||||||
//标签
|
//标签
|
||||||
var labelCache = new MiaoYuDataEntityCache<T_Character_Label>(_dao, T_Character_LabelLock);
|
var labels = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Label>(_dao);
|
||||||
var labels = labelCache.DataList;
|
//标签关联表
|
||||||
var labelRelationCache = new MiaoYuDataEntityCache<T_Character_Label_Relation>(_dao, T_Character_Label_RelationLock);
|
var labelRelations = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Label_Relation>(_dao);
|
||||||
var labelRelations = labelRelationCache.DataList;
|
|
||||||
|
|
||||||
//性格
|
//性格
|
||||||
var personalityCache = new MiaoYuDataEntityCache<T_Character_Personality>(_dao, T_Character_LabelLock);
|
var personalitys = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Personality>(_dao);
|
||||||
var personalitys = personalityCache.DataList;
|
//性格关联表
|
||||||
var personalityRelationsCache = new MiaoYuDataEntityCache<T_Character_Personality_Relation>(_dao, T_Character_Label_RelationLock);
|
var personalityRelations = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Personality_Relation>(_dao);
|
||||||
var personalityRelations = personalityRelationsCache.DataList;
|
//分类
|
||||||
|
var types = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Type>(_dao); ;
|
||||||
|
//分类关联表
|
||||||
//性格
|
var typesRelations = MiaoYuCacheExtend.GetMiaoYuDataEntityCacheList<T_Character_Type_Intimacy>(_dao); ;
|
||||||
var typeCache = new MiaoYuDataEntityCache<T_Character_Type>(_dao, T_Character_LabelLock);
|
|
||||||
var types = typeCache.DataList;
|
|
||||||
var typesRelationsCache = new MiaoYuDataEntityCache<T_Character_Type_Intimacy>(_dao, T_Character_Label_RelationLock);
|
|
||||||
var typesRelations = typesRelationsCache.DataList;
|
|
||||||
|
|
||||||
foreach (var characterCache in characterCaches)
|
foreach (var characterCache in characterCaches)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -130,9 +130,10 @@ namespace HuanMeng.MiaoYu.Model.Dto.Character
|
||||||
.Replace("{label}", this.LabelStr) //标签
|
.Replace("{label}", this.LabelStr) //标签
|
||||||
.Replace("{user_sex}", this.UserSex)
|
.Replace("{user_sex}", this.UserSex)
|
||||||
;
|
;
|
||||||
|
//去除没有意义的词
|
||||||
if (_system.Contains("[是一个。]"))
|
if (_system.Contains("[是一个。]"))
|
||||||
{
|
{
|
||||||
_system = _system .Replace("[是一个。]", "");
|
_system = _system.Replace("[是一个。]", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user