Skip to content

Commit

Permalink
✨调整SqlSugar缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNoCry committed Dec 3, 2024
1 parent d79ca4e commit d0b029f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 72 deletions.
7 changes: 6 additions & 1 deletion Blog.Core.Common/Caches/SqlSugarCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
namespace Blog.Core.Common.Caches;

/// <summary>
/// 实现SqlSugar的ICacheService接口
/// 实现SqlSugar的ICacheService接口<br/>
/// <br/>
/// 建议自己实现业务缓存,注入ICaching直接用即可<br/>
/// <br/>
/// 不建议使用SqlSugar缓存,性能有很大问题,会导致redis堆积<br/>
/// 核心问题在于SqlSugar,每次query(注意:不管你有没有启用,所有表的查询)都会查缓存, insert\update\delete,又会频繁GetAllKey,导致性能特别低<br/>
/// </summary>
public class SqlSugarCacheService : ICacheService
{
Expand Down
28 changes: 28 additions & 0 deletions Blog.Core.Common/Option/AppSettingsOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Blog.Core.Common.Option.Core;

namespace Blog.Core.Common.Option;

public class AppSettingsOption : IConfigurableOptions
{
/// <summary>
/// 迁移表结构
/// </summary>
public bool MigrateDBEnabled { get; set; }

/// <summary>
/// 初始化数据
/// </summary>
public bool SeedDBEnabled { get; set; }

/// <summary>
/// 生成测试数据
/// </summary>
public bool TestSeedDbEnabled { get; set; }

public string Author { get; set; }

/// <summary>
/// 是否启用数据库二级缓存
/// </summary>
public bool CacheDbEnabled { get; set; } = false;
}
68 changes: 0 additions & 68 deletions Blog.Core.Extensions/ServiceExtensions/HttpRuntimeCache.cs

This file was deleted.

9 changes: 6 additions & 3 deletions Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SqlSugar;
using Blog.Core.Common.Caches;
using System.Text.RegularExpressions;
using Blog.Core.Common.Option;
using Blog.Core.Common.Utility;

namespace Blog.Core.Extensions
Expand Down Expand Up @@ -36,7 +37,7 @@ public static void AddSqlsugarSetup(this IServiceCollection services)
{
ConfigId = m.ConnId.ObjToString().ToLower(),
ConnectionString = m.Connection,
DbType = (DbType) m.DbType,
DbType = (DbType)m.DbType,
IsAutoCloseConnection = true,
// Check out more information: https://github.com/anjoy8/Blog.Core/issues/122
//IsShardSameThread = false,
Expand All @@ -55,6 +56,8 @@ public static void AddSqlsugarSetup(this IServiceCollection services)
// 自定义特性
ConfigureExternalServices = new ConfigureExternalServices()
{
//不建议使用,性能有很大问题,会导致redis堆积
//核心问题在于SqlSugar,每次query都会查缓存, insert\update\delete,又会频繁GetAllKey,导致性能特别低
DataInfoCacheService = new SqlSugarCacheService(),
EntityService = (property, column) =>
{
Expand All @@ -73,7 +76,7 @@ public static void AddSqlsugarSetup(this IServiceCollection services)
else
{
if (string.Equals(config.ConfigId.ToString(), MainDb.CurrentDbConnId,
StringComparison.CurrentCultureIgnoreCase))
StringComparison.CurrentCultureIgnoreCase))
{
BaseDBConfig.MainConfig = config;
}
Expand Down Expand Up @@ -103,7 +106,7 @@ public static void AddSqlsugarSetup(this IServiceCollection services)
{
BaseDBConfig.ValidConfig.ForEach(config =>
{
var dbProvider = db.GetConnectionScope((string) config.ConfigId);
var dbProvider = db.GetConnectionScope((string)config.ConfigId);

// 打印SQL语句
dbProvider.Aop.OnLogExecuting = (s, parameters) =>
Expand Down

0 comments on commit d0b029f

Please sign in to comment.