CnmMemoryCache.cs 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. namespace OpenSim.Framework
  32. {
  33. /// <summary>
  34. /// Cenome memory based cache to store key/value pairs (elements) limited time and/or limited size.
  35. /// </summary>
  36. /// <typeparam name="TKey">
  37. /// The type of keys in the cache.
  38. /// </typeparam>
  39. /// <typeparam name="TValue">
  40. /// The type of values in the dictionary.
  41. /// </typeparam>
  42. /// <remarks>
  43. /// <para>
  44. /// Cenome memory cache stores elements to hash table generations. When new element is being added to cache, and new size would exceed
  45. /// maximal allowed size or maximal amount of allowed element count, then elements in oldest generation are deleted. Last access time
  46. /// is also tracked in generation level - thus it is possible that some elements are staying in cache far beyond their expiration time.
  47. /// If elements in older generations are accessed through <see cref="TryGetValue"/> method, they are moved to newest generation.
  48. /// </para>
  49. /// </remarks>
  50. public class CnmMemoryCache<TKey, TValue> : ICnmCache<TKey, TValue>
  51. {
  52. /// <summary>
  53. /// Default maximal count.
  54. /// </summary>
  55. /// <seealso cref="MaxCount"/>
  56. public const int DefaultMaxCount = 4096;
  57. /// <summary>
  58. /// Default maximal size.
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>
  62. /// 128MB = 128 * 1024^2 = 134 217 728 bytes.
  63. /// </para>
  64. /// </remarks>
  65. /// <seealso cref="MaxSize"/>
  66. public const long DefaultMaxSize = 134217728;
  67. /// <summary>
  68. /// How many operations between time checks.
  69. /// </summary>
  70. private const int DefaultOperationsBetweenTimeChecks = 40;
  71. /// <summary>
  72. /// Default expiration time.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// 30 minutes.
  77. /// </para>
  78. /// </remarks>
  79. public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes(30.0);
  80. /// <summary>
  81. /// Minimal allowed expiration time.
  82. /// </summary>
  83. /// <remarks>
  84. /// <para>
  85. /// 5 minutes.
  86. /// </para>
  87. /// </remarks>
  88. public static readonly TimeSpan MinExpirationTime = TimeSpan.FromSeconds(10.0);
  89. /// <summary>
  90. /// Comparer used to compare element keys.
  91. /// </summary>
  92. /// <remarks>
  93. /// Comparer is initialized by constructor.
  94. /// </remarks>
  95. /// <seealso cref="CnmMemoryCache{TKey,TValue}"/>
  96. public readonly IEqualityComparer<TKey> Comparer;
  97. /// <summary>
  98. /// Expiration time.
  99. /// </summary>
  100. private TimeSpan m_expirationTime = DefaultExpirationTime;
  101. /// <summary>
  102. /// Generation bucket count.
  103. /// </summary>
  104. private int m_generationBucketCount;
  105. /// <summary>
  106. /// Generation entry count.
  107. /// </summary>
  108. private int m_generationElementCount;
  109. /// <summary>
  110. /// Generation max size.
  111. /// </summary>
  112. private long m_generationMaxSize;
  113. /// <summary>
  114. /// Maximal allowed count of elements.
  115. /// </summary>
  116. private int m_maxCount;
  117. /// <summary>
  118. /// Maximal allowed total size of elements.
  119. /// </summary>
  120. private long m_maxElementSize;
  121. /// <summary>
  122. /// Maximal size.
  123. /// </summary>
  124. private long m_maxSize;
  125. /// <summary>
  126. /// New generation.
  127. /// </summary>
  128. private IGeneration m_newGeneration;
  129. /// <summary>
  130. /// Old generation.
  131. /// </summary>
  132. private IGeneration m_oldGeneration;
  133. /// <summary>
  134. /// Operations between time check.
  135. /// </summary>
  136. private int m_operationsBetweenTimeChecks = DefaultOperationsBetweenTimeChecks;
  137. /// <summary>
  138. /// Synchronization root object, should always be private and exists always
  139. /// </summary>
  140. private readonly object m_syncRoot = new object();
  141. /// <summary>
  142. /// Version of cache.
  143. /// </summary>
  144. /// <remarks>
  145. /// <para>
  146. /// Updated every time when cache has been changed (element removed, expired, added, replaced).
  147. /// </para>
  148. /// </remarks>
  149. private int m_version;
  150. /// <summary>
  151. /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class.
  152. /// </summary>
  153. public CnmMemoryCache()
  154. : this(DefaultMaxSize)
  155. {
  156. }
  157. /// <summary>
  158. /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class.
  159. /// </summary>
  160. /// <param name="maximalSize">
  161. /// Maximal cache size.
  162. /// </param>
  163. public CnmMemoryCache(long maximalSize)
  164. : this(maximalSize, DefaultMaxCount)
  165. {
  166. }
  167. /// <summary>
  168. /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class.
  169. /// </summary>
  170. /// <param name="maximalSize">
  171. /// Maximal cache size.
  172. /// </param>
  173. /// <param name="maximalCount">
  174. /// Maximal element count.
  175. /// </param>
  176. public CnmMemoryCache(long maximalSize, int maximalCount)
  177. : this(maximalSize, maximalCount, DefaultExpirationTime)
  178. {
  179. }
  180. /// <summary>
  181. /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class.
  182. /// </summary>
  183. /// <param name="maximalSize">
  184. /// Maximal cache size.
  185. /// </param>
  186. /// <param name="maximalCount">
  187. /// Maximal element count.
  188. /// </param>
  189. /// <param name="expirationTime">
  190. /// Elements expiration time.
  191. /// </param>
  192. public CnmMemoryCache(long maximalSize, int maximalCount, TimeSpan expirationTime)
  193. : this(maximalSize, maximalCount, expirationTime, EqualityComparer<TKey>.Default)
  194. {
  195. }
  196. /// <summary>
  197. /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class.
  198. /// </summary>
  199. /// <param name="maximalSize">
  200. /// Maximal cache size.
  201. /// </param>
  202. /// <param name="maximalCount">
  203. /// Maximal element count.
  204. /// </param>
  205. /// <param name="expirationTime">
  206. /// Elements expiration time.
  207. /// </param>
  208. /// <param name="comparer">
  209. /// Comparer used for comparing elements.
  210. /// </param>
  211. /// <exception cref="ArgumentNullException">
  212. /// <see cref="comparer"/>is <see langword="null"/> reference.
  213. /// </exception>
  214. public CnmMemoryCache(long maximalSize,
  215. int maximalCount,
  216. TimeSpan expirationTime,
  217. IEqualityComparer<TKey> comparer)
  218. {
  219. if (comparer == null)
  220. throw new ArgumentNullException("comparer");
  221. if (expirationTime < MinExpirationTime)
  222. expirationTime = MinExpirationTime;
  223. if (maximalCount < 8)
  224. maximalCount = 8;
  225. if (maximalSize < 8)
  226. maximalSize = 8;
  227. if (maximalCount > maximalSize)
  228. maximalCount = (int) maximalSize;
  229. Comparer = comparer;
  230. m_expirationTime = expirationTime;
  231. m_maxSize = maximalSize;
  232. m_maxCount = maximalCount;
  233. Initialize();
  234. }
  235. /// <summary>
  236. /// Add element to new generation.
  237. /// </summary>
  238. /// <param name="bucketIndex">
  239. /// The bucket index.
  240. /// </param>
  241. /// <param name="key">
  242. /// The element's key.
  243. /// </param>
  244. /// <param name="value">
  245. /// The element's value.
  246. /// </param>
  247. /// <param name="size">
  248. /// The element's size.
  249. /// </param>
  250. protected virtual void AddToNewGeneration(int bucketIndex, TKey key, TValue value, long size)
  251. {
  252. // Add to newest generation
  253. if (!m_newGeneration.Set(bucketIndex, key, value, size))
  254. {
  255. // Failed to add new generation
  256. RecycleGenerations();
  257. m_newGeneration.Set(bucketIndex, key, value, size);
  258. }
  259. m_version++;
  260. }
  261. /// <summary>
  262. /// <para>
  263. /// Get keys bucket index.
  264. /// </para>
  265. /// </summary>
  266. /// <param name="key">
  267. /// <para>
  268. /// Key which bucket index is being retrieved.
  269. /// </para>
  270. /// </param>
  271. /// <returns>
  272. /// <para>
  273. /// Bucket index.
  274. /// </para>
  275. /// </returns>
  276. /// <remarks>
  277. /// <para>
  278. /// Method uses <see cref="Comparer"/> to calculate <see cref="key"/> hash code.
  279. /// </para>
  280. /// <para>
  281. /// Bucket index is remainder when element key's hash value is divided by bucket count.
  282. /// </para>
  283. /// <para>
  284. /// For example: key's hash is 72, bucket count is 5, element's bucket index is 72 % 5 = 2.
  285. /// </para>
  286. /// </remarks>
  287. protected virtual int GetBucketIndex(TKey key)
  288. {
  289. return (Comparer.GetHashCode(key) & 0x7FFFFFFF) % m_generationBucketCount;
  290. }
  291. /// <summary>
  292. /// Purge generation from the cache.
  293. /// </summary>
  294. /// <param name="generation">
  295. /// The generation that is purged.
  296. /// </param>
  297. protected virtual void PurgeGeneration(IGeneration generation)
  298. {
  299. generation.Clear();
  300. m_version++;
  301. }
  302. /// <summary>
  303. /// check expired.
  304. /// </summary>
  305. private void CheckExpired()
  306. {
  307. // Do this only one in every m_operationsBetweenTimeChecks
  308. // Fetching time is using several millisecons - it is better not to do all time.
  309. m_operationsBetweenTimeChecks--;
  310. if (m_operationsBetweenTimeChecks <= 0)
  311. PurgeExpired();
  312. }
  313. /// <summary>
  314. /// Initialize cache.
  315. /// </summary>
  316. private void Initialize()
  317. {
  318. m_version++;
  319. m_generationMaxSize = MaxSize / 2;
  320. MaxElementSize = MaxSize / 8;
  321. m_generationElementCount = MaxCount / 2;
  322. // Buckets need to be prime number to get better spread of hash values
  323. m_generationBucketCount = PrimeNumberHelper.GetPrime(m_generationElementCount);
  324. m_newGeneration = new HashGeneration(this);
  325. m_oldGeneration = new HashGeneration(this);
  326. m_oldGeneration.MakeOld();
  327. }
  328. /// <summary>
  329. /// Recycle generations.
  330. /// </summary>
  331. private void RecycleGenerations()
  332. {
  333. // Rotate old generation to new generation, new generation to old generation
  334. IGeneration temp = m_newGeneration;
  335. m_newGeneration = m_oldGeneration;
  336. m_newGeneration.Clear();
  337. m_oldGeneration = temp;
  338. m_oldGeneration.MakeOld();
  339. }
  340. #region Nested type: Enumerator
  341. /// <summary>
  342. /// Key and value pair enumerator.
  343. /// </summary>
  344. private class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
  345. {
  346. /// <summary>
  347. /// Current enumerator.
  348. /// </summary>
  349. private int m_currentEnumerator = -1;
  350. /// <summary>
  351. /// Enumerators to different generations.
  352. /// </summary>
  353. private readonly IEnumerator<KeyValuePair<TKey, TValue>>[] m_generationEnumerators =
  354. new IEnumerator<KeyValuePair<TKey, TValue>>[2];
  355. /// <summary>
  356. /// Initializes a new instance of the <see cref="Enumerator"/> class.
  357. /// </summary>
  358. /// <param name="cache">
  359. /// The cache.
  360. /// </param>
  361. public Enumerator(CnmMemoryCache<TKey, TValue> cache)
  362. {
  363. m_generationEnumerators[ 0 ] = cache.m_newGeneration.GetEnumerator();
  364. m_generationEnumerators[ 1 ] = cache.m_oldGeneration.GetEnumerator();
  365. }
  366. #region IEnumerator<KeyValuePair<TKey,TValue>> Members
  367. /// <summary>
  368. /// Gets the element in the collection at the current position of the enumerator.
  369. /// </summary>
  370. /// <returns>
  371. /// The element in the collection at the current position of the enumerator.
  372. /// </returns>
  373. /// <exception cref="InvalidOperationException">
  374. /// The enumerator has reach end of collection or <see cref="MoveNext"/> is not called.
  375. /// </exception>
  376. public KeyValuePair<TKey, TValue> Current
  377. {
  378. get
  379. {
  380. if (m_currentEnumerator == -1 || m_currentEnumerator >= m_generationEnumerators.Length)
  381. throw new InvalidOperationException();
  382. return m_generationEnumerators[ m_currentEnumerator ].Current;
  383. }
  384. }
  385. /// <summary>
  386. /// Gets the current element in the collection.
  387. /// </summary>
  388. /// <returns>
  389. /// The current element in the collection.
  390. /// </returns>
  391. /// <exception cref="T:System.InvalidOperationException">
  392. /// The enumerator is positioned before the first element of the collection or after the last element.
  393. /// </exception><filterpriority>2</filterpriority>
  394. object IEnumerator.Current
  395. {
  396. get { return Current; }
  397. }
  398. /// <summary>
  399. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  400. /// </summary>
  401. /// <filterpriority>2</filterpriority>
  402. public void Dispose()
  403. {
  404. }
  405. /// <summary>
  406. /// Advances the enumerator to the next element of the collection.
  407. /// </summary>
  408. /// <returns>
  409. /// <see langword="true"/>if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.
  410. /// </returns>
  411. /// <exception cref="T:System.InvalidOperationException">
  412. /// The collection was modified after the enumerator was created.
  413. /// </exception>
  414. /// <filterpriority>2</filterpriority>
  415. public bool MoveNext()
  416. {
  417. if (m_currentEnumerator == -1)
  418. m_currentEnumerator = 0;
  419. while (m_currentEnumerator < m_generationEnumerators.Length)
  420. {
  421. if (m_generationEnumerators[ m_currentEnumerator ].MoveNext())
  422. return true;
  423. m_currentEnumerator++;
  424. }
  425. return false;
  426. }
  427. /// <summary>
  428. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  429. /// </summary>
  430. /// <exception cref="T:System.InvalidOperationException">
  431. /// The collection was modified after the enumerator was created.
  432. /// </exception>
  433. /// <filterpriority>2</filterpriority>
  434. public void Reset()
  435. {
  436. foreach (IEnumerator<KeyValuePair<TKey, TValue>> enumerator in m_generationEnumerators)
  437. {
  438. enumerator.Reset();
  439. }
  440. m_currentEnumerator = -1;
  441. }
  442. #endregion
  443. }
  444. #endregion
  445. #region Nested type: HashGeneration
  446. /// <summary>
  447. /// Hash generation class
  448. /// </summary>
  449. /// <remarks>
  450. /// <para>
  451. /// Current implementation is based to separated chaining with move-to-front heuristics. Hash generations have fixed
  452. /// amount of buckets and it is never rehashed.
  453. /// </para>
  454. /// <para>
  455. /// Read more about hash tables from <a href="http://en.wikipedia.org/wiki/Hash_table">Wiki article</a>.
  456. /// </para>
  457. /// </remarks>
  458. /// <seealso href="http://en.wikipedia.org/wiki/Hash_table"/>
  459. private class HashGeneration : IGeneration
  460. {
  461. /// <summary>
  462. /// Value indicating whether generation was accessed since last time check.
  463. /// </summary>
  464. private bool m_accessedSinceLastTimeCheck;
  465. /// <summary>
  466. /// Index of first element's in element chain.
  467. /// </summary>
  468. /// <value>
  469. /// -1 if there is no element in bucket; otherwise first element's index in the element chain.
  470. /// </value>
  471. /// <remarks>
  472. /// Bucket index is remainder when element key's hash value is divided by bucket count.
  473. /// For example: key's hash is 72, bucket count is 5, element's bucket index is 72 % 5 = 2.
  474. /// </remarks>
  475. private readonly int[] m_buckets;
  476. /// <summary>
  477. /// Cache object.
  478. /// </summary>
  479. private readonly CnmMemoryCache<TKey, TValue> m_cache;
  480. /// <summary>
  481. /// Generation's element array.
  482. /// </summary>
  483. /// <seealso cref="Element"/>
  484. private readonly Element[] m_elements;
  485. /// <summary>
  486. /// Generation's expiration time.
  487. /// </summary>
  488. private DateTime m_expirationTime1;
  489. /// <summary>
  490. /// Index to first free element.
  491. /// </summary>
  492. private int m_firstFreeElement;
  493. /// <summary>
  494. /// Free element count.
  495. /// </summary>
  496. /// <remarks>
  497. /// When generation is cleared or constructed, this is NOT set to element count.
  498. /// This is only tracking elements that are removed and are currently free.
  499. /// </remarks>
  500. private int m_freeCount;
  501. /// <summary>
  502. /// Is this generation "new generation".
  503. /// </summary>
  504. private bool m_newGeneration;
  505. /// <summary>
  506. /// Next unused entry.
  507. /// </summary>
  508. private int m_nextUnusedElement;
  509. /// <summary>
  510. /// Size of data stored to generation.
  511. /// </summary>
  512. private long m_size;
  513. /// <summary>
  514. /// Initializes a new instance of the <see cref="HashGeneration"/> class.
  515. /// </summary>
  516. /// <param name="cache">
  517. /// The cache.
  518. /// </param>
  519. public HashGeneration(CnmMemoryCache<TKey, TValue> cache)
  520. {
  521. m_cache = cache;
  522. m_elements = new Element[m_cache.m_generationElementCount];
  523. m_buckets = new int[m_cache.m_generationBucketCount];
  524. Clear();
  525. }
  526. /// <summary>
  527. /// Find element's index
  528. /// </summary>
  529. /// <param name="bucketIndex">
  530. /// The element's bucket index.
  531. /// </param>
  532. /// <param name="key">
  533. /// The element's key.
  534. /// </param>
  535. /// <param name="moveToFront">
  536. /// Move element to front of elements.
  537. /// </param>
  538. /// <param name="previousIndex">
  539. /// The previous element's index.
  540. /// </param>
  541. /// <returns>
  542. /// Element's index, if found from the generation; -1 otherwise (if element is not found the generation).
  543. /// </returns>
  544. private int FindElementIndex(int bucketIndex, TKey key, bool moveToFront, out int previousIndex)
  545. {
  546. previousIndex = -1;
  547. int elementIndex = m_buckets[ bucketIndex ];
  548. while (elementIndex >= 0)
  549. {
  550. if (m_cache.Comparer.Equals(key, m_elements[ elementIndex ].Key))
  551. {
  552. // Found match
  553. if (moveToFront && previousIndex >= 0)
  554. {
  555. // Move entry to front
  556. m_elements[ previousIndex ].Next = m_elements[ elementIndex ].Next;
  557. m_elements[ elementIndex ].Next = m_buckets[ bucketIndex ];
  558. m_buckets[ bucketIndex ] = elementIndex;
  559. previousIndex = 0;
  560. }
  561. return elementIndex;
  562. }
  563. previousIndex = elementIndex;
  564. elementIndex = m_elements[ elementIndex ].Next;
  565. }
  566. return -1;
  567. }
  568. /// <summary>
  569. /// Remove element front the generation.
  570. /// </summary>
  571. /// <param name="bucketIndex">
  572. /// The bucket index.
  573. /// </param>
  574. /// <param name="entryIndex">
  575. /// The element index.
  576. /// </param>
  577. /// <param name="previousIndex">
  578. /// The element's previous index.
  579. /// </param>
  580. private void RemoveElement(int bucketIndex, int entryIndex, int previousIndex)
  581. {
  582. if (previousIndex >= 0)
  583. m_elements[ previousIndex ].Next = m_elements[ entryIndex ].Next;
  584. else
  585. m_buckets[ bucketIndex ] = m_elements[ entryIndex ].Next;
  586. Size -= m_elements[ entryIndex ].Size;
  587. m_elements[ entryIndex ].Value = default(TValue);
  588. m_elements[ entryIndex ].Key = default(TKey);
  589. // Add element to free elements list
  590. m_elements[ entryIndex ].Next = m_firstFreeElement;
  591. m_firstFreeElement = entryIndex;
  592. m_freeCount++;
  593. }
  594. #region Nested type: Element
  595. /// <summary>
  596. /// Element that stores key, next element in chain, size and value.
  597. /// </summary>
  598. private struct Element
  599. {
  600. /// <summary>
  601. /// Element's key.
  602. /// </summary>
  603. public TKey Key;
  604. /// <summary>
  605. /// Next element in chain.
  606. /// </summary>
  607. /// <remarks>
  608. /// When element have value (something is stored to it), this is index of
  609. /// next element with same bucket index. When element is free, this
  610. /// is index of next element in free element's list.
  611. /// </remarks>
  612. public int Next;
  613. /// <summary>
  614. /// Size of element.
  615. /// </summary>
  616. /// <value>
  617. /// 0 if element is free; otherwise larger than 0.
  618. /// </value>
  619. public long Size;
  620. /// <summary>
  621. /// Element's value.
  622. /// </summary>
  623. /// <remarks>
  624. /// It is possible that this value is <see langword="null"/> even when element
  625. /// have value - element's value is then <see langword="null"/> reference.
  626. /// </remarks>
  627. public TValue Value;
  628. /// <summary>
  629. /// Gets a value indicating whether element is free or have value.
  630. /// </summary>
  631. /// <value>
  632. /// <see langword="true"/> when element is free; otherwise <see langword="false"/>.
  633. /// </value>
  634. public bool IsFree
  635. {
  636. get { return Size == 0; }
  637. }
  638. }
  639. #endregion
  640. #region Nested type: Enumerator
  641. /// <summary>
  642. /// Key value pair enumerator for <see cref="HashGeneration"/> object.
  643. /// </summary>
  644. private class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
  645. {
  646. /// <summary>
  647. /// Current element.
  648. /// </summary>
  649. private KeyValuePair<TKey, TValue> m_current;
  650. /// <summary>
  651. /// Current index.
  652. /// </summary>
  653. private int m_currentIndex;
  654. /// <summary>
  655. /// Generation that is being enumerated.
  656. /// </summary>
  657. private readonly HashGeneration m_generation;
  658. /// <summary>
  659. /// Cache version.
  660. /// </summary>
  661. /// <remarks>
  662. /// When cache is change, version number is changed.
  663. /// </remarks>
  664. /// <seealso cref="CnmMemoryCache{TKey,TValue}.m_version"/>
  665. private readonly int m_version;
  666. /// <summary>
  667. /// Initializes a new instance of the <see cref="Enumerator"/> class.
  668. /// </summary>
  669. /// <param name="generation">
  670. /// The generation.
  671. /// </param>
  672. public Enumerator(HashGeneration generation)
  673. {
  674. m_generation = generation;
  675. m_version = m_generation.m_cache.m_version;
  676. }
  677. #region IEnumerator<KeyValuePair<TKey,TValue>> Members
  678. /// <summary>
  679. /// Gets the element in the collection at the current position of the enumerator.
  680. /// </summary>
  681. /// <returns>
  682. /// The element in the collection at the current position of the enumerator.
  683. /// </returns>
  684. /// <exception cref="InvalidOperationException">
  685. /// The enumerator has reach end of collection or <see cref="MoveNext"/> is not called.
  686. /// </exception>
  687. public KeyValuePair<TKey, TValue> Current
  688. {
  689. get
  690. {
  691. if (m_currentIndex == 0 || m_currentIndex >= m_generation.Count)
  692. throw new InvalidOperationException();
  693. return m_current;
  694. }
  695. }
  696. /// <summary>
  697. /// Gets the current element in the collection.
  698. /// </summary>
  699. /// <returns>
  700. /// The current element in the collection.
  701. /// </returns>
  702. /// <exception cref="InvalidOperationException">
  703. /// The enumerator is positioned before the first element of the collection or after the last element.
  704. /// </exception><filterpriority>2</filterpriority>
  705. object IEnumerator.Current
  706. {
  707. get { return Current; }
  708. }
  709. /// <summary>
  710. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  711. /// </summary>
  712. /// <filterpriority>2</filterpriority>
  713. public void Dispose()
  714. {
  715. }
  716. /// <summary>
  717. /// Advances the enumerator to the next element of the collection.
  718. /// </summary>
  719. /// <returns>
  720. /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  721. /// </returns>
  722. /// <exception cref="InvalidOperationException">
  723. /// The collection was modified after the enumerator was created.
  724. /// </exception>
  725. public bool MoveNext()
  726. {
  727. if (m_version != m_generation.m_cache.m_version)
  728. throw new InvalidOperationException();
  729. while (m_currentIndex < m_generation.Count)
  730. {
  731. if (m_generation.m_elements[ m_currentIndex ].IsFree)
  732. {
  733. m_currentIndex++;
  734. continue;
  735. }
  736. m_current = new KeyValuePair<TKey, TValue>(m_generation.m_elements[ m_currentIndex ].Key,
  737. m_generation.m_elements[ m_currentIndex ].Value);
  738. m_currentIndex++;
  739. return true;
  740. }
  741. m_current = new KeyValuePair<TKey, TValue>();
  742. return false;
  743. }
  744. /// <summary>
  745. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  746. /// </summary>
  747. /// <exception cref="InvalidOperationException">
  748. /// The collection was modified after the enumerator was created.
  749. /// </exception>
  750. /// <filterpriority>2</filterpriority>
  751. public void Reset()
  752. {
  753. if (m_version != m_generation.m_cache.m_version)
  754. throw new InvalidOperationException();
  755. m_currentIndex = 0;
  756. }
  757. #endregion
  758. }
  759. #endregion
  760. #region IGeneration Members
  761. /// <summary>
  762. /// Gets or sets a value indicating whether generation was accessed since last time check.
  763. /// </summary>
  764. public bool AccessedSinceLastTimeCheck
  765. {
  766. get { return m_accessedSinceLastTimeCheck; }
  767. set { m_accessedSinceLastTimeCheck = value; }
  768. }
  769. /// <summary>
  770. /// Gets element count in generation.
  771. /// </summary>
  772. public int Count
  773. {
  774. get { return m_nextUnusedElement - m_freeCount; }
  775. }
  776. /// <summary>
  777. /// Gets or sets generation's expiration time.
  778. /// </summary>
  779. public DateTime ExpirationTime
  780. {
  781. get { return m_expirationTime1; }
  782. set { m_expirationTime1 = value; }
  783. }
  784. /// <summary>
  785. /// Gets or sets size of data stored to generation.
  786. /// </summary>
  787. public long Size
  788. {
  789. get { return m_size; }
  790. private set { m_size = value; }
  791. }
  792. /// <summary>
  793. /// Clear all elements from the generation and make generation new again.
  794. /// </summary>
  795. /// <remarks>
  796. /// When generation is new, it is allowed to add new elements to it and
  797. /// <see cref="IGeneration.TryGetValue"/>doesn't remove elements from it.
  798. /// </remarks>
  799. /// <seealso cref="IGeneration.MakeOld"/>
  800. public void Clear()
  801. {
  802. for (int i = m_buckets.Length - 1 ; i >= 0 ; i--)
  803. {
  804. m_buckets[ i ] = -1;
  805. }
  806. Array.Clear(m_elements, 0, m_elements.Length);
  807. Size = 0;
  808. m_firstFreeElement = -1;
  809. m_freeCount = 0;
  810. m_nextUnusedElement = 0;
  811. m_newGeneration = true;
  812. ExpirationTime = DateTime.MaxValue;
  813. }
  814. /// <summary>
  815. /// Determines whether the <see cref="IGeneration"/> contains an element with the specific key.
  816. /// </summary>
  817. /// <param name="bucketIndex">
  818. /// The bucket index for the <see cref="key"/> to locate in <see cref="IGeneration"/>.
  819. /// </param>
  820. /// <param name="key">
  821. /// The key to locate in the <see cref="IGeneration"/>.
  822. /// </param>
  823. /// <returns>
  824. /// <see langword="true"/>if the <see cref="IGeneration"/> contains an element with the <see cref="key"/>;
  825. /// otherwise <see langword="false"/>.
  826. /// </returns>
  827. public bool Contains(int bucketIndex, TKey key)
  828. {
  829. int previousIndex;
  830. if (FindElementIndex(bucketIndex, key, true, out previousIndex) == -1)
  831. return false;
  832. AccessedSinceLastTimeCheck = true;
  833. return true;
  834. }
  835. /// <summary>
  836. /// Returns an enumerator that iterates through the elements stored <see cref="HashGeneration"/>.
  837. /// </summary>
  838. /// <returns>
  839. /// A <see cref="IEnumerator"/> that can be used to iterate through the <see cref="HashGeneration"/>.
  840. /// </returns>
  841. /// <filterpriority>1</filterpriority>
  842. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  843. {
  844. return new Enumerator(this);
  845. }
  846. /// <summary>
  847. /// Make from generation old generation.
  848. /// </summary>
  849. /// <remarks>
  850. /// When generation is old, <see cref="IGeneration.TryGetValue"/> hit removes element from the generation.
  851. /// </remarks>
  852. /// <seealso cref="IGeneration.Clear"/>
  853. public void MakeOld()
  854. {
  855. m_newGeneration = false;
  856. }
  857. /// <summary>
  858. /// Remove element associated with the key from the generation.
  859. /// </summary>
  860. /// <param name="bucketIndex">
  861. /// The element's bucket index.
  862. /// </param>
  863. /// <param name="key">
  864. /// The element's key.
  865. /// </param>
  866. /// <returns>
  867. /// <see langword="true"/>, if remove was successful; otherwise <see langword="false"/>.
  868. /// </returns>
  869. public bool Remove(int bucketIndex, TKey key)
  870. {
  871. int previousIndex;
  872. int entryIndex = FindElementIndex(bucketIndex, key, false, out previousIndex);
  873. if (entryIndex != -1)
  874. {
  875. RemoveElement(bucketIndex, entryIndex, previousIndex);
  876. AccessedSinceLastTimeCheck = true;
  877. return true;
  878. }
  879. return false;
  880. }
  881. /// <summary>
  882. /// Set or add element to generation.
  883. /// </summary>
  884. /// <param name="bucketIndex">
  885. /// The element's bucket index.
  886. /// </param>
  887. /// <param name="key">
  888. /// The element's key.
  889. /// </param>
  890. /// <param name="value">
  891. /// The element's value.
  892. /// </param>
  893. /// <param name="size">
  894. /// The element's size.
  895. /// </param>
  896. /// <returns>
  897. /// <see langword="true"/>, if setting or adding was successful; otherwise <see langword="false"/>.
  898. /// </returns>
  899. /// <remarks>
  900. /// <para>
  901. /// If element was already existing in generation and new element size fits to collection limits,
  902. /// then it's value is replaced with new one and size information is updated. If element didn't
  903. /// exists in generation before, then generation must have empty space for a new element and
  904. /// size must fit generation's limits, before element is added to generation.
  905. /// </para>
  906. /// </remarks>
  907. public bool Set(int bucketIndex, TKey key, TValue value, long size)
  908. {
  909. Debug.Assert(m_newGeneration, "It is possible to insert new elements only to newest generation.");
  910. Debug.Assert(size > 0, "New element size should be more than 0.");
  911. int previousIndex;
  912. int elementIndex = FindElementIndex(bucketIndex, key, true, out previousIndex);
  913. if (elementIndex == -1)
  914. {
  915. // New key
  916. if (Size + size > m_cache.m_generationMaxSize ||
  917. (m_nextUnusedElement == m_cache.m_generationElementCount && m_freeCount == 0))
  918. {
  919. // Generation is full
  920. return false;
  921. }
  922. // Increase size of generation
  923. Size += size;
  924. // Get first free entry and update free entry list
  925. if (m_firstFreeElement != -1)
  926. {
  927. // There was entry that was removed
  928. elementIndex = m_firstFreeElement;
  929. m_firstFreeElement = m_elements[ elementIndex ].Next;
  930. m_freeCount--;
  931. }
  932. else
  933. {
  934. // No entries removed so far - just take a last one
  935. elementIndex = m_nextUnusedElement;
  936. m_nextUnusedElement++;
  937. }
  938. Debug.Assert(m_elements[ elementIndex ].IsFree, "Allocated element is not free.");
  939. // Move new entry to front
  940. m_elements[ elementIndex ].Next = m_buckets[ bucketIndex ];
  941. m_buckets[ bucketIndex ] = elementIndex;
  942. // Set key and update count
  943. m_elements[ elementIndex ].Key = key;
  944. }
  945. else
  946. {
  947. // Existing key
  948. if (Size - m_elements[ elementIndex ].Size + size > m_cache.m_generationMaxSize)
  949. {
  950. // Generation is full
  951. // Remove existing element, because generation is going to be recycled to
  952. // old generation and element is stored to new generation
  953. RemoveElement(bucketIndex, elementIndex, previousIndex);
  954. return false;
  955. }
  956. // Update generation's size
  957. Size = Size - m_elements[ elementIndex ].Size + size;
  958. }
  959. // Finally set value and size
  960. m_elements[ elementIndex ].Value = value;
  961. m_elements[ elementIndex ].Size = size;
  962. // Success - key was inserterted to generation
  963. AccessedSinceLastTimeCheck = true;
  964. return true;
  965. }
  966. /// <summary>
  967. /// Try to get element associated with key.
  968. /// </summary>
  969. /// <param name="bucketIndex">
  970. /// The element's bucket index.
  971. /// </param>
  972. /// <param name="key">
  973. /// The element's key.
  974. /// </param>
  975. /// <param name="value">
  976. /// The element's value.
  977. /// </param>
  978. /// <param name="size">
  979. /// The element's size.
  980. /// </param>
  981. /// <returns>
  982. /// <see langword="true"/>, if element was successful retrieved; otherwise <see langword="false"/>.
  983. /// </returns>
  984. /// <remarks>
  985. /// <para>
  986. /// If element is not found from generation then <paramref name="value"/> and <paramref name="size"/>
  987. /// are set to default value (default(TValue) and 0).
  988. /// </para>
  989. /// </remarks>
  990. public bool TryGetValue(int bucketIndex, TKey key, out TValue value, out long size)
  991. {
  992. // Find entry index,
  993. int previousIndex;
  994. int elementIndex = FindElementIndex(bucketIndex, key, m_newGeneration, out previousIndex);
  995. if (elementIndex == -1)
  996. {
  997. value = default(TValue);
  998. size = 0;
  999. return false;
  1000. }
  1001. value = m_elements[ elementIndex ].Value;
  1002. size = m_elements[ elementIndex ].Size;
  1003. if (!m_newGeneration)
  1004. {
  1005. // Old generation - remove element, because it is moved to new generation
  1006. RemoveElement(bucketIndex, elementIndex, previousIndex);
  1007. }
  1008. AccessedSinceLastTimeCheck = true;
  1009. return true;
  1010. }
  1011. /// <summary>
  1012. /// Returns an enumerator that iterates through a collection.
  1013. /// </summary>
  1014. /// <returns>
  1015. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  1016. /// </returns>
  1017. /// <filterpriority>2</filterpriority>
  1018. IEnumerator IEnumerable.GetEnumerator()
  1019. {
  1020. return GetEnumerator();
  1021. }
  1022. #endregion
  1023. }
  1024. #endregion
  1025. #region Nested type: IGeneration
  1026. /// <summary>
  1027. /// Cache element generation interface
  1028. /// </summary>
  1029. /// <remarks>
  1030. /// <para>
  1031. /// Generation can hold limited count of elements and limited size of data.
  1032. /// </para>
  1033. /// <para>
  1034. /// There are two kind generations: "new generation" and "old generation(s)". All new elements
  1035. /// are added to "new generation".
  1036. /// </para>
  1037. /// </remarks>
  1038. protected interface IGeneration : IEnumerable<KeyValuePair<TKey, TValue>>
  1039. {
  1040. /// <summary>
  1041. /// Gets or sets a value indicating whether generation was accessed since last time check.
  1042. /// </summary>
  1043. bool AccessedSinceLastTimeCheck { get; set; }
  1044. /// <summary>
  1045. /// Gets element count in generation.
  1046. /// </summary>
  1047. int Count { get; }
  1048. /// <summary>
  1049. /// Gets or sets generation's expiration time.
  1050. /// </summary>
  1051. DateTime ExpirationTime { get; set; }
  1052. /// <summary>
  1053. /// Gets size of data stored to generation.
  1054. /// </summary>
  1055. long Size { get; }
  1056. /// <summary>
  1057. /// Clear all elements from the generation and make generation new again.
  1058. /// </summary>
  1059. /// <remarks>
  1060. /// When generation is new, it is allowed to add new elements to it and
  1061. /// <see cref="TryGetValue"/>doesn't remove elements from it.
  1062. /// </remarks>
  1063. /// <seealso cref="MakeOld"/>
  1064. void Clear();
  1065. /// <summary>
  1066. /// Determines whether the <see cref="IGeneration"/> contains an element with the specific key.
  1067. /// </summary>
  1068. /// <param name="bucketIndex">
  1069. /// The bucket index for the <see cref="key"/> to locate in <see cref="IGeneration"/>.
  1070. /// </param>
  1071. /// <param name="key">
  1072. /// The key to locate in the <see cref="IGeneration"/>.
  1073. /// </param>
  1074. /// <returns>
  1075. /// <see langword="true"/>if the <see cref="IGeneration"/> contains an element with the <see cref="key"/>;
  1076. /// otherwise <see langword="false"/>.
  1077. /// </returns>
  1078. bool Contains(int bucketIndex, TKey key);
  1079. /// <summary>
  1080. /// Make from generation old generation.
  1081. /// </summary>
  1082. /// <remarks>
  1083. /// When generation is old, <see cref="TryGetValue"/> hit removes element from the generation.
  1084. /// </remarks>
  1085. /// <seealso cref="Clear"/>
  1086. void MakeOld();
  1087. /// <summary>
  1088. /// Remove element associated with the key from the generation.
  1089. /// </summary>
  1090. /// <param name="bucketIndex">
  1091. /// The element's bucket index.
  1092. /// </param>
  1093. /// <param name="key">
  1094. /// The element's key.
  1095. /// </param>
  1096. /// <returns>
  1097. /// <see langword="true"/>, if remove was successful; otherwise <see langword="false"/>.
  1098. /// </returns>
  1099. bool Remove(int bucketIndex, TKey key);
  1100. /// <summary>
  1101. /// Set or add element to generation.
  1102. /// </summary>
  1103. /// <param name="bucketIndex">
  1104. /// The element's bucket index.
  1105. /// </param>
  1106. /// <param name="key">
  1107. /// The element's key.
  1108. /// </param>
  1109. /// <param name="value">
  1110. /// The element's value.
  1111. /// </param>
  1112. /// <param name="size">
  1113. /// The element's size.
  1114. /// </param>
  1115. /// <returns>
  1116. /// <see langword="true"/>, if setting or adding was successful; otherwise <see langword="false"/>.
  1117. /// </returns>
  1118. /// <remarks>
  1119. /// <para>
  1120. /// If element was already existing in generation and new element size fits to collection limits,
  1121. /// then it's value is replaced with new one and size information is updated. If element didn't
  1122. /// exists in generation before, then generation must have empty space for a new element and
  1123. /// size must fit generation's limits, before element is added to generation.
  1124. /// </para>
  1125. /// </remarks>
  1126. bool Set(int bucketIndex, TKey key, TValue value, long size);
  1127. /// <summary>
  1128. /// Try to get element associated with key.
  1129. /// </summary>
  1130. /// <param name="bucketIndex">
  1131. /// The element's bucket index.
  1132. /// </param>
  1133. /// <param name="key">
  1134. /// The element's key.
  1135. /// </param>
  1136. /// <param name="value">
  1137. /// The element's value.
  1138. /// </param>
  1139. /// <param name="size">
  1140. /// The element's size.
  1141. /// </param>
  1142. /// <returns>
  1143. /// <see langword="true"/>, if element was successful retrieved; otherwise <see langword="false"/>.
  1144. /// </returns>
  1145. /// <remarks>
  1146. /// <para>
  1147. /// If element is not found from generation then <paramref name="value"/> and <paramref name="size"/>
  1148. /// are set to default value (default(TValue) and 0).
  1149. /// </para>
  1150. /// </remarks>
  1151. bool TryGetValue(int bucketIndex, TKey key, out TValue value, out long size);
  1152. }
  1153. #endregion
  1154. #region ICnmCache<TKey,TValue> Members
  1155. /// <summary>
  1156. /// Gets current count of elements stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1157. /// </summary>
  1158. /// <remarks>
  1159. /// <para>
  1160. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count,
  1161. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1162. /// </para>
  1163. /// </remarks>
  1164. /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/>
  1165. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1166. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1167. /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/>
  1168. public int Count
  1169. {
  1170. get { return m_newGeneration.Count + m_oldGeneration.Count; }
  1171. }
  1172. /// <summary>
  1173. /// Gets or sets elements expiration time.
  1174. /// </summary>
  1175. /// <value>
  1176. /// Elements expiration time.
  1177. /// </value>
  1178. /// <remarks>
  1179. /// <para>
  1180. /// When element has been stored in <see cref="ICnmCache{TKey,TValue}"/> longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>
  1181. /// and it is not accessed through <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> method or element's value is
  1182. /// not replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method, then it is automatically removed from the
  1183. /// <see cref="ICnmCache{TKey,TValue}"/>.
  1184. /// </para>
  1185. /// <para>
  1186. /// It is possible that <see cref="ICnmCache{TKey,TValue}"/> implementation removes element before it's expiration time,
  1187. /// because total size or count of elements stored to cache is larger than <see cref="ICnmCache{TKey,TValue}.MaxSize"/> or <see cref="ICnmCache{TKey,TValue}.MaxCount"/>.
  1188. /// </para>
  1189. /// <para>
  1190. /// It is also possible that element stays in cache longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>.
  1191. /// </para>
  1192. /// <para>
  1193. /// Calling <see cref="ICnmCache{TKey,TValue}.PurgeExpired"/> try to remove all elements that are expired.
  1194. /// </para>
  1195. /// <para>
  1196. /// To disable time limit in cache, set <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> to <see cref="DateTime.MaxValue"/>.
  1197. /// </para>
  1198. /// </remarks>
  1199. /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/>
  1200. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1201. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1202. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1203. /// <seealso cref="ICnmCache{TKey,TValue}.Count"/>
  1204. /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/>
  1205. /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/>
  1206. /// <seealso cref="ICnmCache{TKey,TValue}.Size"/>
  1207. public TimeSpan ExpirationTime
  1208. {
  1209. get { return m_expirationTime; }
  1210. set
  1211. {
  1212. if (value < MinExpirationTime)
  1213. value = MinExpirationTime;
  1214. if (m_expirationTime == value)
  1215. return;
  1216. m_newGeneration.ExpirationTime = (m_newGeneration.ExpirationTime - m_expirationTime) + value;
  1217. m_oldGeneration.ExpirationTime = (m_oldGeneration.ExpirationTime - m_expirationTime) + value;
  1218. m_expirationTime = value;
  1219. PurgeExpired();
  1220. }
  1221. }
  1222. /// <summary>
  1223. /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting count of elements.
  1224. /// </summary>
  1225. /// <value>
  1226. /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> count of elements is limited;
  1227. /// otherwise, <see langword="false"/>.
  1228. /// </value>
  1229. /// <remarks>
  1230. /// <para>
  1231. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count,
  1232. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1233. /// </para>
  1234. /// </remarks>
  1235. /// <seealso cref="ICnmCache{TKey,TValue}.Count"/>
  1236. /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/>
  1237. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1238. /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/>
  1239. public bool IsCountLimited
  1240. {
  1241. get { return true; }
  1242. }
  1243. /// <summary>
  1244. /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting size of elements.
  1245. /// </summary>
  1246. /// <value>
  1247. /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> total size of elements is limited;
  1248. /// otherwise, <see langword="false"/>.
  1249. /// </value>
  1250. /// <remarks>
  1251. /// <para>
  1252. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements,
  1253. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1254. /// </para>
  1255. /// </remarks>
  1256. /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/>
  1257. /// <seealso cref="ICnmCache{TKey,TValue}.Size"/>
  1258. /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/>
  1259. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1260. /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/>
  1261. public bool IsSizeLimited
  1262. {
  1263. get { return true; }
  1264. }
  1265. /// <summary>
  1266. /// Gets a value indicating whether or not access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe).
  1267. /// </summary>
  1268. /// <value>
  1269. /// <see langword="true"/> if access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe);
  1270. /// otherwise, <see langword="false"/>.
  1271. /// </value>
  1272. /// <remarks>
  1273. /// <para>
  1274. /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/> object, use
  1275. /// <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> in <see cref="CnmSynchronizedCache{TKey,TValue}"/> class
  1276. /// to retrieve synchronized wrapper for <see cref="ICnmCache{TKey,TValue}"/> object.
  1277. /// </para>
  1278. /// </remarks>
  1279. /// <seealso cref="ICnmCache{TKey,TValue}.SyncRoot"/>
  1280. /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/>
  1281. public bool IsSynchronized
  1282. {
  1283. get { return false; }
  1284. }
  1285. /// <summary>
  1286. /// Gets a value indicating whether elements stored to <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time.
  1287. /// </summary>
  1288. /// <value>
  1289. /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> has a fixed total size of elements;
  1290. /// otherwise, <see langword="false"/>.
  1291. /// </value>
  1292. /// <remarks>
  1293. /// If <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time and element is not accessed through <see cref="ICnmCache{TKey,TValue}.Set"/>
  1294. /// or <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> methods in <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> , then element is automatically removed from
  1295. /// the cache. Depending on implementation of the <see cref="ICnmCache{TKey,TValue}"/>, some of the elements may
  1296. /// stay longer in cache.
  1297. /// </remarks>
  1298. /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/>
  1299. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1300. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1301. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1302. public bool IsTimeLimited
  1303. {
  1304. get { return ExpirationTime != TimeSpan.MaxValue; }
  1305. }
  1306. /// <summary>
  1307. /// Gets or sets maximal allowed count of elements that can be stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1308. /// </summary>
  1309. /// <value>
  1310. /// <see cref="int.MaxValue"/>, if <see cref="ICnmCache{TKey,TValue}"/> is not limited by count of elements;
  1311. /// otherwise maximal allowed count of elements.
  1312. /// </value>
  1313. /// <remarks>
  1314. /// <para>
  1315. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count,
  1316. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1317. /// </para>
  1318. /// </remarks>
  1319. public int MaxCount
  1320. {
  1321. get { return m_maxCount; }
  1322. set
  1323. {
  1324. if (value < 8)
  1325. value = 8;
  1326. if (m_maxCount == value)
  1327. return;
  1328. m_maxCount = value;
  1329. Initialize();
  1330. }
  1331. }
  1332. /// <summary>
  1333. /// <para>Gets maximal allowed element size.</para>
  1334. /// </summary>
  1335. /// <value>
  1336. /// Maximal allowed element size.
  1337. /// </value>
  1338. /// <remarks>
  1339. /// <para>
  1340. /// If element's size is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is
  1341. /// not added to the <see cref="ICnmCache{TKey,TValue}"/>.
  1342. /// </para>
  1343. /// </remarks>
  1344. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1345. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1346. /// <seealso cref="ICnmCache{TKey,TValue}.Size"/>
  1347. /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/>
  1348. public long MaxElementSize
  1349. {
  1350. get { return m_maxElementSize; }
  1351. private set { m_maxElementSize = value; }
  1352. }
  1353. /// <summary>
  1354. /// Gets or sets maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1355. /// </summary>
  1356. /// <value>
  1357. /// Maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1358. /// </value>
  1359. /// <remarks>
  1360. /// <para>
  1361. /// Normally size is total bytes used by elements in the cache. But it can be any other suitable unit of measure.
  1362. /// </para>
  1363. /// <para>
  1364. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements,
  1365. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1366. /// </para>
  1367. /// </remarks>
  1368. /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/>
  1369. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1370. /// <seealso cref="ICnmCache{TKey,TValue}.Size"/>
  1371. public long MaxSize
  1372. {
  1373. get { return m_maxSize; }
  1374. set
  1375. {
  1376. if (value < 8)
  1377. value = 8;
  1378. if (m_maxSize == value)
  1379. return;
  1380. m_maxSize = value;
  1381. Initialize();
  1382. }
  1383. }
  1384. /// <summary>
  1385. /// Gets total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1386. /// </summary>
  1387. /// <value>
  1388. /// Total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>.
  1389. /// </value>
  1390. /// <remarks>
  1391. /// <para>
  1392. /// Normally bytes, but can be any suitable unit of measure.
  1393. /// </para>
  1394. /// <para>
  1395. /// Element's size is given when element is added or replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method.
  1396. /// </para>
  1397. /// <para>
  1398. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements,
  1399. /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element.
  1400. /// </para>
  1401. /// </remarks>
  1402. /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/>
  1403. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1404. /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/>
  1405. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1406. /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/>
  1407. public long Size
  1408. {
  1409. get { return m_newGeneration.Size + m_oldGeneration.Size; }
  1410. }
  1411. /// <summary>
  1412. /// Gets an object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>.
  1413. /// </summary>
  1414. /// <value>
  1415. /// An object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>.
  1416. /// </value>
  1417. /// <remarks>
  1418. /// <para>
  1419. /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/>, use <see cref="CnmSynchronizedCache{TKey,TValue}"/>
  1420. /// method <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> to retrieve synchronized wrapper interface to
  1421. /// <see cref="ICnmCache{TKey,TValue}"/>.
  1422. /// </para>
  1423. /// </remarks>
  1424. /// <seealso cref="ICnmCache{TKey,TValue}.IsSynchronized"/>
  1425. /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/>
  1426. public object SyncRoot
  1427. {
  1428. get { return m_syncRoot; }
  1429. }
  1430. /// <summary>
  1431. /// Removes all elements from the <see cref="ICnmCache{TKey,TValue}"/>.
  1432. /// </summary>
  1433. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1434. /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/>
  1435. /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/>
  1436. /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/>
  1437. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1438. public void Clear()
  1439. {
  1440. m_newGeneration.Clear();
  1441. m_oldGeneration.Clear();
  1442. m_oldGeneration.MakeOld();
  1443. m_version++;
  1444. }
  1445. /// <summary>
  1446. /// Returns an enumerator that iterates through the elements stored to <see cref="CnmMemoryCache{TKey,TValue}"/>.
  1447. /// </summary>
  1448. /// <returns>
  1449. /// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
  1450. /// </returns>
  1451. /// <filterpriority>1</filterpriority>
  1452. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  1453. {
  1454. return new Enumerator(this);
  1455. }
  1456. /// <summary>
  1457. /// Purge expired elements from the <see cref="ICnmCache{TKey,TValue}"/>.
  1458. /// </summary>
  1459. /// <remarks>
  1460. /// <para>
  1461. /// Element becomes expired when last access time to it has been longer time than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>.
  1462. /// </para>
  1463. /// <para>
  1464. /// Depending on <see cref="ICnmCache{TKey,TValue}"/> implementation, some of expired elements
  1465. /// may stay longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> in the cache.
  1466. /// </para>
  1467. /// </remarks>
  1468. /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/>
  1469. /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/>
  1470. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1471. /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/>
  1472. /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/>
  1473. /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/>
  1474. /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/>
  1475. public void PurgeExpired()
  1476. {
  1477. m_operationsBetweenTimeChecks = DefaultOperationsBetweenTimeChecks;
  1478. if (!IsTimeLimited)
  1479. return;
  1480. DateTime now = DateTime.Now;
  1481. if (m_newGeneration.AccessedSinceLastTimeCheck)
  1482. {
  1483. // New generation has been accessed since last check
  1484. // Update it's expiration time.
  1485. m_newGeneration.ExpirationTime = now + ExpirationTime;
  1486. m_newGeneration.AccessedSinceLastTimeCheck = false;
  1487. }
  1488. else if (m_newGeneration.ExpirationTime < now)
  1489. {
  1490. // New generation has been expired.
  1491. // --> also old generation must be expired.
  1492. PurgeGeneration(m_newGeneration);
  1493. PurgeGeneration(m_oldGeneration);
  1494. return;
  1495. }
  1496. if (m_oldGeneration.ExpirationTime < now)
  1497. PurgeGeneration(m_oldGeneration);
  1498. }
  1499. /// <summary>
  1500. /// Removes element associated with <paramref name="key"/> from the <see cref="ICnmCache{TKey,TValue}"/>.
  1501. /// </summary>
  1502. /// <param name="key">
  1503. /// The key that is associated with element to remove from the <see cref="ICnmCache{TKey,TValue}"/>.
  1504. /// </param>
  1505. /// <exception cref="ArgumentNullException">
  1506. /// <paramref name="key"/>is <see langword="null"/>.
  1507. /// </exception>
  1508. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1509. /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/>
  1510. /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/>
  1511. /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/>
  1512. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1513. public void Remove(TKey key)
  1514. {
  1515. if (key == null)
  1516. throw new ArgumentNullException("key");
  1517. int bucketIndex = GetBucketIndex(key);
  1518. if (!m_newGeneration.Remove(bucketIndex, key))
  1519. {
  1520. if (!m_oldGeneration.Remove(bucketIndex, key))
  1521. {
  1522. CheckExpired();
  1523. return;
  1524. }
  1525. }
  1526. CheckExpired();
  1527. m_version++;
  1528. }
  1529. /// <summary>
  1530. /// Removes elements that are associated with one of <paramref name="keys"/> from the <see cref="ICnmCache{TKey,TValue}"/>.
  1531. /// </summary>
  1532. /// <param name="keys">
  1533. /// The keys that are associated with elements to remove from the <see cref="ICnmCache{TKey,TValue}"/>.
  1534. /// </param>
  1535. /// <exception cref="ArgumentNullException">
  1536. /// <paramref name="keys"/>is <see langword="null"/>.
  1537. /// </exception>
  1538. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1539. /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/>
  1540. /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/>
  1541. /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/>
  1542. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1543. public void RemoveRange(IEnumerable<TKey> keys)
  1544. {
  1545. if (keys == null)
  1546. throw new ArgumentNullException("keys");
  1547. foreach (TKey key in keys)
  1548. {
  1549. if (key == null)
  1550. continue;
  1551. int bucketIndex = GetBucketIndex(key);
  1552. if (!m_newGeneration.Remove(bucketIndex, key))
  1553. m_oldGeneration.Remove(bucketIndex, key);
  1554. }
  1555. CheckExpired();
  1556. m_version++;
  1557. }
  1558. /// <summary>
  1559. /// Add or replace an element with the provided <paramref name="key"/>, <paramref name="value"/> and <paramref name="size"/> to
  1560. /// <see cref="ICnmCache{TKey,TValue}"/>.
  1561. /// </summary>
  1562. /// <param name="key">
  1563. /// The object used as the key of the element. Can't be <see langword="null"/> reference.
  1564. /// </param>
  1565. /// <param name="value">
  1566. /// The object used as the value of the element to add or replace. <see langword="null"/> is allowed.
  1567. /// </param>
  1568. /// <param name="size">
  1569. /// The element's size. Normally bytes, but can be any suitable unit of measure.
  1570. /// </param>
  1571. /// <returns>
  1572. /// <see langword="true"/>if element has been added successfully to the <see cref="ICnmCache{TKey,TValue}"/>;
  1573. /// otherwise <see langword="false"/>.
  1574. /// </returns>
  1575. /// <exception cref="ArgumentNullException">
  1576. /// <paramref name="key"/>is <see langword="null"/>.
  1577. /// </exception>
  1578. /// <exception cref="ArgumentOutOfRangeException">
  1579. /// The element's <paramref name="size"/> is less than 0.
  1580. /// </exception>
  1581. /// <remarks>
  1582. /// <para>
  1583. /// If element's <paramref name="size"/> is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is
  1584. /// not added to the <see cref="ICnmCache{TKey,TValue}"/>, however - possible older element is
  1585. /// removed from the <see cref="ICnmCache{TKey,TValue}"/>.
  1586. /// </para>
  1587. /// <para>
  1588. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements,
  1589. /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element.
  1590. /// </para>
  1591. /// <para>
  1592. /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count,
  1593. /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element.
  1594. /// </para>
  1595. /// </remarks>
  1596. /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/>
  1597. /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/>
  1598. /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/>
  1599. /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/>
  1600. /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/>
  1601. /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/>
  1602. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1603. public bool Set(TKey key, TValue value, long size)
  1604. {
  1605. if (key == null)
  1606. throw new ArgumentNullException("key");
  1607. if (size < 0)
  1608. throw new ArgumentOutOfRangeException("size", size, "Value's size can't be less than 0.");
  1609. if (size > MaxElementSize)
  1610. {
  1611. // Entry size is too big to fit cache - ignore it
  1612. Remove(key);
  1613. return false;
  1614. }
  1615. if (size == 0)
  1616. size = 1;
  1617. int bucketIndex = GetBucketIndex(key);
  1618. m_oldGeneration.Remove(bucketIndex, key);
  1619. AddToNewGeneration(bucketIndex, key, value, size);
  1620. CheckExpired();
  1621. return true;
  1622. }
  1623. /// <summary>
  1624. /// Gets the <paramref name="value"/> associated with the specified <paramref name="key"/>.
  1625. /// </summary>
  1626. /// <returns>
  1627. /// <see langword="true"/>if the <see cref="ICnmCache{TKey,TValue}"/> contains an element with
  1628. /// the specified key; otherwise, <see langword="false"/>.
  1629. /// </returns>
  1630. /// <param name="key">
  1631. /// The key whose <paramref name="value"/> to get.
  1632. /// </param>
  1633. /// <param name="value">
  1634. /// When this method returns, the value associated with the specified <paramref name="key"/>,
  1635. /// if the <paramref name="key"/> is found; otherwise, the
  1636. /// default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.
  1637. /// </param>
  1638. /// <exception cref="ArgumentNullException">
  1639. /// <paramref name="key"/>is <see langword="null"/>.
  1640. /// </exception>
  1641. /// <seealso cref="ICnmCache{TKey,TValue}.Set"/>
  1642. /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/>
  1643. /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/>
  1644. /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/>
  1645. /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/>
  1646. public bool TryGetValue(TKey key, out TValue value)
  1647. {
  1648. if (key == null)
  1649. throw new ArgumentNullException("key");
  1650. int bucketIndex = GetBucketIndex(key);
  1651. long size;
  1652. if (m_newGeneration.TryGetValue(bucketIndex, key, out value, out size))
  1653. {
  1654. CheckExpired();
  1655. return true;
  1656. }
  1657. if (m_oldGeneration.TryGetValue(bucketIndex, key, out value, out size))
  1658. {
  1659. // Move element to new generation
  1660. AddToNewGeneration(bucketIndex, key, value, size);
  1661. CheckExpired();
  1662. return true;
  1663. }
  1664. CheckExpired();
  1665. return false;
  1666. }
  1667. /// <summary>
  1668. /// Returns an enumerator that iterates through a collection.
  1669. /// </summary>
  1670. /// <returns>
  1671. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  1672. /// </returns>
  1673. /// <filterpriority>2</filterpriority>
  1674. IEnumerator IEnumerable.GetEnumerator()
  1675. {
  1676. return GetEnumerator();
  1677. }
  1678. #endregion
  1679. }
  1680. }