ExpiringCacheOS.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. // this is a lighter alternative to libomv, no sliding option
  27. using System;
  28. using System.Threading;
  29. using System.Collections.Generic;
  30. using Timer = System.Threading.Timer;
  31. namespace OpenSim.Framework
  32. {
  33. public sealed class ExpiringCacheOS<TKey1, TValue1> : IDisposable
  34. {
  35. private const int MINEXPIRECHECK = 500;
  36. private Timer m_purgeTimer;
  37. private ReaderWriterLockSlim m_rwLock;
  38. private readonly Dictionary<TKey1, int> m_expireControl;
  39. private readonly Dictionary<TKey1, TValue1> m_values;
  40. TValue1[] valuesArrayCache = null;
  41. private readonly double m_startTS;
  42. private readonly int m_expire;
  43. public ExpiringCacheOS()
  44. {
  45. m_expireControl = new Dictionary<TKey1, int>();
  46. m_values = new Dictionary<TKey1, TValue1>();
  47. m_rwLock = new ReaderWriterLockSlim();
  48. m_expire = MINEXPIRECHECK;
  49. m_startTS = Util.GetTimeStampMS();
  50. }
  51. public ExpiringCacheOS(int expireCheckTimeinMS)
  52. {
  53. m_expireControl = new Dictionary<TKey1, int>();
  54. m_values = new Dictionary<TKey1, TValue1>();
  55. m_rwLock = new ReaderWriterLockSlim();
  56. m_startTS = Util.GetTimeStampMS();
  57. m_expire = (expireCheckTimeinMS > MINEXPIRECHECK) ? m_expire = expireCheckTimeinMS : MINEXPIRECHECK;
  58. }
  59. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  60. private void CheckTimer()
  61. {
  62. if (m_purgeTimer == null)
  63. {
  64. m_purgeTimer = new Timer(Purge, null, m_expire, Timeout.Infinite);
  65. }
  66. }
  67. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  68. private void DisposeTimer()
  69. {
  70. if (m_purgeTimer != null)
  71. {
  72. m_purgeTimer.Dispose();
  73. m_purgeTimer = null;
  74. }
  75. }
  76. ~ExpiringCacheOS()
  77. {
  78. Dispose(false);
  79. }
  80. public void Dispose()
  81. {
  82. Dispose(true);
  83. GC.SuppressFinalize(this);
  84. }
  85. private void Dispose(bool disposing)
  86. {
  87. if (m_rwLock != null)
  88. {
  89. DisposeTimer();
  90. m_rwLock.Dispose();
  91. m_rwLock = null;
  92. }
  93. }
  94. private void Purge(object ignored)
  95. {
  96. bool gotLock = false;
  97. try
  98. {
  99. try { }
  100. finally
  101. {
  102. m_rwLock.EnterUpgradeableReadLock();
  103. gotLock = true;
  104. }
  105. if (m_expireControl.Count == 0)
  106. {
  107. DisposeTimer();
  108. return;
  109. }
  110. int now = (int)(Util.GetTimeStampMS() - m_startTS);
  111. List<TKey1> expired = new List<TKey1>(m_expireControl.Count);
  112. foreach(KeyValuePair<TKey1, int> kvp in m_expireControl)
  113. {
  114. int expire = kvp.Value;
  115. if(expire > 0 && expire < now)
  116. expired.Add(kvp.Key);
  117. }
  118. if(expired.Count > 0)
  119. {
  120. bool gotWriteLock = false;
  121. try
  122. {
  123. try { }
  124. finally
  125. {
  126. m_rwLock.EnterWriteLock();
  127. gotWriteLock = true;
  128. }
  129. valuesArrayCache = null;
  130. foreach (TKey1 key in expired)
  131. {
  132. m_expireControl.Remove(key);
  133. m_values.Remove(key);
  134. }
  135. }
  136. finally
  137. {
  138. if (gotWriteLock)
  139. m_rwLock.ExitWriteLock();
  140. }
  141. if (m_expireControl.Count == 0)
  142. DisposeTimer();
  143. else
  144. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  145. }
  146. else
  147. m_purgeTimer.Change(m_expire, Timeout.Infinite);
  148. }
  149. finally
  150. {
  151. if (gotLock)
  152. m_rwLock.ExitUpgradeableReadLock();
  153. }
  154. }
  155. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  156. public void AddOrUpdate(TKey1 key, TValue1 val)
  157. {
  158. Add(key, val);
  159. }
  160. public void Add(TKey1 key, TValue1 val)
  161. {
  162. bool gotLock = false;
  163. int now = (int)(Util.GetTimeStampMS() - m_startTS) + m_expire;
  164. try
  165. {
  166. try { }
  167. finally
  168. {
  169. m_rwLock.EnterWriteLock();
  170. gotLock = true;
  171. }
  172. m_expireControl[key] = now;
  173. m_values[key] = val;
  174. valuesArrayCache = null;
  175. CheckTimer();
  176. }
  177. finally
  178. {
  179. if (gotLock)
  180. m_rwLock.ExitWriteLock();
  181. }
  182. }
  183. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  184. public void AddOrUpdate(TKey1 key, TValue1 val, int expireSeconds)
  185. {
  186. Add(key, val, expireSeconds * 1000);
  187. }
  188. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  189. public void AddOrUpdate(TKey1 key, TValue1 val, double expireSeconds)
  190. {
  191. Add(key, val, (int)(expireSeconds * 1000));
  192. }
  193. public void Add(TKey1 key, TValue1 val, int expireMS)
  194. {
  195. bool gotLock = false;
  196. int now;
  197. if (expireMS > 0)
  198. {
  199. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  200. now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  201. }
  202. else
  203. now = int.MinValue;
  204. try
  205. {
  206. try { }
  207. finally
  208. {
  209. m_rwLock.EnterWriteLock();
  210. gotLock = true;
  211. }
  212. m_expireControl[key] = now;
  213. m_values[key] = val;
  214. valuesArrayCache = null;
  215. CheckTimer();
  216. }
  217. finally
  218. {
  219. if (gotLock)
  220. m_rwLock.ExitWriteLock();
  221. }
  222. }
  223. public bool Remove(TKey1 key)
  224. {
  225. bool success;
  226. bool gotLock = false;
  227. try
  228. {
  229. try {}
  230. finally
  231. {
  232. m_rwLock.EnterWriteLock();
  233. gotLock = true;
  234. }
  235. success = m_expireControl.Remove(key);
  236. success |= m_values.Remove(key);
  237. if(success)
  238. valuesArrayCache = null;
  239. if (m_expireControl.Count == 0)
  240. DisposeTimer();
  241. }
  242. finally
  243. {
  244. if (gotLock)
  245. m_rwLock.ExitWriteLock();
  246. }
  247. return success;
  248. }
  249. public void Clear()
  250. {
  251. bool gotLock = false;
  252. try
  253. {
  254. try {}
  255. finally
  256. {
  257. m_rwLock.EnterWriteLock();
  258. gotLock = true;
  259. }
  260. DisposeTimer();
  261. m_expireControl.Clear();
  262. m_values.Clear();
  263. valuesArrayCache = null;
  264. }
  265. finally
  266. {
  267. if (gotLock)
  268. m_rwLock.ExitWriteLock();
  269. }
  270. }
  271. public int Count
  272. {
  273. get { return m_expireControl.Count; }
  274. }
  275. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  276. public bool Contains(TKey1 key)
  277. {
  278. return ContainsKey(key);
  279. }
  280. public bool ContainsKey(TKey1 key)
  281. {
  282. bool gotLock = false;
  283. try
  284. {
  285. try { }
  286. finally
  287. {
  288. m_rwLock.EnterReadLock();
  289. gotLock = true;
  290. }
  291. return m_expireControl.ContainsKey(key);
  292. }
  293. finally
  294. {
  295. if (gotLock)
  296. m_rwLock.ExitReadLock();
  297. }
  298. }
  299. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  300. public bool Contains(TKey1 key, int expireMS)
  301. {
  302. return ContainsKey(key, expireMS);
  303. }
  304. public bool ContainsKey(TKey1 key, int expireMS)
  305. {
  306. bool gotLock = false;
  307. try
  308. {
  309. try { }
  310. finally
  311. {
  312. m_rwLock.EnterUpgradeableReadLock();
  313. gotLock = true;
  314. }
  315. if(m_expireControl.ContainsKey(key))
  316. {
  317. bool gotWriteLock = false;
  318. try
  319. {
  320. try { }
  321. finally
  322. {
  323. m_rwLock.EnterWriteLock();
  324. gotWriteLock = true;
  325. }
  326. int now;
  327. if(expireMS > 0)
  328. {
  329. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  330. now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  331. }
  332. else
  333. now = int.MinValue;
  334. m_expireControl[key] = now;
  335. return true;
  336. }
  337. finally
  338. {
  339. if (gotWriteLock)
  340. m_rwLock.ExitWriteLock();
  341. }
  342. }
  343. return false;
  344. }
  345. finally
  346. {
  347. if (gotLock)
  348. m_rwLock.ExitUpgradeableReadLock();
  349. }
  350. }
  351. public bool TryGetValue(TKey1 key, out TValue1 value)
  352. {
  353. bool gotLock = false;
  354. try
  355. {
  356. try {}
  357. finally
  358. {
  359. m_rwLock.EnterReadLock();
  360. gotLock = true;
  361. }
  362. return m_values.TryGetValue(key, out value);
  363. }
  364. finally
  365. {
  366. if (gotLock)
  367. m_rwLock.ExitReadLock();
  368. }
  369. }
  370. public bool TryGetValue(TKey1 key, int expireMS, out TValue1 value)
  371. {
  372. bool success;
  373. bool gotLock = false;
  374. try
  375. {
  376. try { }
  377. finally
  378. {
  379. m_rwLock.EnterUpgradeableReadLock();
  380. gotLock = true;
  381. }
  382. success = m_values.TryGetValue(key, out value);
  383. if(success)
  384. {
  385. bool gotWriteLock = false;
  386. try
  387. {
  388. try { }
  389. finally
  390. {
  391. m_rwLock.EnterWriteLock();
  392. gotWriteLock = true;
  393. }
  394. int now;
  395. if(expireMS > 0)
  396. {
  397. expireMS = (expireMS > m_expire) ? expireMS : m_expire;
  398. now = (int)(Util.GetTimeStampMS() - m_startTS) + expireMS;
  399. }
  400. else
  401. now = int.MinValue;
  402. m_expireControl[key] = now;
  403. }
  404. finally
  405. {
  406. if (gotWriteLock)
  407. m_rwLock.ExitWriteLock();
  408. }
  409. }
  410. }
  411. finally
  412. {
  413. if (gotLock)
  414. m_rwLock.ExitUpgradeableReadLock();
  415. }
  416. return success;
  417. }
  418. public TValue1[] Values
  419. {
  420. get
  421. {
  422. bool gotLock = false;
  423. try
  424. {
  425. try { }
  426. finally
  427. {
  428. m_rwLock.EnterUpgradeableReadLock();
  429. gotLock = true;
  430. }
  431. if(valuesArrayCache == null)
  432. {
  433. valuesArrayCache = new TValue1[m_values.Count];
  434. m_values.Values.CopyTo(valuesArrayCache, 0);
  435. }
  436. return valuesArrayCache;
  437. }
  438. finally
  439. {
  440. if (gotLock)
  441. m_rwLock.ExitUpgradeableReadLock();
  442. }
  443. }
  444. }
  445. /*
  446. public ICollection<TKey1> Keys
  447. {
  448. get
  449. {
  450. bool gotLock = false;
  451. try
  452. {
  453. try { }
  454. finally
  455. {
  456. m_rwLock.EnterUpgradeableReadLock();
  457. gotLock = true;
  458. }
  459. return m_values.Keys;
  460. }
  461. finally
  462. {
  463. if (gotLock)
  464. m_rwLock.ExitUpgradeableReadLock();
  465. }
  466. }
  467. }
  468. */
  469. }
  470. }