using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AsiaINFO.SMS.BusinessFactory { public class CheckRepeat { private static CheckRepeat _instance = null; public static CheckRepeat Instance { get { if (CheckRepeat._instance == null) { CheckRepeat._instance = new CheckRepeat(); } return _instance; } } private CheckRepeat() { } private Dictionary> _hasSubmit = new Dictionary>(); public bool checkMessageId(string msgId) { int curKey = this.getCurKey(); int[] checkKeys = getCheckKeys(curKey); this.removeDict(curKey); foreach (int key in checkKeys) { if (this._hasSubmit.ContainsKey(key) && this._hasSubmit[key] != null) { if (this._hasSubmit[key].ContainsKey(msgId)) return true; } } return false; } private int getCurKey() { DateTime now = DateTime.Now; int min = (int.Parse(now.ToString("mm")) / 10);//0,1,2,3,4,5 return min; } public void addSubmitMessageToCache(string msgId) { int curKey = this.getCurKey(); if (this._hasSubmit.ContainsKey(curKey) && this._hasSubmit[curKey] != null) { Dictionary keys = this._hasSubmit[curKey]; keys.Add(msgId, true); } else if (this._hasSubmit.ContainsKey(curKey)) { Dictionary keys = new Dictionary(); keys.Add(msgId, true); this._hasSubmit[curKey] = keys; } else { Dictionary keys = new Dictionary(); keys.Add(msgId, true); this._hasSubmit.Add(curKey, keys); } DateTime now = DateTime.Now; int min = (int.Parse(now.ToString("mm")) / 10); //log4netService.Debug("addSubmitMessageToCache成功,总数:" + _hasSubmitMobile[min].Count); } private int[] getCheckKeys(int curKey) { if (curKey == 0) { return new int[] { 0, curKey + 5 }; } return new int[] { curKey, curKey - 1 }; } private bool isDelKey(int curKey, int delKey) { if (curKey == delKey) return false; if (curKey == 0 && delKey == 5) { return false; } if (delKey == curKey - 1) return false; //log4netService.Debug("移除成功,curKey=" + curKey); return true; } private void removeDict(int curKey) { List keys = new List(); foreach (int key in this._hasSubmit.Keys) { if (key == curKey) { continue; } if (this.isDelKey(curKey, key)) { keys.Add(key); //keys[keys.Count] = key; //this._hasSubmitMobile[key] = null; } } for (int i = 0; i < keys.Count; i++) { int key = keys[i]; this._hasSubmit[key] = null; } //log4netService.Debug("removeDict正常:" + keys.Count); } } }