From 6db17b110889d31ec30867033cbb10e304d19c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 20 Feb 2025 15:35:33 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E6=96=B0=E5=A2=9E=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A1=A8=E7=BB=93=E6=9E=84=E6=97=B6=EF=BC=8C=E6=B8=85=E7=A9=BA?= =?UTF-8?q?tableNames=E7=BC=93=E5=AD=98=EF=BC=8C=E4=BE=BF=E4=BA=8E?= =?UTF-8?q?=E4=B8=8A=E5=B1=82=E5=BF=AB=E9=80=9F=E6=84=9F=E7=9F=A5=E6=96=B0?= =?UTF-8?q?=E8=A1=A8=E3=80=82=E5=AE=8C=E5=96=84=E5=88=86=E8=A1=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BA=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=88=9B=E5=BB=BA=E9=BB=98=E8=AE=A4=E8=A1=A8=EF=BC=8C?= =?UTF-8?q?=E9=A6=96=E6=AC=A1=E5=86=99=E5=85=A5=E6=97=B6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=B0=E5=88=86=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode/DataAccessLayer/DAL.cs | 10 +++-- XUnitTest.XCode/EntityTests/TimeShardTests.cs | 44 +++++++++++++++++++ XUnitTest.XCode/TimeShardTests.cs | 16 ------- 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 XUnitTest.XCode/EntityTests/TimeShardTests.cs delete mode 100644 XUnitTest.XCode/TimeShardTests.cs diff --git a/XCode/DataAccessLayer/DAL.cs b/XCode/DataAccessLayer/DAL.cs index a56e3a98e..2c71e700d 100644 --- a/XCode/DataAccessLayer/DAL.cs +++ b/XCode/DataAccessLayer/DAL.cs @@ -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 /// /// 获取所有表名,带缓存,不区分大小写 /// - public ICollection TableNames => _cache.GetOrAdd("tableNames", k => new HashSet(GetTableNames(), StringComparer.OrdinalIgnoreCase), 60) ?? []; + public ICollection TableNames => _cache.GetOrAdd($"tableNames:{ConnName}", k => new HashSet(GetTableNames(), StringComparer.OrdinalIgnoreCase), 60) ?? []; /// /// 快速获取所有表名,无缓存,区分大小写 @@ -680,7 +680,7 @@ public partial class DAL /// public static IList 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) diff --git a/XUnitTest.XCode/EntityTests/TimeShardTests.cs b/XUnitTest.XCode/EntityTests/TimeShardTests.cs new file mode 100644 index 000000000..0dafcfe85 --- /dev/null +++ b/XUnitTest.XCode/EntityTests/TimeShardTests.cs @@ -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(() => 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); + } +} diff --git a/XUnitTest.XCode/TimeShardTests.cs b/XUnitTest.XCode/TimeShardTests.cs deleted file mode 100644 index 3ca4e8816..000000000 --- a/XUnitTest.XCode/TimeShardTests.cs +++ /dev/null @@ -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); - } -}