SecurityCounter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. namespace Top.Api.Security
  5. {
  6. /// <summary>
  7. /// 计数器
  8. /// </summary>
  9. public class SecurityCounter : SecurityConstants
  10. {
  11. private static readonly object Lock = new object();
  12. private static readonly IDictionary<string, Counter> AppCounter = new Dictionary<string, Counter>();
  13. private static readonly IDictionary<string, IDictionary<string, Counter>> AppUserCounter = new Dictionary<string, IDictionary<string, Counter>>();
  14. private string appkey;
  15. private static readonly object InitLock = new object();
  16. public SecurityCounter(string appkey)
  17. {
  18. this.appkey = appkey;
  19. lock (InitLock)
  20. {
  21. Counter appCounter = GetAppCounter(appkey);
  22. if (appCounter == null)
  23. {
  24. appCounter = new Counter();
  25. AppCounter.Add(appkey, appCounter);
  26. }
  27. IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
  28. if (userCounter == null)
  29. {
  30. userCounter = new Dictionary<string, Counter>();
  31. AppUserCounter.Add(appkey, userCounter);
  32. }
  33. }
  34. }
  35. public static Counter GetAppCounter(string appkey)
  36. {
  37. Counter appCounter = null;
  38. AppCounter.TryGetValue(appkey, out appCounter);
  39. return appCounter;
  40. }
  41. public static IDictionary<string, Counter> GetUserCounter(string appkey)
  42. {
  43. IDictionary<string, Counter> userCounter = null;
  44. AppUserCounter.TryGetValue(appkey, out userCounter);
  45. return userCounter;
  46. }
  47. public static void AddEncryptCount(string type, Counter counter)
  48. {
  49. if (counter == null)
  50. {
  51. return;
  52. }
  53. if (PHONE.Equals(type))
  54. {
  55. Interlocked.Increment(ref counter.EncryptPhoneNum);
  56. }
  57. else if (NICK.Equals(type))
  58. {
  59. Interlocked.Increment(ref counter.EncryptNickNum);
  60. }
  61. else if (RECEIVER_NAME.Equals(type))
  62. {
  63. Interlocked.Increment(ref counter.EncryptReceiverNameNum);
  64. }
  65. else if (SIMPLE.Equals(type))
  66. {
  67. Interlocked.Increment(ref counter.EncryptSimpleNum);
  68. }
  69. else if (SEARCH.Equals(type))
  70. {
  71. Interlocked.Increment(ref counter.EncryptSearchNum);
  72. }
  73. }
  74. public static void AddDecryptCount(string type, Counter counter)
  75. {
  76. if (counter == null)
  77. {
  78. return;
  79. }
  80. if (PHONE.Equals(type))
  81. {
  82. Interlocked.Increment(ref counter.DecryptPhoneNum);
  83. }
  84. else if (NICK.Equals(type))
  85. {
  86. Interlocked.Increment(ref counter.DecryptNickNum);
  87. }
  88. else if (RECEIVER_NAME.Equals(type))
  89. {
  90. Interlocked.Increment(ref counter.DecryptReceiverNameNum);
  91. }
  92. else if (SIMPLE.Equals(type))
  93. {
  94. Interlocked.Increment(ref counter.DecryptSimpleNum);
  95. }
  96. else if (SEARCH.Equals(type))
  97. {
  98. Interlocked.Increment(ref counter.DecryptSearchNum);
  99. }
  100. }
  101. public static void AddSearchCount(string type, Counter counter)
  102. {
  103. if (counter == null)
  104. {
  105. return;
  106. }
  107. if (PHONE.Equals(type))
  108. {
  109. Interlocked.Increment(ref counter.SearchPhoneNum);
  110. }
  111. else if (NICK.Equals(type))
  112. {
  113. Interlocked.Increment(ref counter.SearchNickNum);
  114. }
  115. else if (RECEIVER_NAME.Equals(type))
  116. {
  117. Interlocked.Increment(ref counter.SearchReceiverNameNum);
  118. }
  119. else if (SIMPLE.Equals(type))
  120. {
  121. Interlocked.Increment(ref counter.SearchSimpleNum);
  122. }
  123. else if (SEARCH.Equals(type))
  124. {
  125. Interlocked.Increment(ref counter.SearchSearchNum);
  126. }
  127. }
  128. public void AddEncryptCount(string type, string session)
  129. {
  130. AddEncryptCount(type, GetCounter(session));
  131. }
  132. public void AddDecryptCount(string type, string session)
  133. {
  134. AddDecryptCount(type, GetCounter(session));
  135. }
  136. public void AddSearchCount(string type, string session)
  137. {
  138. AddSearchCount(type, GetCounter(session));
  139. }
  140. public static void CleanUserCounter(string appkey)
  141. {
  142. IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
  143. if (userCounter != null)
  144. {
  145. lock (Lock)
  146. {
  147. userCounter.Clear();
  148. }
  149. }
  150. }
  151. public static IDictionary<string, Counter> CloneUserCounter(string appkey)
  152. {
  153. IDictionary<string, Counter> targetDictionary = new Dictionary<string, Counter>();
  154. IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
  155. if (userCounter == null)
  156. {
  157. return targetDictionary;
  158. }
  159. lock (Lock)
  160. {
  161. foreach (KeyValuePair<string, Counter> currentPair in userCounter)
  162. {
  163. targetDictionary.Add(currentPair.Key, currentPair.Value);
  164. }
  165. }
  166. return targetDictionary;
  167. }
  168. private Counter GetCounter(string session)
  169. {
  170. Counter counter = null;
  171. if (session == null)
  172. {
  173. counter = GetAppCounter(appkey);
  174. }
  175. else
  176. {
  177. IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
  178. if (userCounter == null)
  179. {
  180. return null;
  181. }
  182. lock (Lock)
  183. {
  184. userCounter.TryGetValue(session, out counter);
  185. if (counter == null)
  186. {
  187. counter = new Counter();
  188. userCounter.Add(session, counter);
  189. }
  190. }
  191. }
  192. return counter;
  193. }
  194. }
  195. }