1
0

ExpiringKey.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. int expire = kvp.Value;
  110. if (expire > 0 && expire < now)
  111. expired.Add(kvp.Key);
  112. }
  113. if (expired.Count > 0)
  114. {
  115. bool gotWriteLock = false;
  116. try
  117. {
  118. try { }
  119. finally
  120. {
  121. m_rwLock.EnterWriteLock();
  122. gotWriteLock = true;
  123. }
  124. foreach (Tkey1 key in expired)
  125. m_dictionary.Remove(key);
  126. }
  127. finally
  128. {
  129. if (gotWriteLock)
  130. m_rwLock.ExitWriteLock();
  131. }
  132. if (m_dictionary.Count == 0)
  133. DisposeTimer();
  134. else
  135. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  136. }
  137. else
  138. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  139. }
  140. finally
  141. {
  142. if (gotLock)
  143. m_rwLock.ExitUpgradeableReadLock();
  144. }
  145. }
  146. public void Add(Tkey1 key)
  147. {
  148. bool gotLock = false;
  149. int now = (int)(Util.GetTimeStampMS() - m_startTS) + m_expire;
  150. try
  151. {
  152. try { }
  153. finally
  154. {
  155. m_rwLock.EnterWriteLock();
  156. gotLock = true;
  157. }
  158. m_dictionary[key] = now;
  159. CheckTimer();
  160. }
  161. finally
  162. {
  163. if (gotLock)
  164. m_rwLock.ExitWriteLock();
  165. }
  166. }
  167. public void Add(Tkey1 key, int expireMS)
  168. {
  169. bool gotLock = false;
  170. int now;
  171. if (expireMS > 0)
  172. {
  173. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  174. now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  175. }
  176. else
  177. now = int.MinValue;
  178. try
  179. {
  180. try { }
  181. finally
  182. {
  183. m_rwLock.EnterWriteLock();
  184. gotLock = true;
  185. }
  186. m_dictionary[key] = now;
  187. CheckTimer();
  188. }
  189. finally
  190. {
  191. if (gotLock)
  192. m_rwLock.ExitWriteLock();
  193. }
  194. }
  195. public bool Remove(Tkey1 key)
  196. {
  197. bool success;
  198. bool gotLock = false;
  199. try
  200. {
  201. try {}
  202. finally
  203. {
  204. m_rwLock.EnterWriteLock();
  205. gotLock = true;
  206. }
  207. success = m_dictionary.Remove(key);
  208. if(m_dictionary.Count == 0)
  209. DisposeTimer();
  210. }
  211. finally
  212. {
  213. if (gotLock)
  214. m_rwLock.ExitWriteLock();
  215. }
  216. return success;
  217. }
  218. public void Clear()
  219. {
  220. bool gotLock = false;
  221. try
  222. {
  223. try {}
  224. finally
  225. {
  226. m_rwLock.EnterWriteLock();
  227. gotLock = true;
  228. }
  229. m_dictionary.Clear();
  230. DisposeTimer();
  231. }
  232. finally
  233. {
  234. if (gotLock)
  235. m_rwLock.ExitWriteLock();
  236. }
  237. }
  238. public int Count
  239. {
  240. get { return m_dictionary.Count; }
  241. }
  242. public bool ContainsKey(Tkey1 key)
  243. {
  244. bool gotLock = false;
  245. try
  246. {
  247. try { }
  248. finally
  249. {
  250. m_rwLock.EnterReadLock();
  251. gotLock = true;
  252. }
  253. return m_dictionary.ContainsKey(key);
  254. }
  255. finally
  256. {
  257. if (gotLock)
  258. m_rwLock.ExitReadLock();
  259. }
  260. }
  261. public bool ContainsKey(Tkey1 key, int expireMS)
  262. {
  263. bool gotLock = false;
  264. try
  265. {
  266. try { }
  267. finally
  268. {
  269. m_rwLock.EnterUpgradeableReadLock();
  270. gotLock = true;
  271. }
  272. if (m_dictionary.ContainsKey(key))
  273. {
  274. bool gotWriteLock = false;
  275. try
  276. {
  277. try { }
  278. finally
  279. {
  280. m_rwLock.EnterWriteLock();
  281. gotWriteLock = true;
  282. }
  283. int now;
  284. if (expireMS > 0)
  285. {
  286. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  287. now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  288. }
  289. else
  290. now = int.MinValue;
  291. m_dictionary[key] = now;
  292. return true;
  293. }
  294. finally
  295. {
  296. if (gotWriteLock)
  297. m_rwLock.ExitWriteLock();
  298. }
  299. }
  300. return false;
  301. }
  302. finally
  303. {
  304. if (gotLock)
  305. m_rwLock.EnterUpgradeableReadLock();
  306. }
  307. }
  308. public bool TryGetValue(Tkey1 key, out int value)
  309. {
  310. bool success;
  311. bool gotLock = false;
  312. try
  313. {
  314. try {}
  315. finally
  316. {
  317. m_rwLock.EnterReadLock();
  318. gotLock = true;
  319. }
  320. success = m_dictionary.TryGetValue(key, out value);
  321. }
  322. finally
  323. {
  324. if (gotLock)
  325. m_rwLock.ExitReadLock();
  326. }
  327. return success;
  328. }
  329. }
  330. }