ExpiringKey.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * - Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * - Neither the name of the openmetaverse.org nor the names
  11. * of its contributors may be used to endorse or promote products derived from
  12. * this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.Threading;
  28. using System.Collections.Generic;
  29. using Timer = System.Threading.Timer ;
  30. namespace OpenSim.Framework
  31. {
  32. public sealed class ExpiringKey<Tkey1> : IDisposable
  33. {
  34. private const int MINEXPIRECHECK = 500;
  35. private Timer m_purgeTimer;
  36. private ReaderWriterLockSlim m_rwLock;
  37. private readonly Dictionary<Tkey1, int> m_dictionary;
  38. private readonly double m_startTS;
  39. private readonly int m_expire;
  40. public ExpiringKey()
  41. {
  42. m_dictionary = new Dictionary<Tkey1, int>();
  43. m_rwLock = new ReaderWriterLockSlim();
  44. m_expire = MINEXPIRECHECK;
  45. m_startTS = Util.GetTimeStampMS();
  46. }
  47. public ExpiringKey(int expireCheckTimeinMS)
  48. {
  49. m_dictionary = new Dictionary<Tkey1, int>();
  50. m_rwLock = new ReaderWriterLockSlim();
  51. m_startTS = Util.GetTimeStampMS();
  52. m_expire = (expireCheckTimeinMS > MINEXPIRECHECK) ? m_expire = expireCheckTimeinMS : MINEXPIRECHECK;
  53. }
  54. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  55. private void CheckTimer()
  56. {
  57. if (m_purgeTimer == null)
  58. {
  59. m_purgeTimer = new Timer(Purge, null, m_expire, Timeout.Infinite);
  60. }
  61. }
  62. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  63. private void DisposeTimer()
  64. {
  65. if (m_purgeTimer != null)
  66. {
  67. m_purgeTimer.Dispose();
  68. m_purgeTimer = null;
  69. }
  70. }
  71. ~ExpiringKey()
  72. {
  73. Dispose(false);
  74. }
  75. public void Dispose()
  76. {
  77. Dispose(true);
  78. GC.SuppressFinalize(this);
  79. }
  80. private void Dispose(bool disposing)
  81. {
  82. if (m_rwLock != null)
  83. {
  84. DisposeTimer();
  85. m_rwLock.Dispose();
  86. m_rwLock = null;
  87. }
  88. }
  89. private void Purge(object ignored)
  90. {
  91. bool gotLock = false;
  92. try
  93. {
  94. try { }
  95. finally
  96. {
  97. m_rwLock.EnterUpgradeableReadLock();
  98. gotLock = true;
  99. }
  100. if (m_dictionary.Count == 0)
  101. {
  102. DisposeTimer();
  103. return;
  104. }
  105. int now = (int)(Util.GetTimeStampMS() - m_startTS);
  106. List<Tkey1> expired = new List<Tkey1>(m_dictionary.Count);
  107. foreach(KeyValuePair<Tkey1,int> kvp in m_dictionary)
  108. {
  109. if(kvp.Value < now)
  110. expired.Add(kvp.Key);
  111. }
  112. if (expired.Count > 0)
  113. {
  114. bool gotWriteLock = false;
  115. try
  116. {
  117. try { }
  118. finally
  119. {
  120. m_rwLock.EnterWriteLock();
  121. gotWriteLock = true;
  122. }
  123. foreach (Tkey1 key in expired)
  124. m_dictionary.Remove(key);
  125. }
  126. finally
  127. {
  128. if (gotWriteLock)
  129. m_rwLock.ExitWriteLock();
  130. }
  131. if (m_dictionary.Count == 0)
  132. DisposeTimer();
  133. else
  134. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  135. }
  136. else
  137. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  138. }
  139. finally
  140. {
  141. if (gotLock)
  142. m_rwLock.ExitUpgradeableReadLock();
  143. }
  144. }
  145. public void Add(Tkey1 key)
  146. {
  147. bool gotLock = false;
  148. int now = (int)(Util.GetTimeStampMS() - m_startTS) + m_expire;
  149. try
  150. {
  151. try { }
  152. finally
  153. {
  154. m_rwLock.EnterWriteLock();
  155. gotLock = true;
  156. }
  157. m_dictionary[key] = now;
  158. CheckTimer();
  159. }
  160. finally
  161. {
  162. if (gotLock)
  163. m_rwLock.ExitWriteLock();
  164. }
  165. }
  166. public void Add(Tkey1 key, int expireMS)
  167. {
  168. bool gotLock = false;
  169. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  170. int now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  171. try
  172. {
  173. try { }
  174. finally
  175. {
  176. m_rwLock.EnterWriteLock();
  177. gotLock = true;
  178. }
  179. m_dictionary[key] = now;
  180. CheckTimer();
  181. }
  182. finally
  183. {
  184. if (gotLock)
  185. m_rwLock.ExitWriteLock();
  186. }
  187. }
  188. public bool Remove(Tkey1 key)
  189. {
  190. bool success;
  191. bool gotLock = false;
  192. try
  193. {
  194. try {}
  195. finally
  196. {
  197. m_rwLock.EnterWriteLock();
  198. gotLock = true;
  199. }
  200. success = m_dictionary.Remove(key);
  201. if(m_dictionary.Count == 0)
  202. DisposeTimer();
  203. }
  204. finally
  205. {
  206. if (gotLock)
  207. m_rwLock.ExitWriteLock();
  208. }
  209. return success;
  210. }
  211. public void Clear()
  212. {
  213. bool gotLock = false;
  214. try
  215. {
  216. try {}
  217. finally
  218. {
  219. m_rwLock.EnterWriteLock();
  220. gotLock = true;
  221. }
  222. m_dictionary.Clear();
  223. DisposeTimer();
  224. }
  225. finally
  226. {
  227. if (gotLock)
  228. m_rwLock.ExitWriteLock();
  229. }
  230. }
  231. public int Count
  232. {
  233. get { return m_dictionary.Count; }
  234. }
  235. public bool ContainsKey(Tkey1 key)
  236. {
  237. bool gotLock = false;
  238. try
  239. {
  240. try { }
  241. finally
  242. {
  243. m_rwLock.EnterReadLock();
  244. gotLock = true;
  245. }
  246. return m_dictionary.ContainsKey(key);
  247. }
  248. finally
  249. {
  250. if (gotLock)
  251. m_rwLock.ExitReadLock();
  252. }
  253. }
  254. public bool ContainsKey(Tkey1 key, int expireMS)
  255. {
  256. bool gotLock = false;
  257. try
  258. {
  259. try { }
  260. finally
  261. {
  262. m_rwLock.EnterUpgradeableReadLock();
  263. gotLock = true;
  264. }
  265. if (m_dictionary.ContainsKey(key))
  266. {
  267. bool gotWriteLock = false;
  268. try
  269. {
  270. try { }
  271. finally
  272. {
  273. m_rwLock.EnterWriteLock();
  274. gotWriteLock = true;
  275. }
  276. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  277. int now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  278. m_dictionary[key] = now;
  279. return true;
  280. }
  281. finally
  282. {
  283. if (gotWriteLock)
  284. m_rwLock.ExitWriteLock();
  285. }
  286. }
  287. return false;
  288. }
  289. finally
  290. {
  291. if (gotLock)
  292. m_rwLock.EnterUpgradeableReadLock();
  293. }
  294. }
  295. public bool TryGetValue(Tkey1 key, out int value)
  296. {
  297. bool success;
  298. bool gotLock = false;
  299. try
  300. {
  301. try {}
  302. finally
  303. {
  304. m_rwLock.EnterReadLock();
  305. gotLock = true;
  306. }
  307. success = m_dictionary.TryGetValue(key, out value);
  308. }
  309. finally
  310. {
  311. if (gotLock)
  312. m_rwLock.ExitReadLock();
  313. }
  314. return success;
  315. }
  316. }
  317. }