[fix] 批量操作数据后,需要统一清空缓存。fix: https://github.com/NewLifeX/NewLife.XCode/issues/61

This commit is contained in:
智能大石头 2025-07-22 22:45:43 +08:00
parent 85485dab32
commit 44ef16c980
4 changed files with 29 additions and 15 deletions

View File

@ -456,6 +456,7 @@ public class SingleEntityCache<TKey, TEntity> : CacheBase<TEntity>, ISingleEntit
{
if (entity == null) return;
if (GetKeyMethod == null) return;
var key = GetKeyMethod(entity);
RemoveKey(key);
}
@ -472,10 +473,10 @@ public class SingleEntityCache<TKey, TEntity> : CacheBase<TEntity>, 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;

View File

@ -20,7 +20,6 @@ public static class EntityExtension
/// <param name="list">实体列表</param>
/// <param name="valueField">作为Value部分的字段默认为空表示整个实体对象为值</param>
/// <returns></returns>
//[Obsolete("将来不再支持实体列表请改用Linq")]
public static IDictionary ToDictionary<T>(this IEnumerable<T> list, String? valueField = null) where T : IEntity
{
if (list == null || !list.Any()) return new Dictionary<String, String>();
@ -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)
{

View File

@ -530,7 +530,7 @@ public class EntitySession<TEntity> : DisposeBase, IEntitySession where TEntity
// Count提供的是非精确数据避免频繁更新
//_Count = -1L;
//_NextCount = DateTime.MinValue;
_NextCount = DateTime.MinValue;
}
String CacheKey => $"{ConnName}_{TableName}_{ThisType.Name}";

View File

@ -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;
/// <summary>指定实体工厂</summary>
public class EntityFactoryAttribute : Attribute
/// <remarks>指定实体工厂</remarks>
/// <param name="type"></param>
[AttributeUsage(AttributeTargets.Class)]
public class EntityFactoryAttribute(Type type) : Attribute
{
/// <summary>实体工厂类型</summary>
public Type Type { get; set; }
/// <summary>指定实体工厂</summary>
/// <param name="type"></param>
public EntityFactoryAttribute(Type type) => Type = type;
public Type Type { get; set; } = type;
}
/// <summary>数据实体操作接口</summary>