HashtableHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PMS.Plugins.Common
  9. {
  10. public static class HashtableHelper
  11. {
  12. private static Hashtable ht = Hashtable.Synchronized(new Hashtable());
  13. private static object obj = new object();
  14. /// <summary>
  15. /// 添加一个缓存项
  16. /// </summary>
  17. /// <param name="key">关键字 Key</param>
  18. /// <param name="data">数据值</param>
  19. public static void AddCacheIitems(string key, object data)
  20. {
  21. try
  22. {
  23. if (key != "")
  24. {
  25. lock (ht.SyncRoot) //zxm20140212添加
  26. {
  27. if (ht[key] != null)
  28. ht.Remove(key);
  29. ht.Add(key, data);
  30. }
  31. }
  32. }
  33. catch (Exception)
  34. {
  35. }
  36. }
  37. /// <summary>
  38. /// 获取Hashtable键值
  39. /// </summary>
  40. /// <param name="key">key</param>
  41. /// <returns></returns>
  42. public static object GetCacheIitems(string key)
  43. {
  44. if (key != "")
  45. {
  46. lock (ht.SyncRoot)
  47. {
  48. return ht[key];
  49. }
  50. }
  51. else
  52. {
  53. return null;
  54. }
  55. }
  56. /// <summary>
  57. /// 移除Hashtable键值
  58. /// </summary>
  59. /// <param name="key">key</param>
  60. public static void RemoveCacheItems(string key)
  61. {
  62. if (key != "")
  63. {
  64. lock (ht.SyncRoot)
  65. {
  66. ht.Remove(key);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 移除所有缓存值
  72. /// </summary>
  73. public static void RemoveAllCacheItems()
  74. {
  75. lock (ht.SyncRoot)
  76. {
  77. ht.Clear();
  78. }
  79. }
  80. public static string GetHashtableCount(string key)
  81. {
  82. string r = "【insureSys:" + ht[key];
  83. return r + "|Hashtable键值总和:" + ht.Count + "】";
  84. }
  85. /// <summary>
  86. /// 批量移除缓存
  87. /// </summary>
  88. /// <param name="key"></param>
  89. public static void BatchRemoveCacheItems(string key)
  90. {
  91. lock (ht.SyncRoot)
  92. {
  93. foreach (DictionaryEntry h in ht)
  94. {
  95. string keyName = h.Key.ToStringEx();
  96. if (keyName.Contains(key))
  97. {
  98. ht.Remove(keyName);
  99. }
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// 将DataTable 转换hashTable
  105. /// </summary>
  106. /// <param name="dt"></param>
  107. /// <returns></returns>
  108. public static Hashtable DataTableToHashTable(DataTable dt)
  109. {
  110. DataRow row = dt.Rows[0];
  111. Hashtable hash = new Hashtable();
  112. for (int i = 0; i < row.Table.Columns.Count; i++)
  113. {
  114. if (row[row.Table.Columns[i].ColumnName].ToStringEx() != "")
  115. hash.Add(row.Table.Columns[i].ColumnName, row[row.Table.Columns[i].ColumnName].ToStringEx());
  116. }
  117. return hash;
  118. }
  119. /// <summary>
  120. /// 获取一个6位验证码随机数
  121. /// </summary>
  122. /// <returns></returns>
  123. private static int NewRandom()
  124. {
  125. lock (obj)
  126. {
  127. Random r = new Random(System.Environment.TickCount);
  128. int i = r.Next(100000, 999999);
  129. return i;
  130. }
  131. }
  132. /// <summary>
  133. /// 新生成一个Key
  134. /// </summary>
  135. /// <returns></returns>
  136. public static string NewKey()
  137. {
  138. string guid = NewRandom().ToStringEx();//Guid.NewGuid().ToString().ToUpper();
  139. return guid;
  140. }
  141. }
  142. }