Skip to content

Commit 6430fd1

Browse files
committed
✨⚡️ 新增SqlSugar DbMaintenance扩展,支持清除SqlSugar表信息缓存功能
使用场景: 调用 DbMaintenance.IsAnyTable 时会缓存表的元数据信息, SqlSugar 未提供显式清理该缓存的方法。 如果通过 DbMaintenance.IsAnyTable(tableName, false) 绕过缓存, 则每次调用都会重新查询所有表信息,影响性能。 建议在InitTable后进行清理缓存.
1 parent cc8965b commit 6430fd1

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Blog.Core.Api/Blog.Core.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Text;
2+
using Blog.Core.Common.DB.Extension;
3+
using Blog.Core.Controllers;
4+
using Blog.Core.Model;
5+
using Blog.Core.Model.Models;
6+
using Microsoft.AspNetCore.Authorization;
7+
using Microsoft.AspNetCore.Mvc;
8+
using SqlSugar;
9+
10+
namespace Blog.Core.Api.Controllers;
11+
12+
/// <summary>
13+
/// SqlSugar 相关测试
14+
/// </summary>
15+
[Route("api/[controller]/[action]")]
16+
[ApiController]
17+
[AllowAnonymous]
18+
public class SqlSugarTestController(ISqlSugarClient db) : BaseApiController
19+
{
20+
/// <summary>
21+
/// 测试建表后,SqlSugar缓存
22+
/// </summary>
23+
/// <returns></returns>
24+
[HttpGet]
25+
public MessageModel ClearDbTableCache()
26+
{
27+
var tableName = "BlogArticle_Test";
28+
29+
//先删除表
30+
try
31+
{
32+
db.DbMaintenance.DropTable(tableName);
33+
db.ClearDbTableCache();
34+
}
35+
catch
36+
{
37+
//Ignore
38+
}
39+
40+
StringBuilder sb = new StringBuilder();
41+
42+
//提前检查表是否存在,测试缓存
43+
sb.AppendLine($"表{tableName} 是否存在:{db.DbMaintenance.IsAnyTable(tableName)}");
44+
45+
//创建表
46+
db.CodeFirst.As<BlogArticle>(tableName).InitTables<BlogArticle>();
47+
sb.AppendLine($"表{tableName} 创建成功");
48+
49+
//检查表是否存在
50+
sb.AppendLine($"表{tableName} 是否存在:{db.DbMaintenance.IsAnyTable(tableName)}");
51+
52+
//清除缓存
53+
db.ClearDbTableCache();
54+
sb.AppendLine($"清除缓存后");
55+
56+
//检查表是否存在
57+
sb.AppendLine($"表{tableName} 是否存在:{db.DbMaintenance.IsAnyTable(tableName)}");
58+
59+
return Success(sb.ToString());
60+
}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using SqlSugar;
2+
3+
namespace Blog.Core.Common.DB.Extension;
4+
5+
/// <summary>
6+
/// SqlSugar DbMaintenance 扩展
7+
/// </summary>
8+
public static class SqlSugarDbMaintenanceExtension
9+
{
10+
/// <summary>
11+
/// 清除 SqlSugar 中表信息的缓存。
12+
/// <para>使用场景:</para>
13+
/// <list type="bullet">
14+
/// <item>
15+
/// <description>
16+
/// 调用 <see cref="IDbMaintenance.IsAnyTable"/> 时会缓存表的元数据信息,
17+
/// SqlSugar 未提供显式清理该缓存的方法。
18+
/// </description>
19+
/// </item>
20+
/// <item>
21+
/// <description>
22+
/// 如果通过 <c>DbMaintenance.IsAnyTable(tableName, false)</c> 绕过缓存,
23+
/// 则每次调用都会重新查询所有表信息,影响性能。
24+
/// </description>
25+
/// </item>
26+
/// </list>
27+
/// 建议在InitTable后进行清理缓存.
28+
/// </summary>
29+
/// <param name="context">SqlSugar的数据库上下文实例。</param>
30+
public static void ClearDbTableCache(this ISqlSugarClient context)
31+
{
32+
var fullCacheKey = GetDbTableCacheKey(context, "DbMaintenanceProvider.GetTableInfoList" + context.CurrentConnectionConfig.ConfigId);
33+
context.CurrentConnectionConfig.ConfigureExternalServices.ReflectionInoCacheService.Remove<List<DbTableInfo>>(fullCacheKey);
34+
}
35+
36+
private static string GetDbTableCacheKey(ISqlSugarClient context, string cacheKey) =>
37+
$"{context.CurrentConnectionConfig.DbType}.{context.Ado.Connection.Database}.{cacheKey}";
38+
}

0 commit comments

Comments
 (0)