using HuizhongLibrary.Log; using System; using System.Collections.Generic; using System.Text; namespace HuizhongLibrary { #region 消息缓存池 public class StringPool { Queue argsPool; public StringPool() { argsPool = new Queue(); } #region 新增对象 public void CheckIn(string item) { if (this.Contains(item) == true) return; lock (argsPool) { try { argsPool.Enqueue(item); } catch (Exception ex) { ErrorFollow.TraceWrite("StringPool.CheckIn", ex.StackTrace, ex.Message); } } } #endregion #region 返回对象 public string CheckOut() { lock (argsPool) { try { if (argsPool.Count == 0) return null; return argsPool.Dequeue(); } catch (Exception ex) { ErrorFollow.TraceWrite("StringPool.CheckOut", ex.StackTrace, ex.Message); } return null; } } #endregion #region 返回缓存数量 public int Available { get { lock (argsPool) { try { return argsPool.Count; } catch (Exception ex) { ErrorFollow.TraceWrite("StringPool.Available", ex.StackTrace, ex.Message); } return 0; } } } #endregion #region 移除 public void Clear() { lock (argsPool) { try { argsPool.Clear(); } catch (Exception ex) { ErrorFollow.TraceWrite("stringPool.Clear", ex.StackTrace, ex.Message); } } } #endregion #region 返回但不移除对象 public string Peek() { lock (argsPool) { try { if (argsPool.Count == 0) return null; return argsPool.Peek(); } catch (Exception ex) { ErrorFollow.TraceWrite("StringPool.Peek", ex.StackTrace, ex.Message); } return null; } } #endregion #region 移除 public void Remove(string item) { for (int i = 0; i < argsPool.Count; i++) { string s=CheckOut(); if (s == item) continue; CheckIn(s); } } #endregion #region 是否已经存在 public bool Contains(string item) { lock (argsPool) { try { for (int i = 0; i < argsPool.Count; i++) { string s = CheckOut(); if (s == item) return true; CheckIn(s); } } catch (Exception ex) { ErrorFollow.TraceWrite("stringPool.Clear", ex.StackTrace, ex.Message); } } return false; } #endregion } #endregion }