添加角色实体类缓存

This commit is contained in:
zpc 2024-07-13 23:00:01 +08:00
parent d19afd6e8a
commit 2268c115e3
11 changed files with 368 additions and 18 deletions

View File

@ -14,6 +14,7 @@ using HuanMeng.MiaoYu.Model.Dto;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using System;
@ -30,6 +31,7 @@ namespace HuanMeng.MiaoYu.Code.Base
/// </summary>
public class MiaoYuBase : BLLBase<DAO>
{
public MiaoYuBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
@ -230,12 +232,28 @@ namespace HuanMeng.MiaoYu.Code.Base
if (_miaoYuCache == null)
{
//new MiaoYuDataEntityCache<T_Character>(Dao).DataList
_miaoYuCache = new MiaoYuCache(Dao);
_miaoYuCache = new MiaoYuCache(Dao, Mapper);
}
return _miaoYuCache;
}
}
#endregion
#region
private Logger<MiaoYuBase>? _logger;
public Logger<MiaoYuBase> _Logger
{
get
{
if (_logger == null)
{
_logger = _serviceProvider.GetRequiredService<Logger<MiaoYuBase>>();
}
return _logger;
}
}
#endregion
}
}

View File

@ -1,12 +1,17 @@
using AutoMapper;
using HuanMeng.MiaoYu.Code.Cache.Special;
using HuanMeng.MiaoYu.Model.Dto.Character;
namespace HuanMeng.MiaoYu.Code.Cache
{
/// <summary>
/// 缓存类
/// </summary>
/// <param name="dao"></param>
public partial class MiaoYuCache(DAO dao)
public partial class MiaoYuCache(DAO dao, IMapper mapper)
{
#region
/// <summary>
@ -34,5 +39,53 @@ namespace HuanMeng.MiaoYu.Code.Cache
}
#endregion
#region
/// <summary>
/// 模型缓存表
/// </summary>
private static object ModelConfigLock = new object();
/// <summary>
/// 模型缓存表
/// </summary>
public MiaoYuDataEntityCache<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, ModelConfigLock);
}
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
}
}

View File

