From 44ef16c98067e8d3454f8546e01fb3d45934531f 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: Tue, 22 Jul 2025 22:45:43 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E6=89=B9=E9=87=8F=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=90=8E=EF=BC=8C=E9=9C=80=E8=A6=81=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=B8=85=E7=A9=BA=E7=BC=93=E5=AD=98=E3=80=82fix:=20ht?= =?UTF-8?q?tps://github.com/NewLifeX/NewLife.XCode/issues/61?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode/Cache/SingleEntityCache.cs | 5 +++-- XCode/Entity/EntityExtension.cs | 22 +++++++++++++++++++--- XCode/Entity/EntitySession.cs | 2 +- XCode/Entity/IEntityOperate.cs | 15 ++++++--------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/XCode/Cache/SingleEntityCache.cs b/XCode/Cache/SingleEntityCache.cs index 1c3c176ab..f978aaec1 100644 --- a/XCode/Cache/SingleEntityCache.cs +++ b/XCode/Cache/SingleEntityCache.cs @@ -456,6 +456,7 @@ public class SingleEntityCache : CacheBase, ISingleEntit { if (entity == null) return; if (GetKeyMethod == null) return; + var key = GetKeyMethod(entity); RemoveKey(key); } @@ -472,10 +473,10 @@ public class SingleEntityCache : CacheBase, ISingleEntit if (es == null) return; // 不要清空单对象缓存,而是设为过期 - var now = TimerX.Now; + var exp = TimerX.Now.AddSeconds(-1); foreach (var item in es) { - item.Value.ExpireTime = now.AddSeconds(-1); + item.Value.ExpireTime = exp; } Using = false; diff --git a/XCode/Entity/EntityExtension.cs b/XCode/Entity/EntityExtension.cs index 113f6e06c..5df6d8667 100644 --- a/XCode/Entity/EntityExtension.cs +++ b/XCode/Entity/EntityExtension.cs @@ -20,7 +20,6 @@ public static class EntityExtension /// 实体列表 /// 作为Value部分的字段,默认为空表示整个实体对象为值 /// - //[Obsolete("将来不再支持实体列表,请改用Linq")] public static IDictionary ToDictionary(this IEnumerable list, String? valueField = null) where T : IEntity { if (list == null || !list.Any()) return new Dictionary(); @@ -128,7 +127,7 @@ public static class EntityExtension var session2 = session ?? fact.Session; // Oracle/MySql批量插入 - if (session2.Dal.SupportBatch && list2.Count() > 1) + if (session2.Dal.SupportBatch && list2.Count > 1) { //DefaultSpan.Current?.AppendTag("SupportBatch"); @@ -392,6 +391,9 @@ public static class EntityExtension count = DoAction(list2, func, count); } + // 统一清空缓存,避免因事务回滚等原因导致缓存数据不一致 + session?.ClearCache(func + "", true); + return count; } @@ -524,6 +526,8 @@ public static class EntityExtension } } + session.ClearCache(nameof(BatchInsert), true); + return rs; } catch (Exception ex) @@ -614,6 +618,8 @@ public static class EntityExtension } } + session.ClearCache(nameof(BatchInsertIgnore), true); + return rs; } catch (Exception ex) @@ -704,6 +710,8 @@ public static class EntityExtension } } + session.ClearCache(nameof(BatchReplace), true); + return rs; } catch (Exception ex) @@ -797,6 +805,8 @@ public static class EntityExtension } } + session.ClearCache(nameof(BatchUpdate), true); + return rs; } catch (Exception ex) @@ -941,6 +951,8 @@ public static class EntityExtension } } + session.ClearCache(nameof(BatchUpsert), true); + return rs; } catch (Exception ex) @@ -1018,7 +1030,11 @@ public static class EntityExtension { if (span != null) span.Tag = $"{session.TableName}[{entity}]"; - return dal.Session.Upsert(session.DataTable, option.Columns, option.UpdateColumns, option.AddColumns, [entity as IModel]); + var rs = dal.Session.Upsert(session.DataTable, option.Columns, option.UpdateColumns, option.AddColumns, [entity]); + + session.ClearCache(nameof(Upsert), true); + + return rs; } catch (Exception ex) { diff --git a/XCode/Entity/EntitySession.cs b/XCode/Entity/EntitySession.cs index a9a919426..3ac5b2dac 100644 --- a/XCode/Entity/EntitySession.cs +++ b/XCode/Entity/EntitySession.cs @@ -530,7 +530,7 @@ public class EntitySession : DisposeBase, IEntitySession where TEntity // Count提供的是非精确数据,避免频繁更新 //_Count = -1L; - //_NextCount = DateTime.MinValue; + _NextCount = DateTime.MinValue; } String CacheKey => $"{ConnName}_{TableName}_{ThisType.Name}"; diff --git a/XCode/Entity/IEntityOperate.cs b/XCode/Entity/IEntityOperate.cs index 180f70273..cf3fc2101 100644 --- a/XCode/Entity/IEntityOperate.cs +++ b/XCode/Entity/IEntityOperate.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Data; +using System.Data; using NewLife.Data; using XCode.Configuration; using XCode.Shards; @@ -9,14 +7,13 @@ using XCode.Statistics; namespace XCode; /// 指定实体工厂 -public class EntityFactoryAttribute : Attribute +/// 指定实体工厂 +/// +[AttributeUsage(AttributeTargets.Class)] +public class EntityFactoryAttribute(Type type) : Attribute { /// 实体工厂类型 - public Type Type { get; set; } - - /// 指定实体工厂 - /// - public EntityFactoryAttribute(Type type) => Type = type; + public Type Type { get; set; } = type; } /// 数据实体操作接口