最近编码的过程中,使用了对象本地内存缓存,缓存用了Dictionary<string,object>, ConcurrentDictionary<string,oject>,还可以是MemoryCache(底层基于Hashtable)。使用缓存,肯定要处理数据变化缓存同步的问题。如何比较数据的变化,演进为新的内存对象数据和已有内存对象数据的相等性比较!
对象的Equals相等性比较,百度、google会有一大堆实现,几个重点的点:
1. 实现接口IEquatable<T>
https://msdn.microsoft.com/en-us/library/ms131190(v=vs.110).aspx
2.重写bool Equals(object other)方法和 int GetHashCode()方法
http://*.com/questions/2734914/whats-the-difference-between-iequatable-and-just-overriding-object-equals
这里直接贴出来一个通用实现,分享给大家:
/// <summary>
/// 流控事件
/// </summary>
[Serializable]
public class FlowControlEvent: IEquatable<FlowControlEvent>
{
public static readonly string Global = "Global"; /// <summary>
/// 标识
/// </summary>
public string ID { get; set; } /// <summary>
/// 流控策略名称
/// </summary>
public string StrategyName { get; set; } /// <summary>
/// 是否手工触发
/// </summary>
public bool IsManuelTrigger { get; set; } /// <summary>
/// 触发时间
/// </summary>
public DateTime TriggerTime { get; set; } /// <summary>
/// 流控策略
/// </summary>
public FlowControlStrategy Strategy { get; set; } /// <summary>
/// 持续时间,单位s
/// </summary>
public long Duration { get; set; } //是否启用
private bool isEnable = true; /// <summary>
/// 是否启用
/// </summary>
public bool IsEnable
{
get
{
return isEnable;
}
set
{
isEnable = value;
}
} /// <summary>
/// 是否使用中
/// </summary>
public bool IsUsing
{
get
{
if (IsEnable == false) return false;
if (IsManuelTrigger)
{
if (Duration == long.MaxValue)
{
return true;
}
else
{
var span = DateTime.Now - TriggerTime;
if (span.TotalSeconds > Duration)
return false;
else
return true;
}
}
else
{
return true;
}
}
} /// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; } /// <summary>
/// 创建人
/// </summary>
public string Creator { get; set; } /// <summary>
/// 最后修改时间
/// </summary>
public DateTime LastModifyTime { get; set; } /// <summary>
/// 最后修改人
/// </summary>
public string LastModifier { get; set; } /// <summary>
/// 相等性比较
/// </summary>
/// <param name="other">要比较的对象</param>
/// <returns>true 相等 false 不相等</returns>
public override bool Equals(object other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (other.GetType() != this.GetType()) return false; return Equals((FlowControlEvent)other);
} /// <summary>
/// 流控事件是否等于同一类型的另一个流控事件
/// </summary>
/// <param name="other">同一类型的另一个流控事件</param>
/// <returns>true 相等 false 不相等</returns>
public bool Equals(FlowControlEvent other)
{
if (other == null)
return false;
if (!string.Equals(this.ID , other.ID) || this.IsEnable != other.IsEnable || this.Duration!= other.Duration
|| !string.Equals(this.StrategyName, other.StrategyName)||this.TriggerTime!= other.TriggerTime)
return false; return true;
} /// <summary>
/// 重载GetHashCode方法
/// </summary>
/// <returns>HashCode</returns>
public override int GetHashCode()
{
unchecked
{
var result = ;
result = (result * ) ^ ID.GetHashCode();
result = (result * ) ^ IsEnable.GetHashCode();
result = (result * ) ^ Duration.GetHashCode();
result = (result * ) ^ StrategyName.GetHashCode();
result = (result * ) ^ TriggerTime.GetHashCode();
return result;
}
}
}
周国庆
2017/4/25