@ -3,7 +3,7 @@
namespace HuanMeng.MiaoYu.Code.Cache
{
/// <summary>
/// 妙语数据库实体类缓存
/// 妙语数据库实体类缓存
/// </summary>
public class MiaoYuDataEntityCache<T>(DAO _dao, object lockObj, int cacheTime = 36000)
: CommonDataEntityCache<T>(lockObj, cacheTime) where T : class

View File

@ -0,0 +1,58 @@
using AutoMapper;
using HuanMeng.MiaoYu.Code.DataAccess;
using HuanMeng.MiaoYu.Model.Dto.Character;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Code.Cache.Special
{
/// <summary>
///
/// </summary>
/// <param name="_dao"></param>
public class CharacterEntityCache(DAO _dao, IMapper mapper) : MiaoYuDataEntityCache<CharacterCache>(_dao, CharacterCacheLock)
{
/// <summary>
/// 锁
/// </summary>
private static object CharacterCacheLock = new object();
/// <summary>
/// 获取缓存数据
/// </summary>
/// <returns></returns>
public override List<CharacterCache> GetDataList()
{
List<CharacterCache> characterCaches = null;
//读取角色表
var characters = _dao.daoDbMiaoYu.context.T_Character.ToList();
if (characters == null)
{
return new List<CharacterCache>();
}
characterCaches = mapper.Map<List<CharacterCache>>(characters);
//查询配置表
var modelConfigs = _dao.daoDbMiaoYu.context.T_Model_Config.ToList();
foreach (var characterCache in characterCaches)
{
var modelConfig = modelConfigs.FirstOrDefault(it => it.Id == characterCache.ModelConfigId);
if (modelConfig != null)
{
characterCache.ModelConfig = modelConfig;
}
}
//移除没有配置模型的类
characterCaches.Where(it => it.ModelConfig == null).ToList().ForEach(characterCache =>
{
characterCaches.Remove(characterCache);
});
return characterCaches;
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Code.Character
{
/// <summary>
/// 角色信息类
/// </summary>
public class CharacterBLL : MiaoYuBase
{
public CharacterBLL(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Task<object> GetCharacter(int index)
{
var charactersList = MiaoYuCache.CharactersList.Where(it => it.Visibility).ToList();
if (charactersList.Count == 0)
{
return null;
}
return null;
}
}
}

View File

@ -68,11 +68,21 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
/// </summary>
public virtual DbSet<T_Chat> T_Chat { get; set; }
/// <summary>
///
/// </summary>
public virtual DbSet<T_Model_Config> T_Model_Config { get; set; }
/// <summary>
/// 用户表
/// </summary>
public virtual DbSet<T_User> T_User { get; set; }
/// <summary>
///
/// </summary>
public virtual DbSet<T_User_Char> T_User_Char { get; set; }
/// <summary>
/// 用户信息表
/// </summary>
@ -109,20 +119,15 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.Gender).HasComment("性别0男1女2其他");
entity.Property(e => e.Model)
.HasMaxLength(255)
.IsUnicode(false)
.HasComment("人物模型");
entity.Property(e => e.ModelConfigId).HasComment("模型Id");
entity.Property(e => e.Name)
.HasMaxLength(50)
.HasComment("人物名字");
entity.Property(e => e.Prologue)
.HasMaxLength(255)
.HasComment("开场白");
entity.Property(e => e.Style).HasComment("风格id");
entity.Property(e => e.System)
.HasMaxLength(255)
.IsUnicode(false)
.HasMaxLength(1000)
.HasComment("人物初始设定");
entity.Property(e => e.TenantId).HasComment("租户Id");
entity.Property(e => e.UpdateTime)
@ -280,6 +285,40 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
}
});
modelBuilder.Entity<T_Model_Config>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__T_Model___3214EC074121E040");
entity.Property(e => e.AnthropicVersion)
.HasMaxLength(255)
.HasComment("模型版本 anthropic-version");
entity.Property(e => e.ApiKey)
.HasMaxLength(255)
.HasComment("模型key x-api-key");
entity.Property(e => e.CreateTime)
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.MaxTokens).HasComment("模型运行最大的max_tokens");
entity.Property(e => e.Model)
.HasMaxLength(50)
.HasComment("模型model");
entity.Property(e => e.ModelName)
.HasMaxLength(50)
.HasComment("模型名称");
entity.Property(e => e.TenantId).HasComment("租户");
entity.Property(e => e.UpdateTime)
.HasComment("修改时间")
.HasColumnType("datetime");
entity.Property(e => e.Url)
.HasMaxLength(255)
.HasComment("模型请求地址");
//添加全局筛选器
if (this.TenantInfo != null)
{
entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId);
}
});
modelBuilder.Entity<T_User>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__T_User__3214EC073733108B");
@ -323,6 +362,30 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
}
});
modelBuilder.Entity<T_User_Char>(entity =>
{
entity.Property(e => e.CharacterId).HasComment("角色Id");
entity.Property(e => e.CreateDateTime)
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.IsEnable).HasComment("是否启用");
entity.Property(e => e.ModelConfigId).HasComment("使用模型Id");
entity.Property(e => e.SessionId).HasComment("会话Id");
entity.Property(e => e.SessionName)
.HasMaxLength(50)
.HasComment("会话名称");
entity.Property(e => e.TenantId).HasComment("租户");
entity.Property(e => e.UpdateDateTime)
.HasComment("修改时间")
.HasColumnType("datetime");
entity.Property(e => e.UserId).HasComment("用户Id");
//添加全局筛选器
if (this.TenantInfo != null)
{
entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId);
}
});
modelBuilder.Entity<T_User_Data>(entity =>
{
entity.HasKey(e => new { e.Id, e.UserId }).HasName("PK__T_User_D__E36C60C3D959FD89");

View File

@ -29,9 +29,9 @@ public partial class T_Character: MultiTenantEntity
public string Prologue { get; set; } = null!;
/// <summary>
/// 风格id
/// 模型Id
/// </summary>
public int? Style { get; set; }
public int ModelConfigId { get; set; }
/// <summary>
/// 公开/私密 0公开 1私密
@ -53,11 +53,6 @@ public partial class T_Character: MultiTenantEntity
/// </summary>
public int Gender { get; set; }
/// <summary>
/// 人物模型
/// </summary>
public string? Model { get; set; }
/// <summary>
/// 人物初始设定
/// </summary>

View File

@ -0,0 +1,49 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
public partial class T_Model_Config: MultiTenantEntity
{
public int Id { get; set; }
/// <summary>
/// 模型名称
/// </summary>
public string ModelName { get; set; } = null!;
/// <summary>
/// 模型model
/// </summary>
public string Model { get; set; } = null!;
/// <summary>
/// 模型运行最大的max_tokens
/// </summary>
public int MaxTokens { get; set; }
/// <summary>
/// 模型key x-api-key
/// </summary>
public string ApiKey { get; set; } = null!;
/// <summary>
/// 模型请求地址
/// </summary>
public string Url { get; set; } = null!;
/// <summary>
/// 模型版本 anthropic-version
/// </summary>
public string AnthropicVersion { get; set; } = null!;
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime UpdateTime { get; set; }
}

View File

@ -0,0 +1,49 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
public partial class T_User_Char: MultiTenantEntity
{
public int Id { get; set; }
/// <summary>
/// 会话Id
/// </summary>
public Guid SessionId { get; set; }
/// <summary>
/// 会话名称
/// </summary>
public string SessionName { get; set; } = null!;
/// <summary>
/// 角色Id
/// </summary>
public int CharacterId { get; set; }
/// <summary>
/// 使用模型Id
/// </summary>
public int ModelConfigId { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateDateTime { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime UpdateDateTime { get; set; }
/// <summary>
/// 是否启用
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 用户Id
/// </summary>
public int UserId { get; set; }
}

View File

@ -0,0 +1,29 @@
using AutoMapper;
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Model.Dto.Character
{
/// <summary>
/// 模型
/// </summary>
[AutoMap(typeof(T_Character))]
public class CharacterCache : T_Character
{
/// <summary>
/// 模型配置
/// </summary>
public T_Model_Config ModelConfig { get; set; }
/// <summary>
/// 多少人聊天过
/// </summary>
public int UseChatCount { get; set; }
}
}

View File

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>