最近編碼的過(guò)程中,使用了對(duì)象本地內(nèi)存緩存,緩存用了Dictionary
10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有阿魯科爾沁免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
對(duì)象的Equals相等性比較,百度、google會(huì)有一大堆實(shí)現(xiàn),幾個(gè)重點(diǎn)的點(diǎn):
1. 實(shí)現(xiàn)接口IEquatable
htt p s : / / ms d n . mi c r o s o f t . c o m /en-us/library/ms131190(v=vs.110).aspx
2.重寫(xiě)bool Equals(object other)方法和 int GetHashCode()方法
htt p : / / s ta ck o v e r f l o w . co m / q u es t i o n s /2 7 3 49 1 4/whats-the-difference-between-iequatable-and-just-overriding-object-equals
這里直接貼出來(lái)一個(gè)通用實(shí)現(xiàn),分享給大家:
1 ///2 /// 流控事件 3 /// 4 [Serializable] 5 public class FlowControlEvent: IEquatable6 { 7 public static readonly string Global = "Global"; 8 9 /// 10 /// 標(biāo)識(shí) 11 /// 12 public string ID { get; set; } 13 14 ///15 /// 流控策略名稱(chēng) 16 /// 17 public string StrategyName { get; set; } 18 19 ///20 /// 是否手工觸發(fā) 21 /// 22 public bool IsManuelTrigger { get; set; } 23 24 ///25 /// 觸發(fā)時(shí)間 26 /// 27 public DateTime TriggerTime { get; set; } 28 29 ///30 /// 流控策略 31 /// 32 public FlowControlStrategy Strategy { get; set; } 33 34 ///35 /// 持續(xù)時(shí)間,單位s 36 /// 37 public long Duration { get; set; } 38 39 //是否啟用 40 private bool isEnable = true; 41 42 ///43 /// 是否啟用 44 /// 45 public bool IsEnable 46 { 47 get 48 { 49 return isEnable; 50 } 51 set 52 { 53 isEnable = value; 54 } 55 } 56 57 ///58 /// 是否使用中 59 /// 60 public bool IsUsing 61 { 62 get 63 { 64 if (IsEnable == false) return false; 65 if (IsManuelTrigger) 66 { 67 if (Duration == long.MaxValue) 68 { 69 return true; 70 } 71 else 72 { 73 var span = DateTime.Now - TriggerTime; 74 if (span.TotalSeconds > Duration) 75 return false; 76 else 77 return true; 78 } 79 } 80 else 81 { 82 return true; 83 } 84 } 85 } 86 87 ///88 /// 創(chuàng)建時(shí)間 89 /// 90 public DateTime CreateTime { get; set; } 91 92 ///93 /// 創(chuàng)建人 94 /// 95 public string Creator { get; set; } 96 97 ///98 /// 最后修改時(shí)間 99 /// 100 public DateTime LastModifyTime { get; set; }101 102 ///103 /// 最后修改人104 /// 105 public string LastModifier { get; set; }106 107 ///108 /// 相等性比較109 /// 110 /// 要比較的對(duì)象111 ///true 相等 false 不相等 112 public override bool Equals(object other)113 {114 if (ReferenceEquals(null, other)) return false;115 if (ReferenceEquals(this, other)) return true;116 if (other.GetType() != this.GetType()) return false;117 118 return Equals((FlowControlEvent)other);119 }120 121 ///122 /// 流控事件是否等于同一類(lèi)型的另一個(gè)流控事件123 /// 124 /// 同一類(lèi)型的另一個(gè)流控事件125 ///true 相等 false 不相等 126 public bool Equals(FlowControlEvent other)127 {128 if (other == null)129 return false;130 if (!string.Equals(this.ID , other.ID) || this.IsEnable != other.IsEnable || this.Duration!= other.Duration131 || !string.Equals(this.StrategyName, other.StrategyName)||this.TriggerTime!= other.TriggerTime)132 return false;133 134 return true;135 }136 137 ///138 /// 重載GetHashCode方法139 /// 140 ///HashCode 141 public override int GetHashCode()142 {143 unchecked144 {145 var result = 0;146 result = (result * 397) ^ ID.GetHashCode();147 result = (result * 397) ^ IsEnable.GetHashCode();148 result = (result * 397) ^ Duration.GetHashCode();149 result = (result * 397) ^ StrategyName.GetHashCode();150 result = (result * 397) ^ TriggerTime.GetHashCode();151 return result;152 }153 }154 }