[fix] 新增修改表结构时,清空tableNames缓存,便于上层快速感知新表。完善分表自动创建的逻辑,不创建默认表,首次写入时自动创建新分表

This commit is contained in:
智能大石头 2025-02-20 15:35:33 +08:00
parent 62acf904b5
commit 6db17b1108
3 changed files with 51 additions and 19 deletions

View File

@ -86,7 +86,7 @@ public partial class DAL
public IAsyncDbSession AsyncSession => (Db as DbBase)!.CreateSessionForAsync();
private String? _mapTo;
private readonly ICache _cache = new MemoryCache();
private static ICache _cache = new MemoryCache();
#endregion
#region
@ -634,7 +634,7 @@ public partial class DAL
/// <summary>
/// 获取所有表名,带缓存,不区分大小写
/// </summary>
public ICollection<String> TableNames => _cache.GetOrAdd("tableNames", k => new HashSet<String>(GetTableNames(), StringComparer.OrdinalIgnoreCase), 60) ?? [];
public ICollection<String> TableNames => _cache.GetOrAdd($"tableNames:{ConnName}", k => new HashSet<String>(GetTableNames(), StringComparer.OrdinalIgnoreCase), 60) ?? [];
/// <summary>
/// 快速获取所有表名,无缓存,区分大小写
@ -680,7 +680,7 @@ public partial class DAL
/// <returns></returns>
public static IList<IDataTable> Import(String xml)
{
if (xml.IsNullOrEmpty()) return new IDataTable[0];
if (xml.IsNullOrEmpty()) return [];
return ModelHelper.FromXml(xml, CreateTable);
}
@ -820,6 +820,10 @@ public partial class DAL
span?.SetError(ex, null);
throw;
}
finally
{
_cache.Remove($"tableNames:{ConnName}");
}
}
private void FixIndexName(IDataTable table)

View File

@ -0,0 +1,44 @@
using System;
using System.IO;
using NewLife;
using XCode.Exceptions;
using Xunit;
using XUnitTest.XCode.TestEntity;
namespace XUnitTest.XCode.EntityTests;
public class TimeShardTests
{
[Fact]
public void Test1()
{
var file = Setting.Current.DataPath.CombinePath("test.db");
if (File.Exists(file)) File.Delete(file);
//var dal = ExpressLogs.Meta.Session.Dal;
//var table = ExpressLogs.Meta.Table.DataTable;
//dal.SetTables(table);
// 对分表查行数,报错
Assert.Throws<XSqlException>(() => ExpressLogs.FindCount());
// 分表查询,没有数据
var time = DateTime.Now.AddHours(1);
var list = ExpressLogs.Search(time.Date, time, null, null);
Assert.Empty(list);
// 写入数据
var entity = new ExpressLogs
{
Code = "123456",
};
entity.Insert();
list = ExpressLogs.Search(time.Date, time, null, null);
Assert.Single(list);
//var n = ExpressLogs.Meta.Count;
//Assert.Equal(0, n);
}
}

View File

@ -1,16 +0,0 @@
using Xunit;
using XUnitTest.XCode.TestEntity;
namespace XUnitTest.XCode;
public class TimeShardTests
{
[Fact]
public void Test1()
{
var n = ExpressLogs.Meta.Count;
Assert.Equal(0, n);
}
}