diff options
-rw-r--r-- | CONTRIBUTORS.txt | 1 | ||||
-rw-r--r-- | OpenSim/Framework/CnmMemoryCache.cs | 1852 | ||||
-rw-r--r-- | OpenSim/Framework/CnmSynchronizedCache.cs | 746 | ||||
-rw-r--r-- | OpenSim/Framework/ICnmCache.cs | 441 | ||||
-rw-r--r-- | OpenSim/Framework/PrimeNumberHelper.cs | 98 | ||||
-rw-r--r-- | OpenSim/Region/CoreModules/Asset/CenomeAssetCache.cs | 382 | ||||
-rw-r--r-- | OpenSim/Region/CoreModules/Resources/CoreModulePlugin.addin.xml | 1 | ||||
-rw-r--r-- | bin/config-include/GridCommon.ini.example | 8 | ||||
-rw-r--r-- | bin/config-include/StandaloneCommon.ini.example | 8 |
9 files changed, 3537 insertions, 0 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index e774812..6917c59 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt | |||
@@ -73,6 +73,7 @@ what it is today. | |||
73 | * Ewe Loon | 73 | * Ewe Loon |
74 | * Fly-Man | 74 | * Fly-Man |
75 | * Flyte Xevious | 75 | * Flyte Xevious |
76 | * Imaze Rhiano | ||
76 | * Intimidated | 77 | * Intimidated |
77 | * Jeremy Bongio (IBM) | 78 | * Jeremy Bongio (IBM) |
78 | * jhurliman | 79 | * jhurliman |
diff --git a/OpenSim/Framework/CnmMemoryCache.cs b/OpenSim/Framework/CnmMemoryCache.cs new file mode 100644 index 0000000..6ca71ec --- /dev/null +++ b/OpenSim/Framework/CnmMemoryCache.cs | |||
@@ -0,0 +1,1852 @@ | |||
1 | // -------------------------------------------------------------------------------------------------------------------- | ||
2 | // <copyright company="" file="CnmMemoryCache.cs"> | ||
3 | // | ||
4 | // </copyright> | ||
5 | // <summary> | ||
6 | // | ||
7 | // </summary> | ||
8 | // -------------------------------------------------------------------------------------------------------------------- | ||
9 | |||
10 | using System; | ||
11 | using System.Collections; | ||
12 | using System.Collections.Generic; | ||
13 | using System.Diagnostics; | ||
14 | |||
15 | namespace OpenSim.Framework | ||
16 | { | ||
17 | /// <summary> | ||
18 | /// Cenome memory based cache to store key/value pairs (elements) limited time and/or limited size. | ||
19 | /// </summary> | ||
20 | /// <typeparam name="TKey"> | ||
21 | /// The type of keys in the cache. | ||
22 | /// </typeparam> | ||
23 | /// <typeparam name="TValue"> | ||
24 | /// The type of values in the dictionary. | ||
25 | /// </typeparam> | ||
26 | /// <remarks> | ||
27 | /// <para> | ||
28 | /// Cenome memory cache stores elements to hash table generations. When new element is being added to cache, and new size would exceed | ||
29 | /// maximal allowed size or maximal amount of allowed element count, then elements in oldest generation are deleted. Last access time | ||
30 | /// is also tracked in generation level - thus it is possible that some elements are staying in cache far beyond their expiration time. | ||
31 | /// If elements in older generations are accessed through <see cref="TryGetValue"/> method, they are moved to newest generation. | ||
32 | /// </para> | ||
33 | /// </remarks> | ||
34 | public class CnmMemoryCache<TKey, TValue> : ICnmCache<TKey, TValue> | ||
35 | { | ||
36 | /// <summary> | ||
37 | /// Default maximal count. | ||
38 | /// </summary> | ||
39 | /// <seealso cref="MaxCount"/> | ||
40 | public const int DefaultMaxCount = 4096; | ||
41 | |||
42 | /// <summary> | ||
43 | /// Default maximal size. | ||
44 | /// </summary> | ||
45 | /// <remarks> | ||
46 | /// <para> | ||
47 | /// 128MB = 128 * 1024^2 = 134 217 728 bytes. | ||
48 | /// </para> | ||
49 | /// </remarks> | ||
50 | /// <seealso cref="MaxSize"/> | ||
51 | public const long DefaultMaxSize = 134217728; | ||
52 | |||
53 | /// <summary> | ||
54 | /// How many operations between time checks. | ||
55 | /// </summary> | ||
56 | private const int DefaultOperationsBetweenTimeChecks = 40; | ||
57 | |||
58 | /// <summary> | ||
59 | /// Default expiration time. | ||
60 | /// </summary> | ||
61 | /// <remarks> | ||
62 | /// <para> | ||
63 | /// 30 minutes. | ||
64 | /// </para> | ||
65 | /// </remarks> | ||
66 | public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes( 30.0 ); | ||
67 | |||
68 | /// <summary> | ||
69 | /// Minimal allowed expiration time. | ||
70 | /// </summary> | ||
71 | /// <remarks> | ||
72 | /// <para> | ||
73 | /// 5 minutes. | ||
74 | /// </para> | ||
75 | /// </remarks> | ||
76 | public static readonly TimeSpan MinExpirationTime = TimeSpan.FromSeconds( 10.0 ); | ||
77 | |||
78 | /// <summary> | ||
79 | /// Comparer used to compare element keys. | ||
80 | /// </summary> | ||
81 | /// <remarks> | ||
82 | /// Comparer is initialized by constructor. | ||
83 | /// </remarks> | ||
84 | /// <seealso cref="CnmMemoryCache{TKey,TValue}"/> | ||
85 | public readonly IEqualityComparer<TKey> Comparer; | ||
86 | |||
87 | /// <summary> | ||
88 | /// Expiration time. | ||
89 | /// </summary> | ||
90 | private TimeSpan m_expirationTime = DefaultExpirationTime; | ||
91 | |||
92 | /// <summary> | ||
93 | /// Generation bucket count. | ||
94 | /// </summary> | ||
95 | private int m_generationBucketCount; | ||
96 | |||
97 | /// <summary> | ||
98 | /// Generation entry count. | ||
99 | /// </summary> | ||
100 | private int m_generationElementCount; | ||
101 | |||
102 | /// <summary> | ||
103 | /// Generation max size. | ||
104 | /// </summary> | ||
105 | private long m_generationMaxSize; | ||
106 | |||
107 | /// <summary> | ||
108 | /// Maximal allowed count of elements. | ||
109 | /// </summary> | ||
110 | private int m_maxCount; | ||
111 | |||
112 | /// <summary> | ||
113 | /// Maximal allowed total size of elements. | ||
114 | /// </summary> | ||
115 | private long m_maxElementSize; | ||
116 | |||
117 | /// <summary> | ||
118 | /// Maximal size. | ||
119 | /// </summary> | ||
120 | private long m_maxSize; | ||
121 | |||
122 | /// <summary> | ||
123 | /// New generation. | ||
124 | /// </summary> | ||
125 | private IGeneration m_newGeneration; | ||
126 | |||
127 | /// <summary> | ||
128 | /// Old generation. | ||
129 | /// </summary> | ||
130 | private IGeneration m_oldGeneration; | ||
131 | |||
132 | /// <summary> | ||
133 | /// Operations between time check. | ||
134 | /// </summary> | ||
135 | private int m_operationsBetweenTimeChecks = DefaultOperationsBetweenTimeChecks; | ||
136 | |||
137 | /// <summary> | ||
138 | /// Synchronization root object, should always be private and exists always | ||
139 | /// </summary> | ||
140 | private readonly object m_syncRoot = new object(); | ||
141 | |||
142 | /// <summary> | ||
143 | /// Version of cache. | ||
144 | /// </summary> | ||
145 | /// <remarks> | ||
146 | /// <para> | ||
147 | /// Updated every time when cache has been changed (element removed, expired, added, replaced). | ||
148 | /// </para> | ||
149 | /// </remarks> | ||
150 | private int m_version; | ||
151 | |||
152 | /// <summary> | ||
153 | /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class. | ||
154 | /// </summary> | ||
155 | public CnmMemoryCache() | ||
156 | : this( DefaultMaxSize ) | ||
157 | { | ||
158 | } | ||
159 | |||
160 | /// <summary> | ||
161 | /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class. | ||
162 | /// </summary> | ||
163 | /// <param name="maximalSize"> | ||
164 | /// Maximal cache size. | ||
165 | /// </param> | ||
166 | public CnmMemoryCache( long maximalSize ) | ||
167 | : this( maximalSize, DefaultMaxCount ) | ||
168 | { | ||
169 | } | ||
170 | |||
171 | /// <summary> | ||
172 | /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class. | ||
173 | /// </summary> | ||
174 | /// <param name="maximalSize"> | ||
175 | /// Maximal cache size. | ||
176 | /// </param> | ||
177 | /// <param name="maximalCount"> | ||
178 | /// Maximal element count. | ||
179 | /// </param> | ||
180 | public CnmMemoryCache( long maximalSize, int maximalCount ) | ||
181 | : this( maximalSize, maximalCount, DefaultExpirationTime ) | ||
182 | { | ||
183 | } | ||
184 | |||
185 | /// <summary> | ||
186 | /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class. | ||
187 | /// </summary> | ||
188 | /// <param name="maximalSize"> | ||
189 | /// Maximal cache size. | ||
190 | /// </param> | ||
191 | /// <param name="maximalCount"> | ||
192 | /// Maximal element count. | ||
193 | /// </param> | ||
194 | /// <param name="expirationTime"> | ||
195 | /// Elements expiration time. | ||
196 | /// </param> | ||
197 | public CnmMemoryCache( long maximalSize, int maximalCount, TimeSpan expirationTime ) | ||
198 | : this( maximalSize, maximalCount, expirationTime, EqualityComparer<TKey>.Default ) | ||
199 | { | ||
200 | } | ||
201 | |||
202 | /// <summary> | ||
203 | /// Initializes a new instance of the <see cref="CnmMemoryCache{TKey,TValue}"/> class. | ||
204 | /// </summary> | ||
205 | /// <param name="maximalSize"> | ||
206 | /// Maximal cache size. | ||
207 | /// </param> | ||
208 | /// <param name="maximalCount"> | ||
209 | /// Maximal element count. | ||
210 | /// </param> | ||
211 | /// <param name="expirationTime"> | ||
212 | /// Elements expiration time. | ||
213 | /// </param> | ||
214 | /// <param name="comparer"> | ||
215 | /// Comparer used for comparing elements. | ||
216 | /// </param> | ||
217 | /// <exception cref="ArgumentNullException"> | ||
218 | /// <see cref="comparer"/>is <see langword="null"/> reference. | ||
219 | /// </exception> | ||
220 | public CnmMemoryCache( long maximalSize, | ||
221 | int maximalCount, | ||
222 | TimeSpan expirationTime, | ||
223 | IEqualityComparer<TKey> comparer ) | ||
224 | { | ||
225 | if( comparer == null ) | ||
226 | throw new ArgumentNullException( "comparer" ); | ||
227 | |||
228 | if( expirationTime < MinExpirationTime ) | ||
229 | expirationTime = MinExpirationTime; | ||
230 | if( maximalCount < 8 ) | ||
231 | maximalCount = 8; | ||
232 | if( maximalSize < 8 ) | ||
233 | maximalSize = 8; | ||
234 | if( maximalCount > maximalSize ) | ||
235 | maximalCount = (int) maximalSize; | ||
236 | |||
237 | Comparer = comparer; | ||
238 | m_expirationTime = expirationTime; | ||
239 | m_maxSize = maximalSize; | ||
240 | m_maxCount = maximalCount; | ||
241 | |||
242 | Initialize(); | ||
243 | } | ||
244 | |||
245 | /// <summary> | ||
246 | /// Add element to new generation. | ||
247 | /// </summary> | ||
248 | /// <param name="bucketIndex"> | ||
249 | /// The bucket index. | ||
250 | /// </param> | ||
251 | /// <param name="key"> | ||
252 | /// The element's key. | ||
253 | /// </param> | ||
254 | /// <param name="value"> | ||
255 | /// The element's value. | ||
256 | /// </param> | ||
257 | /// <param name="size"> | ||
258 | /// The element's size. | ||
259 | /// </param> | ||
260 | protected virtual void AddToNewGeneration( int bucketIndex, TKey key, TValue value, long size ) | ||
261 | { | ||
262 | // Add to newest generation | ||
263 | if( !m_newGeneration.Set( bucketIndex, key, value, size ) ) | ||
264 | { | ||
265 | // Failed to add new generation | ||
266 | RecycleGenerations(); | ||
267 | m_newGeneration.Set( bucketIndex, key, value, size ); | ||
268 | } | ||
269 | |||
270 | m_version++; | ||
271 | } | ||
272 | |||
273 | /// <summary> | ||
274 | /// <para> | ||
275 | /// Get keys bucket index. | ||
276 | /// </para> | ||
277 | /// </summary> | ||
278 | /// <param name="key"> | ||
279 | /// <para> | ||
280 | /// Key which bucket index is being retrieved. | ||
281 | /// </para> | ||
282 | /// </param> | ||
283 | /// <returns> | ||
284 | /// <para> | ||
285 | /// Bucket index. | ||
286 | /// </para> | ||
287 | /// </returns> | ||
288 | /// <remarks> | ||
289 | /// <para> | ||
290 | /// Method uses <see cref="Comparer"/> to calculate <see cref="key"/> hash code. | ||
291 | /// </para> | ||
292 | /// <para> | ||
293 | /// Bucket index is remainder when element key's hash value is divided by bucket count. | ||
294 | /// </para> | ||
295 | /// <para> | ||
296 | /// For example: key's hash is 72, bucket count is 5, element's bucket index is 72 % 5 = 2. | ||
297 | /// </para> | ||
298 | /// </remarks> | ||
299 | protected virtual int GetBucketIndex( TKey key ) | ||
300 | { | ||
301 | return (Comparer.GetHashCode( key ) & 0x7FFFFFFF) % m_generationBucketCount; | ||
302 | } | ||
303 | |||
304 | /// <summary> | ||
305 | /// Purge generation from the cache. | ||
306 | /// </summary> | ||
307 | /// <param name="generation"> | ||
308 | /// The generation that is purged. | ||
309 | /// </param> | ||
310 | protected virtual void PurgeGeneration( IGeneration generation ) | ||
311 | { | ||
312 | generation.Clear(); | ||
313 | m_version++; | ||
314 | } | ||
315 | |||
316 | /// <summary> | ||
317 | /// check expired. | ||
318 | /// </summary> | ||
319 | private void CheckExpired() | ||
320 | { | ||
321 | // Do this only one in every m_operationsBetweenTimeChecks | ||
322 | // Fetching time is using several millisecons - it is better not to do all time. | ||
323 | m_operationsBetweenTimeChecks--; | ||
324 | if( m_operationsBetweenTimeChecks <= 0 ) | ||
325 | PurgeExpired(); | ||
326 | } | ||
327 | |||
328 | /// <summary> | ||
329 | /// Initialize cache. | ||
330 | /// </summary> | ||
331 | private void Initialize() | ||
332 | { | ||
333 | m_version++; | ||
334 | |||
335 | m_generationMaxSize = MaxSize / 2; | ||
336 | MaxElementSize = MaxSize / 8; | ||
337 | m_generationElementCount = MaxCount / 2; | ||
338 | |||
339 | // Buckets need to be prime number to get better spread of hash values | ||
340 | m_generationBucketCount = PrimeNumberHelper.GetPrime( m_generationElementCount ); | ||
341 | |||
342 | m_newGeneration = new HashGeneration( this ); | ||
343 | m_oldGeneration = new HashGeneration( this ); | ||
344 | m_oldGeneration.MakeOld(); | ||
345 | } | ||
346 | |||
347 | /// <summary> | ||
348 | /// Recycle generations. | ||
349 | /// </summary> | ||
350 | private void RecycleGenerations() | ||
351 | { | ||
352 | // Rotate old generation to new generation, new generation to old generation | ||
353 | IGeneration temp = m_newGeneration; | ||
354 | m_newGeneration = m_oldGeneration; | ||
355 | m_newGeneration.Clear(); | ||
356 | m_oldGeneration = temp; | ||
357 | m_oldGeneration.MakeOld(); | ||
358 | } | ||
359 | |||
360 | #region Nested type: Enumerator | ||
361 | |||
362 | /// <summary> | ||
363 | /// Key and value pair enumerator. | ||
364 | /// </summary> | ||
365 | private class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> | ||
366 | { | ||
367 | /// <summary> | ||
368 | /// Current enumerator. | ||
369 | /// </summary> | ||
370 | private int m_currentEnumerator = -1; | ||
371 | |||
372 | /// <summary> | ||
373 | /// Enumerators to different generations. | ||
374 | /// </summary> | ||
375 | private readonly IEnumerator<KeyValuePair<TKey, TValue>>[] m_generationEnumerators = | ||
376 | new IEnumerator<KeyValuePair<TKey, TValue>>[2]; | ||
377 | |||
378 | /// <summary> | ||
379 | /// Initializes a new instance of the <see cref="Enumerator"/> class. | ||
380 | /// </summary> | ||
381 | /// <param name="cache"> | ||
382 | /// The cache. | ||
383 | /// </param> | ||
384 | public Enumerator( CnmMemoryCache<TKey, TValue> cache ) | ||
385 | { | ||
386 | m_generationEnumerators[ 0 ] = cache.m_newGeneration.GetEnumerator(); | ||
387 | m_generationEnumerators[ 1 ] = cache.m_oldGeneration.GetEnumerator(); | ||
388 | } | ||
389 | |||
390 | #region IEnumerator<KeyValuePair<TKey,TValue>> Members | ||
391 | |||
392 | /// <summary> | ||
393 | /// Gets the element in the collection at the current position of the enumerator. | ||
394 | /// </summary> | ||
395 | /// <returns> | ||
396 | /// The element in the collection at the current position of the enumerator. | ||
397 | /// </returns> | ||
398 | /// <exception cref="InvalidOperationException"> | ||
399 | /// The enumerator has reach end of collection or <see cref="MoveNext"/> is not called. | ||
400 | /// </exception> | ||
401 | public KeyValuePair<TKey, TValue> Current | ||
402 | { | ||
403 | get | ||
404 | { | ||
405 | if( m_currentEnumerator == -1 || m_currentEnumerator >= m_generationEnumerators.Length ) | ||
406 | throw new InvalidOperationException(); | ||
407 | |||
408 | return m_generationEnumerators[ m_currentEnumerator ].Current; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | /// <summary> | ||
413 | /// Gets the current element in the collection. | ||
414 | /// </summary> | ||
415 | /// <returns> | ||
416 | /// The current element in the collection. | ||
417 | /// </returns> | ||
418 | /// <exception cref="T:System.InvalidOperationException"> | ||
419 | /// The enumerator is positioned before the first element of the collection or after the last element. | ||
420 | /// </exception><filterpriority>2</filterpriority> | ||
421 | object IEnumerator.Current | ||
422 | { | ||
423 | get { return Current; } | ||
424 | } | ||
425 | |||
426 | /// <summary> | ||
427 | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
428 | /// </summary> | ||
429 | /// <filterpriority>2</filterpriority> | ||
430 | public void Dispose() | ||
431 | { | ||
432 | } | ||
433 | |||
434 | /// <summary> | ||
435 | /// Advances the enumerator to the next element of the collection. | ||
436 | /// </summary> | ||
437 | /// <returns> | ||
438 | /// <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. | ||
439 | /// </returns> | ||
440 | /// <exception cref="T:System.InvalidOperationException"> | ||
441 | /// The collection was modified after the enumerator was created. | ||
442 | /// </exception> | ||
443 | /// <filterpriority>2</filterpriority> | ||
444 | public bool MoveNext() | ||
445 | { | ||
446 | if( m_currentEnumerator == -1 ) | ||
447 | m_currentEnumerator = 0; | ||
448 | |||
449 | while( m_currentEnumerator < m_generationEnumerators.Length ) | ||
450 | { | ||
451 | if( m_generationEnumerators[ m_currentEnumerator ].MoveNext() ) | ||
452 | return true; | ||
453 | |||
454 | m_currentEnumerator++; | ||
455 | } | ||
456 | |||
457 | return false; | ||
458 | } | ||
459 | |||
460 | /// <summary> | ||
461 | /// Sets the enumerator to its initial position, which is before the first element in the collection. | ||
462 | /// </summary> | ||
463 | /// <exception cref="T:System.InvalidOperationException"> | ||
464 | /// The collection was modified after the enumerator was created. | ||
465 | /// </exception> | ||
466 | /// <filterpriority>2</filterpriority> | ||
467 | public void Reset() | ||
468 | { | ||
469 | foreach( IEnumerator<KeyValuePair<TKey, TValue>> enumerator in m_generationEnumerators ) | ||
470 | { | ||
471 | enumerator.Reset(); | ||
472 | } | ||
473 | |||
474 | m_currentEnumerator = -1; | ||
475 | } | ||
476 | |||
477 | #endregion | ||
478 | } | ||
479 | |||
480 | #endregion | ||
481 | |||
482 | #region Nested type: HashGeneration | ||
483 | |||
484 | /// <summary> | ||
485 | /// Hash generation class | ||
486 | /// </summary> | ||
487 | /// <remarks> | ||
488 | /// <para> | ||
489 | /// Current implementation is based to separated chaining with move-to-front heuristics. Hash generations have fixed | ||
490 | /// amount of buckets and it is never rehashed. | ||
491 | /// </para> | ||
492 | /// <para> | ||
493 | /// Read more about hash tables from <a href="http://en.wikipedia.org/wiki/Hash_table">Wiki article</a>. | ||
494 | /// </para> | ||
495 | /// </remarks> | ||
496 | /// <seealso href="http://en.wikipedia.org/wiki/Hash_table"/> | ||
497 | private class HashGeneration : IGeneration | ||
498 | { | ||
499 | /// <summary> | ||
500 | /// Value indicating whether generation was accessed since last time check. | ||
501 | /// </summary> | ||
502 | private bool m_accessedSinceLastTimeCheck; | ||
503 | |||
504 | /// <summary> | ||
505 | /// Index of first element's in element chain. | ||
506 | /// </summary> | ||
507 | /// <value> | ||
508 | /// -1 if there is no element in bucket; otherwise first element's index in the element chain. | ||
509 | /// </value> | ||
510 | /// <remarks> | ||
511 | /// Bucket index is remainder when element key's hash value is divided by bucket count. | ||
512 | /// For example: key's hash is 72, bucket count is 5, element's bucket index is 72 % 5 = 2. | ||
513 | /// </remarks> | ||
514 | private readonly int[] m_buckets; | ||
515 | |||
516 | /// <summary> | ||
517 | /// Cache object. | ||
518 | /// </summary> | ||
519 | private readonly CnmMemoryCache<TKey, TValue> m_cache; | ||
520 | |||
521 | /// <summary> | ||
522 | /// Generation's element array. | ||
523 | /// </summary> | ||
524 | /// <seealso cref="Element"/> | ||
525 | private readonly Element[] m_elements; | ||
526 | |||
527 | /// <summary> | ||
528 | /// Generation's expiration time. | ||
529 | /// </summary> | ||
530 | private DateTime m_expirationTime1; | ||
531 | |||
532 | /// <summary> | ||
533 | /// Index to first free element. | ||
534 | /// </summary> | ||
535 | private int m_firstFreeElement; | ||
536 | |||
537 | /// <summary> | ||
538 | /// Free element count. | ||
539 | /// </summary> | ||
540 | /// <remarks> | ||
541 | /// When generation is cleared or constructed, this is NOT set to element count. | ||
542 | /// This is only tracking elements that are removed and are currently free. | ||
543 | /// </remarks> | ||
544 | private int m_freeCount; | ||
545 | |||
546 | /// <summary> | ||
547 | /// Is this generation "new generation". | ||
548 | /// </summary> | ||
549 | private bool m_newGeneration; | ||
550 | |||
551 | /// <summary> | ||
552 | /// Next unused entry. | ||
553 | /// </summary> | ||
554 | private int m_nextUnusedElement; | ||
555 | |||
556 | /// <summary> | ||
557 | /// Size of data stored to generation. | ||
558 | /// </summary> | ||
559 | private long m_size; | ||
560 | |||
561 | /// <summary> | ||
562 | /// Initializes a new instance of the <see cref="HashGeneration"/> class. | ||
563 | /// </summary> | ||
564 | /// <param name="cache"> | ||
565 | /// The cache. | ||
566 | /// </param> | ||
567 | public HashGeneration( CnmMemoryCache<TKey, TValue> cache ) | ||
568 | { | ||
569 | m_cache = cache; | ||
570 | m_elements = new Element[m_cache.m_generationElementCount]; | ||
571 | m_buckets = new int[m_cache.m_generationBucketCount]; | ||
572 | Clear(); | ||
573 | } | ||
574 | |||
575 | /// <summary> | ||
576 | /// Find element's index | ||
577 | /// </summary> | ||
578 | /// <param name="bucketIndex"> | ||
579 | /// The element's bucket index. | ||
580 | /// </param> | ||
581 | /// <param name="key"> | ||
582 | /// The element's key. | ||
583 | /// </param> | ||
584 | /// <param name="moveToFront"> | ||
585 | /// Move element to front of elements. | ||
586 | /// </param> | ||
587 | /// <param name="previousIndex"> | ||
588 | /// The previous element's index. | ||
589 | /// </param> | ||
590 | /// <returns> | ||
591 | /// Element's index, if found from the generation; -1 otherwise (if element is not found the generation). | ||
592 | /// </returns> | ||
593 | private int FindElementIndex( int bucketIndex, TKey key, bool moveToFront, out int previousIndex ) | ||
594 | { | ||
595 | previousIndex = -1; | ||
596 | int elementIndex = m_buckets[ bucketIndex ]; | ||
597 | while( elementIndex >= 0 ) | ||
598 | { | ||
599 | if( m_cache.Comparer.Equals( key, m_elements[ elementIndex ].Key ) ) | ||
600 | { | ||
601 | // Found match | ||
602 | if( moveToFront && previousIndex >= 0 ) | ||
603 | { | ||
604 | // Move entry to front | ||
605 | m_elements[ previousIndex ].Next = m_elements[ elementIndex ].Next; | ||
606 | m_elements[ elementIndex ].Next = m_buckets[ bucketIndex ]; | ||
607 | m_buckets[ bucketIndex ] = elementIndex; | ||
608 | previousIndex = 0; | ||
609 | } | ||
610 | |||
611 | return elementIndex; | ||
612 | } | ||
613 | |||
614 | previousIndex = elementIndex; | ||
615 | elementIndex = m_elements[ elementIndex ].Next; | ||
616 | } | ||
617 | |||
618 | return -1; | ||
619 | } | ||
620 | |||
621 | /// <summary> | ||
622 | /// Remove element front the generation. | ||
623 | /// </summary> | ||
624 | /// <param name="bucketIndex"> | ||
625 | /// The bucket index. | ||
626 | /// </param> | ||
627 | /// <param name="entryIndex"> | ||
628 | /// The element index. | ||
629 | /// </param> | ||
630 | /// <param name="previousIndex"> | ||
631 | /// The element's previous index. | ||
632 | /// </param> | ||
633 | private void RemoveElement( int bucketIndex, int entryIndex, int previousIndex ) | ||
634 | { | ||
635 | if( previousIndex >= 0 ) | ||
636 | m_elements[ previousIndex ].Next = m_elements[ entryIndex ].Next; | ||
637 | else | ||
638 | m_buckets[ bucketIndex ] = m_elements[ entryIndex ].Next; | ||
639 | |||
640 | Size -= m_elements[ entryIndex ].Size; | ||
641 | m_elements[ entryIndex ].Value = default(TValue); | ||
642 | m_elements[ entryIndex ].Key = default(TKey); | ||
643 | |||
644 | // Add element to free elements list | ||
645 | m_elements[ entryIndex ].Next = m_firstFreeElement; | ||
646 | m_firstFreeElement = entryIndex; | ||
647 | m_freeCount++; | ||
648 | } | ||
649 | |||
650 | #region Nested type: Element | ||
651 | |||
652 | /// <summary> | ||
653 | /// Element that stores key, next element in chain, size and value. | ||
654 | /// </summary> | ||
655 | private struct Element | ||
656 | { | ||
657 | /// <summary> | ||
658 | /// Element's key. | ||
659 | /// </summary> | ||
660 | public TKey Key; | ||
661 | |||
662 | /// <summary> | ||
663 | /// Next element in chain. | ||
664 | /// </summary> | ||
665 | /// <remarks> | ||
666 | /// When element have value (something is stored to it), this is index of | ||
667 | /// next element with same bucket index. When element is free, this | ||
668 | /// is index of next element in free element's list. | ||
669 | /// </remarks> | ||
670 | public int Next; | ||
671 | |||
672 | /// <summary> | ||
673 | /// Size of element. | ||
674 | /// </summary> | ||
675 | /// <value> | ||
676 | /// 0 if element is free; otherwise larger than 0. | ||
677 | /// </value> | ||
678 | public long Size; | ||
679 | |||
680 | /// <summary> | ||
681 | /// Element's value. | ||
682 | /// </summary> | ||
683 | /// <remarks> | ||
684 | /// It is possible that this value is <see langword="null"/> even when element | ||
685 | /// have value - element's value is then <see langword="null"/> reference. | ||
686 | /// </remarks> | ||
687 | public TValue Value; | ||
688 | |||
689 | /// <summary> | ||
690 | /// Gets a value indicating whether element is free or have value. | ||
691 | /// </summary> | ||
692 | /// <value> | ||
693 | /// <see langword="true"/> when element is free; otherwise <see langword="false"/>. | ||
694 | /// </value> | ||
695 | public bool IsFree | ||
696 | { | ||
697 | get { return Size == 0; } | ||
698 | } | ||
699 | } | ||
700 | |||
701 | #endregion | ||
702 | |||
703 | #region Nested type: Enumerator | ||
704 | |||
705 | /// <summary> | ||
706 | /// Key value pair enumerator for <see cref="HashGeneration"/> object. | ||
707 | /// </summary> | ||
708 | private class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> | ||
709 | { | ||
710 | /// <summary> | ||
711 | /// Current element. | ||
712 | /// </summary> | ||
713 | private KeyValuePair<TKey, TValue> m_current; | ||
714 | |||
715 | /// <summary> | ||
716 | /// Current index. | ||
717 | /// </summary> | ||
718 | private int m_currentIndex; | ||
719 | |||
720 | /// <summary> | ||
721 | /// Generation that is being enumerated. | ||
722 | /// </summary> | ||
723 | private readonly HashGeneration m_generation; | ||
724 | |||
725 | /// <summary> | ||
726 | /// Cache version. | ||
727 | /// </summary> | ||
728 | /// <remarks> | ||
729 | /// When cache is change, version number is changed. | ||
730 | /// </remarks> | ||
731 | /// <seealso cref="CnmMemoryCache{TKey,TValue}.m_version"/> | ||
732 | private readonly int m_version; | ||
733 | |||
734 | /// <summary> | ||
735 | /// Initializes a new instance of the <see cref="Enumerator"/> class. | ||
736 | /// </summary> | ||
737 | /// <param name="generation"> | ||
738 | /// The generation. | ||
739 | /// </param> | ||
740 | public Enumerator( HashGeneration generation ) | ||
741 | { | ||
742 | m_generation = generation; | ||
743 | m_version = m_generation.m_cache.m_version; | ||
744 | } | ||
745 | |||
746 | #region IEnumerator<KeyValuePair<TKey,TValue>> Members | ||
747 | |||
748 | /// <summary> | ||
749 | /// Gets the element in the collection at the current position of the enumerator. | ||
750 | /// </summary> | ||
751 | /// <returns> | ||
752 | /// The element in the collection at the current position of the enumerator. | ||
753 | /// </returns> | ||
754 | /// <exception cref="InvalidOperationException"> | ||
755 | /// The enumerator has reach end of collection or <see cref="MoveNext"/> is not called. | ||
756 | /// </exception> | ||
757 | public KeyValuePair<TKey, TValue> Current | ||
758 | { | ||
759 | get | ||
760 | { | ||
761 | if( m_currentIndex == 0 || m_currentIndex >= m_generation.Count ) | ||
762 | throw new InvalidOperationException(); | ||
763 | |||
764 | return m_current; | ||
765 | } | ||
766 | } | ||
767 | |||
768 | /// <summary> | ||
769 | /// Gets the current element in the collection. | ||
770 | /// </summary> | ||
771 | /// <returns> | ||
772 | /// The current element in the collection. | ||
773 | /// </returns> | ||
774 | /// <exception cref="InvalidOperationException"> | ||
775 | /// The enumerator is positioned before the first element of the collection or after the last element. | ||
776 | /// </exception><filterpriority>2</filterpriority> | ||
777 | object IEnumerator.Current | ||
778 | { | ||
779 | get { return Current; } | ||
780 | } | ||
781 | |||
782 | /// <summary> | ||
783 | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
784 | /// </summary> | ||
785 | /// <filterpriority>2</filterpriority> | ||
786 | public void Dispose() | ||
787 | { | ||
788 | } | ||
789 | |||
790 | /// <summary> | ||
791 | /// Advances the enumerator to the next element of the collection. | ||
792 | /// </summary> | ||
793 | /// <returns> | ||
794 | /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. | ||
795 | /// </returns> | ||
796 | /// <exception cref="InvalidOperationException"> | ||
797 | /// The collection was modified after the enumerator was created. | ||
798 | /// </exception> | ||
799 | public bool MoveNext() | ||
800 | { | ||
801 | if( m_version != m_generation.m_cache.m_version ) | ||
802 | throw new InvalidOperationException(); | ||
803 | |||
804 | while( m_currentIndex < m_generation.Count ) | ||
805 | { | ||
806 | if( m_generation.m_elements[ m_currentIndex ].IsFree ) | ||
807 | { | ||
808 | m_currentIndex++; | ||
809 | continue; | ||
810 | } | ||
811 | |||
812 | m_current = new KeyValuePair<TKey, TValue>( m_generation.m_elements[ m_currentIndex ].Key, | ||
813 | m_generation.m_elements[ m_currentIndex ].Value ); | ||
814 | m_currentIndex++; | ||
815 | return true; | ||
816 | } | ||
817 | |||
818 | m_current = new KeyValuePair<TKey, TValue>(); | ||
819 | return false; | ||
820 | } | ||
821 | |||
822 | /// <summary> | ||
823 | /// Sets the enumerator to its initial position, which is before the first element in the collection. | ||
824 | /// </summary> | ||
825 | /// <exception cref="InvalidOperationException"> | ||
826 | /// The collection was modified after the enumerator was created. | ||
827 | /// </exception> | ||
828 | /// <filterpriority>2</filterpriority> | ||
829 | public void Reset() | ||
830 | { | ||
831 | if( m_version != m_generation.m_cache.m_version ) | ||
832 | throw new InvalidOperationException(); | ||
833 | |||
834 | m_currentIndex = 0; | ||
835 | } | ||
836 | |||
837 | #endregion | ||
838 | } | ||
839 | |||
840 | #endregion | ||
841 | |||
842 | #region IGeneration Members | ||
843 | |||
844 | /// <summary> | ||
845 | /// Gets or sets a value indicating whether generation was accessed since last time check. | ||
846 | /// </summary> | ||
847 | public bool AccessedSinceLastTimeCheck | ||
848 | { | ||
849 | get { return m_accessedSinceLastTimeCheck; } | ||
850 | |||
851 | set { m_accessedSinceLastTimeCheck = value; } | ||
852 | } | ||
853 | |||
854 | /// <summary> | ||
855 | /// Gets element count in generation. | ||
856 | /// </summary> | ||
857 | public int Count | ||
858 | { | ||
859 | get { return m_nextUnusedElement - m_freeCount; } | ||
860 | } | ||
861 | |||
862 | /// <summary> | ||
863 | /// Gets or sets generation's expiration time. | ||
864 | /// </summary> | ||
865 | public DateTime ExpirationTime | ||
866 | { | ||
867 | get { return m_expirationTime1; } | ||
868 | |||
869 | set { m_expirationTime1 = value; } | ||
870 | } | ||
871 | |||
872 | /// <summary> | ||
873 | /// Gets or sets size of data stored to generation. | ||
874 | /// </summary> | ||
875 | public long Size | ||
876 | { | ||
877 | get { return m_size; } | ||
878 | |||
879 | private set { m_size = value; } | ||
880 | } | ||
881 | |||
882 | /// <summary> | ||
883 | /// Clear all elements from the generation and make generation new again. | ||
884 | /// </summary> | ||
885 | /// <remarks> | ||
886 | /// When generation is new, it is allowed to add new elements to it and | ||
887 | /// <see cref="IGeneration.TryGetValue"/>doesn't remove elements from it. | ||
888 | /// </remarks> | ||
889 | /// <seealso cref="IGeneration.MakeOld"/> | ||
890 | public void Clear() | ||
891 | { | ||
892 | for( int i = m_buckets.Length - 1 ; i >= 0 ; i-- ) | ||
893 | { | ||
894 | m_buckets[ i ] = -1; | ||
895 | } | ||
896 | |||
897 | Array.Clear( m_elements, 0, m_elements.Length ); | ||
898 | Size = 0; | ||
899 | m_firstFreeElement = -1; | ||
900 | m_freeCount = 0; | ||
901 | m_nextUnusedElement = 0; | ||
902 | m_newGeneration = true; | ||
903 | ExpirationTime = DateTime.MaxValue; | ||
904 | } | ||
905 | |||
906 | /// <summary> | ||
907 | /// Determines whether the <see cref="IGeneration"/> contains an element with the specific key. | ||
908 | /// </summary> | ||
909 | /// <param name="bucketIndex"> | ||
910 | /// The bucket index for the <see cref="key"/> to locate in <see cref="IGeneration"/>. | ||
911 | /// </param> | ||
912 | /// <param name="key"> | ||
913 | /// The key to locate in the <see cref="IGeneration"/>. | ||
914 | /// </param> | ||
915 | /// <returns> | ||
916 | /// <see langword="true"/>if the <see cref="IGeneration"/> contains an element with the <see cref="key"/>; | ||
917 | /// otherwise <see langword="false"/>. | ||
918 | /// </returns> | ||
919 | public bool Contains( int bucketIndex, TKey key ) | ||
920 | { | ||
921 | int previousIndex; | ||
922 | if( FindElementIndex( bucketIndex, key, true, out previousIndex ) == -1 ) | ||
923 | return false; | ||
924 | |||
925 | AccessedSinceLastTimeCheck = true; | ||
926 | return true; | ||
927 | } | ||
928 | |||
929 | /// <summary> | ||
930 | /// Returns an enumerator that iterates through the elements stored <see cref="HashGeneration"/>. | ||
931 | /// </summary> | ||
932 | /// <returns> | ||
933 | /// A <see cref="IEnumerator"/> that can be used to iterate through the <see cref="HashGeneration"/>. | ||
934 | /// </returns> | ||
935 | /// <filterpriority>1</filterpriority> | ||
936 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() | ||
937 | { | ||
938 | return new Enumerator( this ); | ||
939 | } | ||
940 | |||
941 | /// <summary> | ||
942 | /// Make from generation old generation. | ||
943 | /// </summary> | ||
944 | /// <remarks> | ||
945 | /// When generation is old, <see cref="IGeneration.TryGetValue"/> hit removes element from the generation. | ||
946 | /// </remarks> | ||
947 | /// <seealso cref="IGeneration.Clear"/> | ||
948 | public void MakeOld() | ||
949 | { | ||
950 | m_newGeneration = false; | ||
951 | } | ||
952 | |||
953 | /// <summary> | ||
954 | /// Remove element associated with the key from the generation. | ||
955 | /// </summary> | ||
956 | /// <param name="bucketIndex"> | ||
957 | /// The element's bucket index. | ||
958 | /// </param> | ||
959 | /// <param name="key"> | ||
960 | /// The element's key. | ||
961 | /// </param> | ||
962 | /// <returns> | ||
963 | /// <see langword="true"/>, if remove was successful; otherwise <see langword="false"/>. | ||
964 | /// </returns> | ||
965 | public bool Remove( int bucketIndex, TKey key ) | ||
966 | { | ||
967 | int previousIndex; | ||
968 | int entryIndex = FindElementIndex( bucketIndex, key, false, out previousIndex ); | ||
969 | if( entryIndex != -1 ) | ||
970 | { | ||
971 | RemoveElement( bucketIndex, entryIndex, previousIndex ); | ||
972 | AccessedSinceLastTimeCheck = true; | ||
973 | return true; | ||
974 | } | ||
975 | |||
976 | return false; | ||
977 | } | ||
978 | |||
979 | /// <summary> | ||
980 | /// Set or add element to generation. | ||
981 | /// </summary> | ||
982 | /// <param name="bucketIndex"> | ||
983 | /// The element's bucket index. | ||
984 | /// </param> | ||
985 | /// <param name="key"> | ||
986 | /// The element's key. | ||
987 | /// </param> | ||
988 | /// <param name="value"> | ||
989 | /// The element's value. | ||
990 | /// </param> | ||
991 | /// <param name="size"> | ||
992 | /// The element's size. | ||
993 | /// </param> | ||
994 | /// <returns> | ||
995 | /// <see langword="true"/>, if setting or adding was successful; otherwise <see langword="false"/>. | ||
996 | /// </returns> | ||
997 | /// <remarks> | ||
998 | /// <para> | ||
999 | /// If element was already existing in generation and new element size fits to collection limits, | ||
1000 | /// then it's value is replaced with new one and size information is updated. If element didn't | ||
1001 | /// exists in generation before, then generation must have empty space for a new element and | ||
1002 | /// size must fit generation's limits, before element is added to generation. | ||
1003 | /// </para> | ||
1004 | /// </remarks> | ||
1005 | public bool Set( int bucketIndex, TKey key, TValue value, long size ) | ||
1006 | { | ||
1007 | Debug.Assert( m_newGeneration, "It is possible to insert new elements only to newest generation." ); | ||
1008 | Debug.Assert( size > 0, "New element size should be more than 0." ); | ||
1009 | |||
1010 | int previousIndex; | ||
1011 | int elementIndex = FindElementIndex( bucketIndex, key, true, out previousIndex ); | ||
1012 | if( elementIndex == -1 ) | ||
1013 | { | ||
1014 | // New key | ||
1015 | if( Size + size > m_cache.m_generationMaxSize || | ||
1016 | (m_nextUnusedElement == m_cache.m_generationElementCount && m_freeCount == 0) ) | ||
1017 | { | ||
1018 | // Generation is full | ||
1019 | return false; | ||
1020 | } | ||
1021 | |||
1022 | // Increase size of generation | ||
1023 | Size += size; | ||
1024 | |||
1025 | // Get first free entry and update free entry list | ||
1026 | if( m_firstFreeElement != -1 ) | ||
1027 | { | ||
1028 | // There was entry that was removed | ||
1029 | elementIndex = m_firstFreeElement; | ||
1030 | m_firstFreeElement = m_elements[ elementIndex ].Next; | ||
1031 | m_freeCount--; | ||
1032 | } | ||
1033 | else | ||
1034 | { | ||
1035 | // No entries removed so far - just take a last one | ||
1036 | elementIndex = m_nextUnusedElement; | ||
1037 | m_nextUnusedElement++; | ||
1038 | } | ||
1039 | |||
1040 | Debug.Assert( m_elements[ elementIndex ].IsFree, "Allocated element is not free." ); | ||
1041 | |||
1042 | // Move new entry to front | ||
1043 | m_elements[ elementIndex ].Next = m_buckets[ bucketIndex ]; | ||
1044 | m_buckets[ bucketIndex ] = elementIndex; | ||
1045 | |||
1046 | // Set key and update count | ||
1047 | m_elements[ elementIndex ].Key = key; | ||
1048 | } | ||
1049 | else | ||
1050 | { | ||
1051 | // Existing key | ||
1052 | if( Size - m_elements[ elementIndex ].Size + size > m_cache.m_generationMaxSize ) | ||
1053 | { | ||
1054 | // Generation is full | ||
1055 | // Remove existing element, because generation is going to be recycled to | ||
1056 | // old generation and element is stored to new generation | ||
1057 | RemoveElement( bucketIndex, elementIndex, previousIndex ); | ||
1058 | return false; | ||
1059 | } | ||
1060 | |||
1061 | // Update generation's size | ||
1062 | Size = Size - m_elements[ elementIndex ].Size + size; | ||
1063 | } | ||
1064 | |||
1065 | // Finally set value and size | ||
1066 | m_elements[ elementIndex ].Value = value; | ||
1067 | m_elements[ elementIndex ].Size = size; | ||
1068 | |||
1069 | // Success - key was inserterted to generation | ||
1070 | AccessedSinceLastTimeCheck = true; | ||
1071 | return true; | ||
1072 | } | ||
1073 | |||
1074 | /// <summary> | ||
1075 | /// Try to get element associated with key. | ||
1076 | /// </summary> | ||
1077 | /// <param name="bucketIndex"> | ||
1078 | /// The element's bucket index. | ||
1079 | /// </param> | ||
1080 | /// <param name="key"> | ||
1081 | /// The element's key. | ||
1082 | /// </param> | ||
1083 | /// <param name="value"> | ||
1084 | /// The element's value. | ||
1085 | /// </param> | ||
1086 | /// <param name="size"> | ||
1087 | /// The element's size. | ||
1088 | /// </param> | ||
1089 | /// <returns> | ||
1090 | /// <see langword="true"/>, if element was successful retrieved; otherwise <see langword="false"/>. | ||
1091 | /// </returns> | ||
1092 | /// <remarks> | ||
1093 | /// <para> | ||
1094 | /// If element is not found from generation then <paramref name="value"/> and <paramref name="size"/> | ||
1095 | /// are set to default value (default(TValue) and 0). | ||
1096 | /// </para> | ||
1097 | /// </remarks> | ||
1098 | public bool TryGetValue( int bucketIndex, TKey key, out TValue value, out long size ) | ||
1099 | { | ||
1100 | // Find entry index, | ||
1101 | int previousIndex; | ||
1102 | int elementIndex = FindElementIndex( bucketIndex, key, m_newGeneration, out previousIndex ); | ||
1103 | if( elementIndex == -1 ) | ||
1104 | { | ||
1105 | value = default(TValue); | ||
1106 | size = 0; | ||
1107 | return false; | ||
1108 | } | ||
1109 | |||
1110 | value = m_elements[ elementIndex ].Value; | ||
1111 | size = m_elements[ elementIndex ].Size; | ||
1112 | |||
1113 | if( !m_newGeneration ) | ||
1114 | { | ||
1115 | // Old generation - remove element, because it is moved to new generation | ||
1116 | RemoveElement( bucketIndex, elementIndex, previousIndex ); | ||
1117 | } | ||
1118 | |||
1119 | AccessedSinceLastTimeCheck = true; | ||
1120 | return true; | ||
1121 | } | ||
1122 | |||
1123 | /// <summary> | ||
1124 | /// Returns an enumerator that iterates through a collection. | ||
1125 | /// </summary> | ||
1126 | /// <returns> | ||
1127 | /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection. | ||
1128 | /// </returns> | ||
1129 | /// <filterpriority>2</filterpriority> | ||
1130 | IEnumerator IEnumerable.GetEnumerator() | ||
1131 | { | ||
1132 | return GetEnumerator(); | ||
1133 | } | ||
1134 | |||
1135 | #endregion | ||
1136 | } | ||
1137 | |||
1138 | #endregion | ||
1139 | |||
1140 | #region Nested type: IGeneration | ||
1141 | |||
1142 | /// <summary> | ||
1143 | /// Cache element generation interface | ||
1144 | /// </summary> | ||
1145 | /// <remarks> | ||
1146 | /// <para> | ||
1147 | /// Generation can hold limited count of elements and limited size of data. | ||
1148 | /// </para> | ||
1149 | /// <para> | ||
1150 | /// There are two kind generations: "new generation" and "old generation(s)". All new elements | ||
1151 | /// are added to "new generation". | ||
1152 | /// </para> | ||
1153 | /// </remarks> | ||
1154 | protected interface IGeneration : IEnumerable<KeyValuePair<TKey, TValue>> | ||
1155 | { | ||
1156 | /// <summary> | ||
1157 | /// Gets or sets a value indicating whether generation was accessed since last time check. | ||
1158 | /// </summary> | ||
1159 | bool AccessedSinceLastTimeCheck { get; set; } | ||
1160 | |||
1161 | /// <summary> | ||
1162 | /// Gets element count in generation. | ||
1163 | /// </summary> | ||
1164 | int Count { get; } | ||
1165 | |||
1166 | /// <summary> | ||
1167 | /// Gets or sets generation's expiration time. | ||
1168 | /// </summary> | ||
1169 | DateTime ExpirationTime { get; set; } | ||
1170 | |||
1171 | /// <summary> | ||
1172 | /// Gets size of data stored to generation. | ||
1173 | /// </summary> | ||
1174 | long Size { get; } | ||
1175 | |||
1176 | /// <summary> | ||
1177 | /// Clear all elements from the generation and make generation new again. | ||
1178 | /// </summary> | ||
1179 | /// <remarks> | ||
1180 | /// When generation is new, it is allowed to add new elements to it and | ||
1181 | /// <see cref="TryGetValue"/>doesn't remove elements from it. | ||
1182 | /// </remarks> | ||
1183 | /// <seealso cref="MakeOld"/> | ||
1184 | void Clear(); | ||
1185 | |||
1186 | /// <summary> | ||
1187 | /// Determines whether the <see cref="IGeneration"/> contains an element with the specific key. | ||
1188 | /// </summary> | ||
1189 | /// <param name="bucketIndex"> | ||
1190 | /// The bucket index for the <see cref="key"/> to locate in <see cref="IGeneration"/>. | ||
1191 | /// </param> | ||
1192 | /// <param name="key"> | ||
1193 | /// The key to locate in the <see cref="IGeneration"/>. | ||
1194 | /// </param> | ||
1195 | /// <returns> | ||
1196 | /// <see langword="true"/>if the <see cref="IGeneration"/> contains an element with the <see cref="key"/>; | ||
1197 | /// otherwise <see langword="false"/>. | ||
1198 | /// </returns> | ||
1199 | bool Contains( int bucketIndex, TKey key ); | ||
1200 | |||
1201 | /// <summary> | ||
1202 | /// Make from generation old generation. | ||
1203 | /// </summary> | ||
1204 | /// <remarks> | ||
1205 | /// When generation is old, <see cref="TryGetValue"/> hit removes element from the generation. | ||
1206 | /// </remarks> | ||
1207 | /// <seealso cref="Clear"/> | ||
1208 | void MakeOld(); | ||
1209 | |||
1210 | /// <summary> | ||
1211 | /// Remove element associated with the key from the generation. | ||
1212 | /// </summary> | ||
1213 | /// <param name="bucketIndex"> | ||
1214 | /// The element's bucket index. | ||
1215 | /// </param> | ||
1216 | /// <param name="key"> | ||
1217 | /// The element's key. | ||
1218 | /// </param> | ||
1219 | /// <returns> | ||
1220 | /// <see langword="true"/>, if remove was successful; otherwise <see langword="false"/>. | ||
1221 | /// </returns> | ||
1222 | bool Remove( int bucketIndex, TKey key ); | ||
1223 | |||
1224 | /// <summary> | ||
1225 | /// Set or add element to generation. | ||
1226 | /// </summary> | ||
1227 | /// <param name="bucketIndex"> | ||
1228 | /// The element's bucket index. | ||
1229 | /// </param> | ||
1230 | /// <param name="key"> | ||
1231 | /// The element's key. | ||
1232 | /// </param> | ||
1233 | /// <param name="value"> | ||
1234 | /// The element's value. | ||
1235 | /// </param> | ||
1236 | /// <param name="size"> | ||
1237 | /// The element's size. | ||
1238 | /// </param> | ||
1239 | /// <returns> | ||
1240 | /// <see langword="true"/>, if setting or adding was successful; otherwise <see langword="false"/>. | ||
1241 | /// </returns> | ||
1242 | /// <remarks> | ||
1243 | /// <para> | ||
1244 | /// If element was already existing in generation and new element size fits to collection limits, | ||
1245 | /// then it's value is replaced with new one and size information is updated. If element didn't | ||
1246 | /// exists in generation before, then generation must have empty space for a new element and | ||
1247 | /// size must fit generation's limits, before element is added to generation. | ||
1248 | /// </para> | ||
1249 | /// </remarks> | ||
1250 | bool Set( int bucketIndex, TKey key, TValue value, long size ); | ||
1251 | |||
1252 | /// <summary> | ||
1253 | /// Try to get element associated with key. | ||
1254 | /// </summary> | ||
1255 | /// <param name="bucketIndex"> | ||
1256 | /// The element's bucket index. | ||
1257 | /// </param> | ||
1258 | /// <param name="key"> | ||
1259 | /// The element's key. | ||
1260 | /// </param> | ||
1261 | /// <param name="value"> | ||
1262 | /// The element's value. | ||
1263 | /// </param> | ||
1264 | /// <param name="size"> | ||
1265 | /// The element's size. | ||
1266 | /// </param> | ||
1267 | /// <returns> | ||
1268 | /// <see langword="true"/>, if element was successful retrieved; otherwise <see langword="false"/>. | ||
1269 | /// </returns> | ||
1270 | /// <remarks> | ||
1271 | /// <para> | ||
1272 | /// If element is not found from generation then <paramref name="value"/> and <paramref name="size"/> | ||
1273 | /// are set to default value (default(TValue) and 0). | ||
1274 | /// </para> | ||
1275 | /// </remarks> | ||
1276 | bool TryGetValue( int bucketIndex, TKey key, out TValue value, out long size ); | ||
1277 | } | ||
1278 | |||
1279 | #endregion | ||
1280 | |||
1281 | #region ICnmCache<TKey,TValue> Members | ||
1282 | |||
1283 | /// <summary> | ||
1284 | /// Gets current count of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1285 | /// </summary> | ||
1286 | /// <remarks> | ||
1287 | /// <para> | ||
1288 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
1289 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1290 | /// </para> | ||
1291 | /// </remarks> | ||
1292 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
1293 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1294 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1295 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
1296 | public int Count | ||
1297 | { | ||
1298 | get { return m_newGeneration.Count + m_oldGeneration.Count; } | ||
1299 | } | ||
1300 | |||
1301 | /// <summary> | ||
1302 | /// Gets or sets elements expiration time. | ||
1303 | /// </summary> | ||
1304 | /// <value> | ||
1305 | /// Elements expiration time. | ||
1306 | /// </value> | ||
1307 | /// <remarks> | ||
1308 | /// <para> | ||
1309 | /// When element has been stored in <see cref="ICnmCache{TKey,TValue}"/> longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
1310 | /// and it is not accessed through <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> method or element's value is | ||
1311 | /// not replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method, then it is automatically removed from the | ||
1312 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
1313 | /// </para> | ||
1314 | /// <para> | ||
1315 | /// It is possible that <see cref="ICnmCache{TKey,TValue}"/> implementation removes element before it's expiration time, | ||
1316 | /// 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"/>. | ||
1317 | /// </para> | ||
1318 | /// <para> | ||
1319 | /// It is also possible that element stays in cache longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>. | ||
1320 | /// </para> | ||
1321 | /// <para> | ||
1322 | /// Calling <see cref="ICnmCache{TKey,TValue}.PurgeExpired"/> try to remove all elements that are expired. | ||
1323 | /// </para> | ||
1324 | /// <para> | ||
1325 | /// To disable time limit in cache, set <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> to <see cref="DateTime.MaxValue"/>. | ||
1326 | /// </para> | ||
1327 | /// </remarks> | ||
1328 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
1329 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1330 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1331 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1332 | /// <seealso cref="ICnmCache{TKey,TValue}.Count"/> | ||
1333 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
1334 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
1335 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
1336 | public TimeSpan ExpirationTime | ||
1337 | { | ||
1338 | get { return m_expirationTime; } | ||
1339 | |||
1340 | set | ||
1341 | { | ||
1342 | if( value < MinExpirationTime ) | ||
1343 | value = MinExpirationTime; | ||
1344 | |||
1345 | if( m_expirationTime == value ) | ||
1346 | return; | ||
1347 | |||
1348 | m_newGeneration.ExpirationTime = (m_newGeneration.ExpirationTime - m_expirationTime) + value; | ||
1349 | m_oldGeneration.ExpirationTime = (m_oldGeneration.ExpirationTime - m_expirationTime) + value; | ||
1350 | m_expirationTime = value; | ||
1351 | |||
1352 | PurgeExpired(); | ||
1353 | } | ||
1354 | } | ||
1355 | |||
1356 | /// <summary> | ||
1357 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting count of elements. | ||
1358 | /// </summary> | ||
1359 | /// <value> | ||
1360 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> count of elements is limited; | ||
1361 | /// otherwise, <see langword="false"/>. | ||
1362 | /// </value> | ||
1363 | /// <remarks> | ||
1364 | /// <para> | ||
1365 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
1366 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1367 | /// </para> | ||
1368 | /// </remarks> | ||
1369 | /// <seealso cref="ICnmCache{TKey,TValue}.Count"/> | ||
1370 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
1371 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1372 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
1373 | public bool IsCountLimited | ||
1374 | { | ||
1375 | get { return true; } | ||
1376 | } | ||
1377 | |||
1378 | /// <summary> | ||
1379 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting size of elements. | ||
1380 | /// </summary> | ||
1381 | /// <value> | ||
1382 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> total size of elements is limited; | ||
1383 | /// otherwise, <see langword="false"/>. | ||
1384 | /// </value> | ||
1385 | /// <remarks> | ||
1386 | /// <para> | ||
1387 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
1388 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1389 | /// </para> | ||
1390 | /// </remarks> | ||
1391 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
1392 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
1393 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
1394 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1395 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
1396 | public bool IsSizeLimited | ||
1397 | { | ||
1398 | get { return true; } | ||
1399 | } | ||
1400 | |||
1401 | /// <summary> | ||
1402 | /// Gets a value indicating whether or not access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe). | ||
1403 | /// </summary> | ||
1404 | /// <value> | ||
1405 | /// <see langword="true"/> if access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe); | ||
1406 | /// otherwise, <see langword="false"/>. | ||
1407 | /// </value> | ||
1408 | /// <remarks> | ||
1409 | /// <para> | ||
1410 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/> object, use | ||
1411 | /// <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> in <see cref="CnmSynchronizedCache{TKey,TValue}"/> class | ||
1412 | /// to retrieve synchronized wrapper for <see cref="ICnmCache{TKey,TValue}"/> object. | ||
1413 | /// </para> | ||
1414 | /// </remarks> | ||
1415 | /// <seealso cref="ICnmCache{TKey,TValue}.SyncRoot"/> | ||
1416 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
1417 | public bool IsSynchronized | ||
1418 | { | ||
1419 | get { return false; } | ||
1420 | } | ||
1421 | |||
1422 | /// <summary> | ||
1423 | /// Gets a value indicating whether elements stored to <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time. | ||
1424 | /// </summary> | ||
1425 | /// <value> | ||
1426 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> has a fixed total size of elements; | ||
1427 | /// otherwise, <see langword="false"/>. | ||
1428 | /// </value> | ||
1429 | /// <remarks> | ||
1430 | /// If <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time and element is not accessed through <see cref="ICnmCache{TKey,TValue}.Set"/> | ||
1431 | /// or <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> methods in <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> , then element is automatically removed from | ||
1432 | /// the cache. Depending on implementation of the <see cref="ICnmCache{TKey,TValue}"/>, some of the elements may | ||
1433 | /// stay longer in cache. | ||
1434 | /// </remarks> | ||
1435 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
1436 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1437 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1438 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1439 | public bool IsTimeLimited | ||
1440 | { | ||
1441 | get { return ExpirationTime != TimeSpan.MaxValue; } | ||
1442 | } | ||
1443 | |||
1444 | /// <summary> | ||
1445 | /// Gets or sets maximal allowed count of elements that can be stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1446 | /// </summary> | ||
1447 | /// <value> | ||
1448 | /// <see cref="int.MaxValue"/>, if <see cref="ICnmCache{TKey,TValue}"/> is not limited by count of elements; | ||
1449 | /// otherwise maximal allowed count of elements. | ||
1450 | /// </value> | ||
1451 | /// <remarks> | ||
1452 | /// <para> | ||
1453 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
1454 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1455 | /// </para> | ||
1456 | /// </remarks> | ||
1457 | public int MaxCount | ||
1458 | { | ||
1459 | get { return m_maxCount; } | ||
1460 | |||
1461 | set | ||
1462 | { | ||
1463 | if( value < 8 ) | ||
1464 | value = 8; | ||
1465 | if( m_maxCount == value ) | ||
1466 | return; | ||
1467 | |||
1468 | m_maxCount = value; | ||
1469 | Initialize(); | ||
1470 | } | ||
1471 | } | ||
1472 | |||
1473 | /// <summary> | ||
1474 | /// <para>Gets maximal allowed element size.</para> | ||
1475 | /// </summary> | ||
1476 | /// <value> | ||
1477 | /// Maximal allowed element size. | ||
1478 | /// </value> | ||
1479 | /// <remarks> | ||
1480 | /// <para> | ||
1481 | /// If element's size is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is | ||
1482 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1483 | /// </para> | ||
1484 | /// </remarks> | ||
1485 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1486 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1487 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
1488 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
1489 | public long MaxElementSize | ||
1490 | { | ||
1491 | get { return m_maxElementSize; } | ||
1492 | |||
1493 | private set { m_maxElementSize = value; } | ||
1494 | } | ||
1495 | |||
1496 | /// <summary> | ||
1497 | /// Gets or sets maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1498 | /// </summary> | ||
1499 | /// <value> | ||
1500 | /// Maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1501 | /// </value> | ||
1502 | /// <remarks> | ||
1503 | /// <para> | ||
1504 | /// Normally size is total bytes used by elements in the cache. But it can be any other suitable unit of measure. | ||
1505 | /// </para> | ||
1506 | /// <para> | ||
1507 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
1508 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1509 | /// </para> | ||
1510 | /// </remarks> | ||
1511 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
1512 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1513 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
1514 | public long MaxSize | ||
1515 | { | ||
1516 | get { return m_maxSize; } | ||
1517 | |||
1518 | set | ||
1519 | { | ||
1520 | if( value < 8 ) | ||
1521 | value = 8; | ||
1522 | if( m_maxSize == value ) | ||
1523 | return; | ||
1524 | |||
1525 | m_maxSize = value; | ||
1526 | Initialize(); | ||
1527 | } | ||
1528 | } | ||
1529 | |||
1530 | /// <summary> | ||
1531 | /// Gets total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1532 | /// </summary> | ||
1533 | /// <value> | ||
1534 | /// Total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
1535 | /// </value> | ||
1536 | /// <remarks> | ||
1537 | /// <para> | ||
1538 | /// Normally bytes, but can be any suitable unit of measure. | ||
1539 | /// </para> | ||
1540 | /// <para> | ||
1541 | /// Element's size is given when element is added or replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method. | ||
1542 | /// </para> | ||
1543 | /// <para> | ||
1544 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
1545 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
1546 | /// </para> | ||
1547 | /// </remarks> | ||
1548 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
1549 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1550 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
1551 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1552 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
1553 | public long Size | ||
1554 | { | ||
1555 | get { return m_newGeneration.Size + m_oldGeneration.Size; } | ||
1556 | } | ||
1557 | |||
1558 | /// <summary> | ||
1559 | /// Gets an object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1560 | /// </summary> | ||
1561 | /// <value> | ||
1562 | /// An object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1563 | /// </value> | ||
1564 | /// <remarks> | ||
1565 | /// <para> | ||
1566 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/>, use <see cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
1567 | /// method <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> to retrieve synchronized wrapper interface to | ||
1568 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
1569 | /// </para> | ||
1570 | /// </remarks> | ||
1571 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSynchronized"/> | ||
1572 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
1573 | public object SyncRoot | ||
1574 | { | ||
1575 | get { return m_syncRoot; } | ||
1576 | } | ||
1577 | |||
1578 | /// <summary> | ||
1579 | /// Removes all elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1580 | /// </summary> | ||
1581 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1582 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
1583 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
1584 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
1585 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1586 | public void Clear() | ||
1587 | { | ||
1588 | m_newGeneration.Clear(); | ||
1589 | m_oldGeneration.Clear(); | ||
1590 | m_oldGeneration.MakeOld(); | ||
1591 | m_version++; | ||
1592 | } | ||
1593 | |||
1594 | /// <summary> | ||
1595 | /// Returns an enumerator that iterates through the elements stored to <see cref="CnmMemoryCache{TKey,TValue}"/>. | ||
1596 | /// </summary> | ||
1597 | /// <returns> | ||
1598 | /// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection. | ||
1599 | /// </returns> | ||
1600 | /// <filterpriority>1</filterpriority> | ||
1601 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() | ||
1602 | { | ||
1603 | return new Enumerator( this ); | ||
1604 | } | ||
1605 | |||
1606 | /// <summary> | ||
1607 | /// Purge expired elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1608 | /// </summary> | ||
1609 | /// <remarks> | ||
1610 | /// <para> | ||
1611 | /// Element becomes expired when last access time to it has been longer time than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>. | ||
1612 | /// </para> | ||
1613 | /// <para> | ||
1614 | /// Depending on <see cref="ICnmCache{TKey,TValue}"/> implementation, some of expired elements | ||
1615 | /// may stay longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> in the cache. | ||
1616 | /// </para> | ||
1617 | /// </remarks> | ||
1618 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
1619 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
1620 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1621 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
1622 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
1623 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
1624 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
1625 | public void PurgeExpired() | ||
1626 | { | ||
1627 | m_operationsBetweenTimeChecks = DefaultOperationsBetweenTimeChecks; | ||
1628 | |||
1629 | if( !IsTimeLimited ) | ||
1630 | return; | ||
1631 | |||
1632 | DateTime now = DateTime.Now; | ||
1633 | if( m_newGeneration.AccessedSinceLastTimeCheck ) | ||
1634 | { | ||
1635 | // New generation has been accessed since last check | ||
1636 | // Update it's expiration time. | ||
1637 | m_newGeneration.ExpirationTime = now + ExpirationTime; | ||
1638 | m_newGeneration.AccessedSinceLastTimeCheck = false; | ||
1639 | } | ||
1640 | else if( m_newGeneration.ExpirationTime < now ) | ||
1641 | { | ||
1642 | // New generation has been expired. | ||
1643 | // --> also old generation must be expired. | ||
1644 | PurgeGeneration( m_newGeneration ); | ||
1645 | PurgeGeneration( m_oldGeneration ); | ||
1646 | return; | ||
1647 | } | ||
1648 | |||
1649 | if( m_oldGeneration.ExpirationTime < now ) | ||
1650 | PurgeGeneration( m_oldGeneration ); | ||
1651 | } | ||
1652 | |||
1653 | /// <summary> | ||
1654 | /// Removes element associated with <paramref name="key"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1655 | /// </summary> | ||
1656 | /// <param name="key"> | ||
1657 | /// The key that is associated with element to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1658 | /// </param> | ||
1659 | /// <exception cref="ArgumentNullException"> | ||
1660 | /// <paramref name="key"/>is <see langword="null"/>. | ||
1661 | /// </exception> | ||
1662 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1663 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
1664 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
1665 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
1666 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1667 | public void Remove( TKey key ) | ||
1668 | { | ||
1669 | if( key == null ) | ||
1670 | throw new ArgumentNullException( "key" ); | ||
1671 | |||
1672 | int bucketIndex = GetBucketIndex( key ); | ||
1673 | if( !m_newGeneration.Remove( bucketIndex, key ) ) | ||
1674 | { | ||
1675 | if( !m_oldGeneration.Remove( bucketIndex, key ) ) | ||
1676 | { | ||
1677 | CheckExpired(); | ||
1678 | return; | ||
1679 | } | ||
1680 | } | ||
1681 | |||
1682 | CheckExpired(); | ||
1683 | m_version++; | ||
1684 | } | ||
1685 | |||
1686 | /// <summary> | ||
1687 | /// Removes elements that are associated with one of <paramref name="keys"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1688 | /// </summary> | ||
1689 | /// <param name="keys"> | ||
1690 | /// The keys that are associated with elements to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1691 | /// </param> | ||
1692 | /// <exception cref="ArgumentNullException"> | ||
1693 | /// <paramref name="keys"/>is <see langword="null"/>. | ||
1694 | /// </exception> | ||
1695 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1696 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
1697 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
1698 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
1699 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1700 | public void RemoveRange( IEnumerable<TKey> keys ) | ||
1701 | { | ||
1702 | if( keys == null ) | ||
1703 | throw new ArgumentNullException( "keys" ); | ||
1704 | |||
1705 | foreach( TKey key in keys ) | ||
1706 | { | ||
1707 | if( key == null ) | ||
1708 | continue; | ||
1709 | |||
1710 | int bucketIndex = GetBucketIndex( key ); | ||
1711 | if( !m_newGeneration.Remove( bucketIndex, key ) ) | ||
1712 | m_oldGeneration.Remove( bucketIndex, key ); | ||
1713 | } | ||
1714 | |||
1715 | CheckExpired(); | ||
1716 | m_version++; | ||
1717 | } | ||
1718 | |||
1719 | /// <summary> | ||
1720 | /// Add or replace an element with the provided <paramref name="key"/>, <paramref name="value"/> and <paramref name="size"/> to | ||
1721 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
1722 | /// </summary> | ||
1723 | /// <param name="key"> | ||
1724 | /// The object used as the key of the element. Can't be <see langword="null"/> reference. | ||
1725 | /// </param> | ||
1726 | /// <param name="value"> | ||
1727 | /// The object used as the value of the element to add or replace. <see langword="null"/> is allowed. | ||
1728 | /// </param> | ||
1729 | /// <param name="size"> | ||
1730 | /// The element's size. Normally bytes, but can be any suitable unit of measure. | ||
1731 | /// </param> | ||
1732 | /// <returns> | ||
1733 | /// <see langword="true"/>if element has been added successfully to the <see cref="ICnmCache{TKey,TValue}"/>; | ||
1734 | /// otherwise <see langword="false"/>. | ||
1735 | /// </returns> | ||
1736 | /// <exception cref="ArgumentNullException"> | ||
1737 | /// <paramref name="key"/>is <see langword="null"/>. | ||
1738 | /// </exception> | ||
1739 | /// <exception cref="ArgumentOutOfRangeException"> | ||
1740 | /// The element's <paramref name="size"/> is less than 0. | ||
1741 | /// </exception> | ||
1742 | /// <remarks> | ||
1743 | /// <para> | ||
1744 | /// If element's <paramref name="size"/> is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is | ||
1745 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>, however - possible older element is | ||
1746 | /// removed from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
1747 | /// </para> | ||
1748 | /// <para> | ||
1749 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
1750 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
1751 | /// </para> | ||
1752 | /// <para> | ||
1753 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
1754 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
1755 | /// </para> | ||
1756 | /// </remarks> | ||
1757 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
1758 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
1759 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
1760 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
1761 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
1762 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
1763 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1764 | public bool Set( TKey key, TValue value, long size ) | ||
1765 | { | ||
1766 | if( key == null ) | ||
1767 | throw new ArgumentNullException( "key" ); | ||
1768 | |||
1769 | if( size < 0 ) | ||
1770 | throw new ArgumentOutOfRangeException( "size", size, "Value's size can't be less than 0." ); | ||
1771 | |||
1772 | if( size > MaxElementSize ) | ||
1773 | { | ||
1774 | // Entry size is too big to fit cache - ignore it | ||
1775 | Remove( key ); | ||
1776 | return false; | ||
1777 | } | ||
1778 | |||
1779 | if( size == 0 ) | ||
1780 | size = 1; | ||
1781 | |||
1782 | int bucketIndex = GetBucketIndex( key ); | ||
1783 | m_oldGeneration.Remove( bucketIndex, key ); | ||
1784 | AddToNewGeneration( bucketIndex, key, value, size ); | ||
1785 | CheckExpired(); | ||
1786 | |||
1787 | return true; | ||
1788 | } | ||
1789 | |||
1790 | /// <summary> | ||
1791 | /// Gets the <paramref name="value"/> associated with the specified <paramref name="key"/>. | ||
1792 | /// </summary> | ||
1793 | /// <returns> | ||
1794 | /// <see langword="true"/>if the <see cref="ICnmCache{TKey,TValue}"/> contains an element with | ||
1795 | /// the specified key; otherwise, <see langword="false"/>. | ||
1796 | /// </returns> | ||
1797 | /// <param name="key"> | ||
1798 | /// The key whose <paramref name="value"/> to get. | ||
1799 | /// </param> | ||
1800 | /// <param name="value"> | ||
1801 | /// When this method returns, the value associated with the specified <paramref name="key"/>, | ||
1802 | /// if the <paramref name="key"/> is found; otherwise, the | ||
1803 | /// default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized. | ||
1804 | /// </param> | ||
1805 | /// <exception cref="ArgumentNullException"> | ||
1806 | /// <paramref name="key"/>is <see langword="null"/>. | ||
1807 | /// </exception> | ||
1808 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
1809 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
1810 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
1811 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
1812 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
1813 | public bool TryGetValue( TKey key, out TValue value ) | ||
1814 | { | ||
1815 | if( key == null ) | ||
1816 | throw new ArgumentNullException( "key" ); | ||
1817 | |||
1818 | int bucketIndex = GetBucketIndex( key ); | ||
1819 | long size; | ||
1820 | if( m_newGeneration.TryGetValue( bucketIndex, key, out value, out size ) ) | ||
1821 | { | ||
1822 | CheckExpired(); | ||
1823 | return true; | ||
1824 | } | ||
1825 | |||
1826 | if( m_oldGeneration.TryGetValue( bucketIndex, key, out value, out size ) ) | ||
1827 | { | ||
1828 | // Move element to new generation | ||
1829 | AddToNewGeneration( bucketIndex, key, value, size ); | ||
1830 | CheckExpired(); | ||
1831 | return true; | ||
1832 | } | ||
1833 | |||
1834 | CheckExpired(); | ||
1835 | return false; | ||
1836 | } | ||
1837 | |||
1838 | /// <summary> | ||
1839 | /// Returns an enumerator that iterates through a collection. | ||
1840 | /// </summary> | ||
1841 | /// <returns> | ||
1842 | /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection. | ||
1843 | /// </returns> | ||
1844 | /// <filterpriority>2</filterpriority> | ||
1845 | IEnumerator IEnumerable.GetEnumerator() | ||
1846 | { | ||
1847 | return GetEnumerator(); | ||
1848 | } | ||
1849 | |||
1850 | #endregion | ||
1851 | } | ||
1852 | } | ||
diff --git a/OpenSim/Framework/CnmSynchronizedCache.cs b/OpenSim/Framework/CnmSynchronizedCache.cs new file mode 100644 index 0000000..418a095 --- /dev/null +++ b/OpenSim/Framework/CnmSynchronizedCache.cs | |||
@@ -0,0 +1,746 @@ | |||
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.Threading; | ||
31 | |||
32 | namespace OpenSim.Framework | ||
33 | { | ||
34 | /// <summary> | ||
35 | /// Synchronized Cenome cache wrapper. | ||
36 | /// </summary> | ||
37 | /// <typeparam name="TKey"> | ||
38 | /// The type of keys in the cache. | ||
39 | /// </typeparam> | ||
40 | /// <typeparam name="TValue"> | ||
41 | /// The type of values in the cache. | ||
42 | /// </typeparam> | ||
43 | /// <remarks> | ||
44 | /// <para> | ||
45 | /// Enumerator will block other threads, until enumerator's <see cref="IDisposable.Dispose"/> method is called. | ||
46 | /// "foreach" statement is automatically calling it. | ||
47 | /// </para> | ||
48 | /// </remarks> | ||
49 | public class CnmSynchronizedCache<TKey, TValue> : ICnmCache<TKey, TValue> | ||
50 | { | ||
51 | /// <summary> | ||
52 | /// The cache object. | ||
53 | /// </summary> | ||
54 | private readonly ICnmCache<TKey, TValue> m_cache; | ||
55 | |||
56 | /// <summary> | ||
57 | /// Synchronization root. | ||
58 | /// </summary> | ||
59 | private readonly object m_syncRoot; | ||
60 | |||
61 | /// <summary> | ||
62 | /// Initializes a new instance of the <see cref="CnmSynchronizedCache{TKey,TValue}"/> class. | ||
63 | /// Initializes a new instance of the <see cref="CnmSynchronizedCache{TKey,TValue}"/> class. | ||
64 | /// </summary> | ||
65 | /// <param name="cache"> | ||
66 | /// The cache. | ||
67 | /// </param> | ||
68 | private CnmSynchronizedCache( ICnmCache<TKey, TValue> cache ) | ||
69 | { | ||
70 | m_cache = cache; | ||
71 | m_syncRoot = m_cache.SyncRoot; | ||
72 | } | ||
73 | |||
74 | /// <summary> | ||
75 | /// Returns a <see cref="ICnmCache{TKey,TValue}"/> wrapper that is synchronized (thread safe). | ||
76 | /// </summary> | ||
77 | /// <param name="cache"> | ||
78 | /// The <see cref="ICnmCache{TKey,TValue}"/> to synchronize. | ||
79 | /// </param> | ||
80 | /// <returns> | ||
81 | /// A <see cref="ICnmCache{TKey,TValue}"/> wrapper that is synchronized (thread safe). | ||
82 | /// </returns> | ||
83 | /// <exception cref="ArgumentNullException"> | ||
84 | /// <paramref name="cache"/>is null. | ||
85 | /// </exception> | ||
86 | public static ICnmCache<TKey, TValue> Synchronized( ICnmCache<TKey, TValue> cache ) | ||
87 | { | ||
88 | if( cache == null ) | ||
89 | throw new ArgumentNullException( "cache" ); | ||
90 | return cache.IsSynchronized ? cache : new CnmSynchronizedCache<TKey, TValue>( cache ); | ||
91 | } | ||
92 | |||
93 | #region Nested type: SynchronizedEnumerator | ||
94 | |||
95 | /// <summary> | ||
96 | /// Synchronized enumerator. | ||
97 | /// </summary> | ||
98 | private class SynchronizedEnumerator : IEnumerator<KeyValuePair<TKey, TValue>> | ||
99 | { | ||
100 | /// <summary> | ||
101 | /// Enumerator that is being synchronized. | ||
102 | /// </summary> | ||
103 | private readonly IEnumerator<KeyValuePair<TKey, TValue>> m_enumerator; | ||
104 | |||
105 | /// <summary> | ||
106 | /// Synchronization root. | ||
107 | /// </summary> | ||
108 | private object m_syncRoot; | ||
109 | |||
110 | /// <summary> | ||
111 | /// Initializes a new instance of the <see cref="SynchronizedEnumerator"/> class. | ||
112 | /// </summary> | ||
113 | /// <param name="enumerator"> | ||
114 | /// The enumerator that is being synchronized. | ||
115 | /// </param> | ||
116 | /// <param name="syncRoot"> | ||
117 | /// The sync root. | ||
118 | /// </param> | ||
119 | public SynchronizedEnumerator( IEnumerator<KeyValuePair<TKey, TValue>> enumerator, object syncRoot ) | ||
120 | { | ||
121 | m_syncRoot = syncRoot; | ||
122 | m_enumerator = enumerator; | ||
123 | Monitor.Enter( m_syncRoot ); | ||
124 | } | ||
125 | |||
126 | /// <summary> | ||
127 | /// Finalizes an instance of the <see cref="SynchronizedEnumerator"/> class. | ||
128 | /// </summary> | ||
129 | ~SynchronizedEnumerator() | ||
130 | { | ||
131 | Dispose(); | ||
132 | } | ||
133 | |||
134 | #region IEnumerator<KeyValuePair<TKey,TValue>> Members | ||
135 | |||
136 | /// <summary> | ||
137 | /// Gets the element in the collection at the current position of the enumerator. | ||
138 | /// </summary> | ||
139 | /// <returns> | ||
140 | /// The element in the collection at the current position of the enumerator. | ||
141 | /// </returns> | ||
142 | /// <exception cref="InvalidOperationException"> | ||
143 | /// The enumerator has reach end of collection or <see cref="MoveNext"/> is not called. | ||
144 | /// </exception> | ||
145 | public KeyValuePair<TKey, TValue> Current | ||
146 | { | ||
147 | get { return m_enumerator.Current; } | ||
148 | } | ||
149 | |||
150 | /// <summary> | ||
151 | /// Gets the current element in the collection. | ||
152 | /// </summary> | ||
153 | /// <returns> | ||
154 | /// The current element in the collection. | ||
155 | /// </returns> | ||
156 | /// <exception cref="InvalidOperationException"> | ||
157 | /// The enumerator is positioned before the first element of the collection or after the last element. | ||
158 | /// </exception><filterpriority>2</filterpriority> | ||
159 | object IEnumerator.Current | ||
160 | { | ||
161 | get { return Current; } | ||
162 | } | ||
163 | |||
164 | /// <summary> | ||
165 | /// Releases synchronization lock. | ||
166 | /// </summary> | ||
167 | public void Dispose() | ||
168 | { | ||
169 | if( m_syncRoot != null ) | ||
170 | { | ||
171 | Monitor.Exit( m_syncRoot ); | ||
172 | m_syncRoot = null; | ||
173 | } | ||
174 | |||
175 | m_enumerator.Dispose(); | ||
176 | GC.SuppressFinalize( this ); | ||
177 | } | ||
178 | |||
179 | /// <summary> | ||
180 | /// Advances the enumerator to the next element of the collection. | ||
181 | /// </summary> | ||
182 | /// <returns> | ||
183 | /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. | ||
184 | /// </returns> | ||
185 | /// <exception cref="InvalidOperationException"> | ||
186 | /// The collection was modified after the enumerator was created. | ||
187 | /// </exception> | ||
188 | public bool MoveNext() | ||
189 | { | ||
190 | return m_enumerator.MoveNext(); | ||
191 | } | ||
192 | |||
193 | /// <summary> | ||
194 | /// Sets the enumerator to its initial position, which is before the first element in the collection. | ||
195 | /// </summary> | ||
196 | /// <exception cref="InvalidOperationException"> | ||
197 | /// The collection was modified after the enumerator was created. | ||
198 | /// </exception> | ||
199 | public void Reset() | ||
200 | { | ||
201 | m_enumerator.Reset(); | ||
202 | } | ||
203 | |||
204 | #endregion | ||
205 | } | ||
206 | |||
207 | #endregion | ||
208 | |||
209 | #region ICnmCache<TKey,TValue> Members | ||
210 | |||
211 | /// <summary> | ||
212 | /// Gets current count of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
213 | /// </summary> | ||
214 | /// <remarks> | ||
215 | /// <para> | ||
216 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
217 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
218 | /// </para> | ||
219 | /// </remarks> | ||
220 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
221 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
222 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
223 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
224 | public int Count | ||
225 | { | ||
226 | get | ||
227 | { | ||
228 | lock( m_syncRoot ) | ||
229 | { | ||
230 | return m_cache.Count; | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | |||
235 | /// <summary> | ||
236 | /// Gets or sets elements expiration time. | ||
237 | /// </summary> | ||
238 | /// <value> | ||
239 | /// Elements expiration time. | ||
240 | /// </value> | ||
241 | /// <remarks> | ||
242 | /// <para> | ||
243 | /// When element has been stored in <see cref="ICnmCache{TKey,TValue}"/> longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
244 | /// and it is not accessed through <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> method or element's value is | ||
245 | /// not replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method, then it is automatically removed from the | ||
246 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
247 | /// </para> | ||
248 | /// <para> | ||
249 | /// It is possible that <see cref="ICnmCache{TKey,TValue}"/> implementation removes element before it's expiration time, | ||
250 | /// 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"/>. | ||
251 | /// </para> | ||
252 | /// <para> | ||
253 | /// It is also possible that element stays in cache longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>. | ||
254 | /// </para> | ||
255 | /// <para> | ||
256 | /// Calling <see cref="ICnmCache{TKey,TValue}.PurgeExpired"/> try to remove all elements that are expired. | ||
257 | /// </para> | ||
258 | /// <para> | ||
259 | /// To disable time limit in cache, set <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> to <see cref="DateTime.MaxValue"/>. | ||
260 | /// </para> | ||
261 | /// </remarks> | ||
262 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
263 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
264 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
265 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
266 | /// <seealso cref="ICnmCache{TKey,TValue}.Count"/> | ||
267 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
268 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
269 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
270 | public TimeSpan ExpirationTime | ||
271 | { | ||
272 | get | ||
273 | { | ||
274 | lock( m_syncRoot ) | ||
275 | { | ||
276 | return m_cache.ExpirationTime; | ||
277 | } | ||
278 | } | ||
279 | |||
280 | set | ||
281 | { | ||
282 | lock( m_syncRoot ) | ||
283 | { | ||
284 | m_cache.ExpirationTime = value; | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | |||
289 | /// <summary> | ||
290 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting count of elements. | ||
291 | /// </summary> | ||
292 | /// <value> | ||
293 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> count of elements is limited; | ||
294 | /// otherwise, <see langword="false"/>. | ||
295 | /// </value> | ||
296 | /// <remarks> | ||
297 | /// <para> | ||
298 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
299 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
300 | /// </para> | ||
301 | /// </remarks> | ||
302 | /// <seealso cref="ICnmCache{TKey,TValue}.Count"/> | ||
303 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxCount"/> | ||
304 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
305 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
306 | public bool IsCountLimited | ||
307 | { | ||
308 | get | ||
309 | { | ||
310 | lock( m_syncRoot ) | ||
311 | { | ||
312 | return m_cache.IsCountLimited; | ||
313 | } | ||
314 | } | ||
315 | } | ||
316 | |||
317 | /// <summary> | ||
318 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting size of elements. | ||
319 | /// </summary> | ||
320 | /// <value> | ||
321 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> total size of elements is limited; | ||
322 | /// otherwise, <see langword="false"/>. | ||
323 | /// </value> | ||
324 | /// <remarks> | ||
325 | /// <para> | ||
326 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
327 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
328 | /// </para> | ||
329 | /// </remarks> | ||
330 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
331 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
332 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
333 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
334 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
335 | public bool IsSizeLimited | ||
336 | { | ||
337 | get | ||
338 | { | ||
339 | lock( m_syncRoot ) | ||
340 | { | ||
341 | return m_cache.IsSizeLimited; | ||
342 | } | ||
343 | } | ||
344 | } | ||
345 | |||
346 | /// <summary> | ||
347 | /// Gets a value indicating whether or not access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe). | ||
348 | /// </summary> | ||
349 | /// <value> | ||
350 | /// <see langword="true"/> if access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe); | ||
351 | /// otherwise, <see langword="false"/>. | ||
352 | /// </value> | ||
353 | /// <remarks> | ||
354 | /// <para> | ||
355 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/> object, use | ||
356 | /// <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> in <see cref="CnmSynchronizedCache{TKey,TValue}"/> class | ||
357 | /// to retrieve synchronized wrapper for <see cref="ICnmCache{TKey,TValue}"/> object. | ||
358 | /// </para> | ||
359 | /// </remarks> | ||
360 | /// <seealso cref="ICnmCache{TKey,TValue}.SyncRoot"/> | ||
361 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
362 | public bool IsSynchronized | ||
363 | { | ||
364 | get { return true; } | ||
365 | } | ||
366 | |||
367 | /// <summary> | ||
368 | /// Gets a value indicating whether elements stored to <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time. | ||
369 | /// </summary> | ||
370 | /// <value> | ||
371 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> has a fixed total size of elements; | ||
372 | /// otherwise, <see langword="false"/>. | ||
373 | /// </value> | ||
374 | /// <remarks> | ||
375 | /// If <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time and element is not accessed through <see cref="ICnmCache{TKey,TValue}.Set"/> | ||
376 | /// or <see cref="ICnmCache{TKey,TValue}.TryGetValue"/> methods in <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> , then element is automatically removed from | ||
377 | /// the cache. Depending on implementation of the <see cref="ICnmCache{TKey,TValue}"/>, some of the elements may | ||
378 | /// stay longer in cache. | ||
379 | /// </remarks> | ||
380 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
381 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
382 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
383 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
384 | public bool IsTimeLimited | ||
385 | { | ||
386 | get | ||
387 | { | ||
388 | lock( m_syncRoot ) | ||
389 | { | ||
390 | return m_cache.IsTimeLimited; | ||
391 | } | ||
392 | } | ||
393 | } | ||
394 | |||
395 | /// <summary> | ||
396 | /// Gets or sets maximal allowed count of elements that can be stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
397 | /// </summary> | ||
398 | /// <value> | ||
399 | /// <see cref="int.MaxValue"/>, if <see cref="ICnmCache{TKey,TValue}"/> is not limited by count of elements; | ||
400 | /// otherwise maximal allowed count of elements. | ||
401 | /// </value> | ||
402 | /// <remarks> | ||
403 | /// <para> | ||
404 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
405 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
406 | /// </para> | ||
407 | /// </remarks> | ||
408 | public int MaxCount | ||
409 | { | ||
410 | get | ||
411 | { | ||
412 | lock( m_syncRoot ) | ||
413 | { | ||
414 | return m_cache.MaxCount; | ||
415 | } | ||
416 | } | ||
417 | |||
418 | set | ||
419 | { | ||
420 | lock( m_syncRoot ) | ||
421 | { | ||
422 | m_cache.MaxCount = value; | ||
423 | } | ||
424 | } | ||
425 | } | ||
426 | |||
427 | /// <summary> | ||
428 | /// <para>Gets maximal allowed element size.</para> | ||
429 | /// </summary> | ||
430 | /// <value> | ||
431 | /// Maximal allowed element size. | ||
432 | /// </value> | ||
433 | /// <remarks> | ||
434 | /// <para> | ||
435 | /// If element's size is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is | ||
436 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
437 | /// </para> | ||
438 | /// </remarks> | ||
439 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
440 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
441 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
442 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
443 | public long MaxElementSize | ||
444 | { | ||
445 | get | ||
446 | { | ||
447 | lock( m_syncRoot ) | ||
448 | { | ||
449 | return m_cache.MaxElementSize; | ||
450 | } | ||
451 | } | ||
452 | } | ||
453 | |||
454 | /// <summary> | ||
455 | /// Gets or sets maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
456 | /// </summary> | ||
457 | /// <value> | ||
458 | /// Maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
459 | /// </value> | ||
460 | /// <remarks> | ||
461 | /// <para> | ||
462 | /// Normally size is total bytes used by elements in the cache. But it can be any other suitable unit of measure. | ||
463 | /// </para> | ||
464 | /// <para> | ||
465 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
466 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
467 | /// </para> | ||
468 | /// </remarks> | ||
469 | /// <exception cref="ArgumentOutOfRangeException">value is less than 0.</exception> | ||
470 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
471 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
472 | /// <seealso cref="ICnmCache{TKey,TValue}.Size"/> | ||
473 | public long MaxSize | ||
474 | { | ||
475 | get | ||
476 | { | ||
477 | lock( m_syncRoot ) | ||
478 | { | ||
479 | return m_cache.MaxSize; | ||
480 | } | ||
481 | } | ||
482 | |||
483 | set | ||
484 | { | ||
485 | lock( m_syncRoot ) | ||
486 | { | ||
487 | m_cache.MaxSize = value; | ||
488 | } | ||
489 | } | ||
490 | } | ||
491 | |||
492 | /// <summary> | ||
493 | /// Gets total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
494 | /// </summary> | ||
495 | /// <value> | ||
496 | /// Total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
497 | /// </value> | ||
498 | /// <remarks> | ||
499 | /// <para> | ||
500 | /// Normally bytes, but can be any suitable unit of measure. | ||
501 | /// </para> | ||
502 | /// <para> | ||
503 | /// Element's size is given when element is added or replaced by <see cref="ICnmCache{TKey,TValue}.Set"/> method. | ||
504 | /// </para> | ||
505 | /// <para> | ||
506 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
507 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
508 | /// </para> | ||
509 | /// </remarks> | ||
510 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxElementSize"/> | ||
511 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
512 | /// <seealso cref="ICnmCache{TKey,TValue}.MaxSize"/> | ||
513 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
514 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
515 | public long Size | ||
516 | { | ||
517 | get | ||
518 | { | ||
519 | lock( m_syncRoot ) | ||
520 | { | ||
521 | return m_cache.Size; | ||
522 | } | ||
523 | } | ||
524 | } | ||
525 | |||
526 | /// <summary> | ||
527 | /// Gets an object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
528 | /// </summary> | ||
529 | /// <value> | ||
530 | /// An object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
531 | /// </value> | ||
532 | /// <remarks> | ||
533 | /// <para> | ||
534 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/>, use <see cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
535 | /// method <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> to retrieve synchronized wrapper interface to | ||
536 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
537 | /// </para> | ||
538 | /// </remarks> | ||
539 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSynchronized"/> | ||
540 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
541 | public object SyncRoot | ||
542 | { | ||
543 | get { return m_syncRoot; } | ||
544 | } | ||
545 | |||
546 | /// <summary> | ||
547 | /// Removes all elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
548 | /// </summary> | ||
549 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
550 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
551 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
552 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
553 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
554 | public void Clear() | ||
555 | { | ||
556 | lock( m_syncRoot ) | ||
557 | { | ||
558 | m_cache.Clear(); | ||
559 | } | ||
560 | } | ||
561 | |||
562 | /// <summary> | ||
563 | /// Returns an enumerator that iterates through the elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
564 | /// </summary> | ||
565 | /// <returns> | ||
566 | /// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection. | ||
567 | /// </returns> | ||
568 | /// <filterpriority>1</filterpriority> | ||
569 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() | ||
570 | { | ||
571 | lock( m_syncRoot ) | ||
572 | { | ||
573 | return new SynchronizedEnumerator( m_cache.GetEnumerator(), m_syncRoot ); | ||
574 | } | ||
575 | } | ||
576 | |||
577 | /// <summary> | ||
578 | /// Purge expired elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
579 | /// </summary> | ||
580 | /// <remarks> | ||
581 | /// <para> | ||
582 | /// Element becomes expired when last access time to it has been longer time than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/>. | ||
583 | /// </para> | ||
584 | /// <para> | ||
585 | /// Depending on <see cref="ICnmCache{TKey,TValue}"/> implementation, some of expired elements | ||
586 | /// may stay longer than <see cref="ICnmCache{TKey,TValue}.ExpirationTime"/> in the cache. | ||
587 | /// </para> | ||
588 | /// </remarks> | ||
589 | /// <seealso cref="ICnmCache{TKey,TValue}.IsTimeLimited"/> | ||
590 | /// <seealso cref="ICnmCache{TKey,TValue}.ExpirationTime"/> | ||
591 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
592 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
593 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
594 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
595 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
596 | public void PurgeExpired() | ||
597 | { | ||
598 | lock( m_syncRoot ) | ||
599 | { | ||
600 | m_cache.PurgeExpired(); | ||
601 | } | ||
602 | } | ||
603 | |||
604 | /// <summary> | ||
605 | /// Removes element associated with <paramref name="key"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
606 | /// </summary> | ||
607 | /// <param name="key"> | ||
608 | /// The key that is associated with element to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
609 | /// </param> | ||
610 | /// <exception cref="ArgumentNullException"> | ||
611 | /// <paramref name="key"/>is <see langword="null"/>. | ||
612 | /// </exception> | ||
613 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
614 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
615 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
616 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
617 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
618 | public void Remove( TKey key ) | ||
619 | { | ||
620 | lock( m_syncRoot ) | ||
621 | { | ||
622 | m_cache.Remove( key ); | ||
623 | } | ||
624 | } | ||
625 | |||
626 | /// <summary> | ||
627 | /// Removes elements that are associated with one of <paramref name="keys"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
628 | /// </summary> | ||
629 | /// <param name="keys"> | ||
630 | /// The keys that are associated with elements to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
631 | /// </param> | ||
632 | /// <exception cref="ArgumentNullException"> | ||
633 | /// <paramref name="keys"/>is <see langword="null"/>. | ||
634 | /// </exception> | ||
635 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
636 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
637 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
638 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
639 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
640 | public void RemoveRange( IEnumerable<TKey> keys ) | ||
641 | { | ||
642 | lock( m_syncRoot ) | ||
643 | { | ||
644 | m_cache.RemoveRange( keys ); | ||
645 | } | ||
646 | } | ||
647 | |||
648 | /// <summary> | ||
649 | /// Add or replace an element with the provided <paramref name="key"/>, <paramref name="value"/> and <paramref name="size"/> to | ||
650 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
651 | /// </summary> | ||
652 | /// <param name="key"> | ||
653 | /// The object used as the key of the element. Can't be <see langword="null"/> reference. | ||
654 | /// </param> | ||
655 | /// <param name="value"> | ||
656 | /// The object used as the value of the element to add or replace. <see langword="null"/> is allowed. | ||
657 | /// </param> | ||
658 | /// <param name="size"> | ||
659 | /// The element's size. Normally bytes, but can be any suitable unit of measure. | ||
660 | /// </param> | ||
661 | /// <returns> | ||
662 | /// <see langword="true"/>if element has been added successfully to the <see cref="ICnmCache{TKey,TValue}"/>; | ||
663 | /// otherwise <see langword="false"/>. | ||
664 | /// </returns> | ||
665 | /// <exception cref="ArgumentNullException"> | ||
666 | /// <paramref name="key"/>is <see langword="null"/>. | ||
667 | /// </exception> | ||
668 | /// <exception cref="ArgumentOutOfRangeException"> | ||
669 | /// The element's <paramref name="size"/> is less than 0. | ||
670 | /// </exception> | ||
671 | /// <remarks> | ||
672 | /// <para> | ||
673 | /// If element's <paramref name="size"/> is larger than <see cref="ICnmCache{TKey,TValue}.MaxElementSize"/>, then element is | ||
674 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>, however - possible older element is | ||
675 | /// removed from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
676 | /// </para> | ||
677 | /// <para> | ||
678 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
679 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
680 | /// </para> | ||
681 | /// <para> | ||
682 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
683 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
684 | /// </para> | ||
685 | /// </remarks> | ||
686 | /// <seealso cref="ICnmCache{TKey,TValue}.IsSizeLimited"/> | ||
687 | /// <seealso cref="ICnmCache{TKey,TValue}.IsCountLimited"/> | ||
688 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
689 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
690 | /// <seealso cref="ICnmCache{TKey,TValue}.TryGetValue"/> | ||
691 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
692 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
693 | public bool Set( TKey key, TValue value, long size ) | ||
694 | { | ||
695 | lock( m_syncRoot ) | ||
696 | { | ||
697 | return m_cache.Set( key, value, size ); | ||
698 | } | ||
699 | } | ||
700 | |||
701 | /// <summary> | ||
702 | /// Gets the <paramref name="value"/> associated with the specified <paramref name="key"/>. | ||
703 | /// </summary> | ||
704 | /// <returns> | ||
705 | /// <see langword="true"/>if the <see cref="ICnmCache{TKey,TValue}"/> contains an element with | ||
706 | /// the specified key; otherwise, <see langword="false"/>. | ||
707 | /// </returns> | ||
708 | /// <param name="key"> | ||
709 | /// The key whose <paramref name="value"/> to get. | ||
710 | /// </param> | ||
711 | /// <param name="value"> | ||
712 | /// When this method returns, the value associated with the specified <paramref name="key"/>, | ||
713 | /// if the <paramref name="key"/> is found; otherwise, the | ||
714 | /// default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized. | ||
715 | /// </param> | ||
716 | /// <exception cref="ArgumentNullException"> | ||
717 | /// <paramref name="key"/>is <see langword="null"/>. | ||
718 | /// </exception> | ||
719 | /// <seealso cref="ICnmCache{TKey,TValue}.Set"/> | ||
720 | /// <seealso cref="ICnmCache{TKey,TValue}.Remove"/> | ||
721 | /// <seealso cref="ICnmCache{TKey,TValue}.RemoveRange"/> | ||
722 | /// <seealso cref="ICnmCache{TKey,TValue}.Clear"/> | ||
723 | /// <seealso cref="ICnmCache{TKey,TValue}.PurgeExpired"/> | ||
724 | public bool TryGetValue( TKey key, out TValue value ) | ||
725 | { | ||
726 | lock( m_syncRoot ) | ||
727 | { | ||
728 | return m_cache.TryGetValue( key, out value ); | ||
729 | } | ||
730 | } | ||
731 | |||
732 | /// <summary> | ||
733 | /// Returns an enumerator that iterates through the elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
734 | /// </summary> | ||
735 | /// <returns> | ||
736 | /// A <see cref="IEnumerator"/> that can be used to iterate through the collection. | ||
737 | /// </returns> | ||
738 | /// <filterpriority>1</filterpriority> | ||
739 | IEnumerator IEnumerable.GetEnumerator() | ||
740 | { | ||
741 | return GetEnumerator(); | ||
742 | } | ||
743 | |||
744 | #endregion | ||
745 | } | ||
746 | } | ||
diff --git a/OpenSim/Framework/ICnmCache.cs b/OpenSim/Framework/ICnmCache.cs new file mode 100644 index 0000000..cba8a7f --- /dev/null +++ b/OpenSim/Framework/ICnmCache.cs | |||
@@ -0,0 +1,441 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | |||
31 | namespace OpenSim.Framework | ||
32 | { | ||
33 | /// <summary> | ||
34 | /// Represent generic cache to store key/value pairs (elements) limited by time, size and count of elements. | ||
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 cache. | ||
41 | /// </typeparam> | ||
42 | /// <remarks> | ||
43 | /// <para> | ||
44 | /// Cache store limitations: | ||
45 | /// </para> | ||
46 | /// <list type="table"> | ||
47 | /// <listheader> | ||
48 | /// <term>Limitation</term> | ||
49 | /// <description>Description</description> | ||
50 | /// </listheader> | ||
51 | /// <item> | ||
52 | /// <term>Time</term> | ||
53 | /// <description> | ||
54 | /// Element that is not accessed through <see cref="TryGetValue"/> or <see cref="Set"/> in last <see cref="ExpirationTime"/> are | ||
55 | /// removed from the cache automatically. Depending on implementation of the cache some of elements may stay longer in cache. | ||
56 | /// <see cref="IsTimeLimited"/> returns <see langword="true"/>, if cache is limited by time. | ||
57 | /// </description> | ||
58 | /// </item> | ||
59 | /// <item> | ||
60 | /// <term>Count</term> | ||
61 | /// <description> | ||
62 | /// When adding an new element to cache that already have <see cref="MaxCount"/> of elements, cache will remove less recently | ||
63 | /// used element(s) from the cache, until element fits to cache. | ||
64 | /// <see cref="IsCountLimited"/> returns <see langword="true"/>, if cache is limiting element count. | ||
65 | /// </description> | ||
66 | /// </item> | ||
67 | /// <item> | ||
68 | /// <term>Size</term> | ||
69 | /// <description> | ||
70 | /// <description> | ||
71 | /// When adding an new element to cache that already have <see cref="MaxSize"/> of elements, cache will remove less recently | ||
72 | /// used element(s) from the cache, until element fits to cache. | ||
73 | /// <see cref="IsSizeLimited"/> returns <see langword="true"/>, if cache is limiting total size of elements. | ||
74 | /// Normally size is bytes used by element in the cache. But it can be any other suitable unit of measure. | ||
75 | /// </description> | ||
76 | /// </description> | ||
77 | /// </item> | ||
78 | /// </list> | ||
79 | /// </remarks> | ||
80 | public interface ICnmCache<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> | ||
81 | { | ||
82 | /// <summary> | ||
83 | /// Gets current count of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
84 | /// </summary> | ||
85 | /// <remarks> | ||
86 | /// <para> | ||
87 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
88 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
89 | /// </para> | ||
90 | /// </remarks> | ||
91 | /// <seealso cref="MaxCount"/> | ||
92 | /// <seealso cref="IsCountLimited"/> | ||
93 | /// <seealso cref="IsSizeLimited"/> | ||
94 | /// <seealso cref="IsTimeLimited"/> | ||
95 | int Count { get; } | ||
96 | |||
97 | /// <summary> | ||
98 | /// Gets or sets elements expiration time. | ||
99 | /// </summary> | ||
100 | /// <value> | ||
101 | /// Elements expiration time. | ||
102 | /// </value> | ||
103 | /// <remarks> | ||
104 | /// <para> | ||
105 | /// When element has been stored in <see cref="ICnmCache{TKey,TValue}"/> longer than <see cref="ExpirationTime"/> | ||
106 | /// and it is not accessed through <see cref="TryGetValue"/> method or element's value is | ||
107 | /// not replaced by <see cref="Set"/> method, then it is automatically removed from the | ||
108 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
109 | /// </para> | ||
110 | /// <para> | ||
111 | /// It is possible that <see cref="ICnmCache{TKey,TValue}"/> implementation removes element before it's expiration time, | ||
112 | /// because total size or count of elements stored to cache is larger than <see cref="MaxSize"/> or <see cref="MaxCount"/>. | ||
113 | /// </para> | ||
114 | /// <para> | ||
115 | /// It is also possible that element stays in cache longer than <see cref="ExpirationTime"/>. | ||
116 | /// </para> | ||
117 | /// <para> | ||
118 | /// Calling <see cref="PurgeExpired"/> try to remove all elements that are expired. | ||
119 | /// </para> | ||
120 | /// <para> | ||
121 | /// To disable time limit in cache, set <see cref="ExpirationTime"/> to <see cref="DateTime.MaxValue"/>. | ||
122 | /// </para> | ||
123 | /// </remarks> | ||
124 | /// <seealso cref="IsTimeLimited"/> | ||
125 | /// <seealso cref="IsCountLimited"/> | ||
126 | /// <seealso cref="IsSizeLimited"/> | ||
127 | /// <seealso cref="PurgeExpired"/> | ||
128 | /// <seealso cref="Count"/> | ||
129 | /// <seealso cref="MaxCount"/> | ||
130 | /// <seealso cref="MaxSize"/> | ||
131 | /// <seealso cref="Size"/> | ||
132 | TimeSpan ExpirationTime { get; set; } | ||
133 | |||
134 | /// <summary> | ||
135 | /// Gets a value indicating whether or not access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe). | ||
136 | /// </summary> | ||
137 | /// <value> | ||
138 | /// <see langword="true"/> if access to the <see cref="ICnmCache{TKey,TValue}"/> is synchronized (thread safe); | ||
139 | /// otherwise, <see langword="false"/>. | ||
140 | /// </value> | ||
141 | /// <remarks> | ||
142 | /// <para> | ||
143 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/> object, use | ||
144 | /// <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> in <see cref="CnmSynchronizedCache{TKey,TValue}"/> class | ||
145 | /// to retrieve synchronized wrapper for <see cref="ICnmCache{TKey,TValue}"/> object. | ||
146 | /// </para> | ||
147 | /// </remarks> | ||
148 | /// <seealso cref="SyncRoot"/> | ||
149 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
150 | bool IsSynchronized { get; } | ||
151 | |||
152 | /// <summary> | ||
153 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting count of elements. | ||
154 | /// </summary> | ||
155 | /// <value> | ||
156 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> count of elements is limited; | ||
157 | /// otherwise, <see langword="false"/>. | ||
158 | /// </value> | ||
159 | /// <remarks> | ||
160 | /// <para> | ||
161 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
162 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
163 | /// </para> | ||
164 | /// </remarks> | ||
165 | /// <seealso cref="Count"/> | ||
166 | /// <seealso cref="MaxCount"/> | ||
167 | /// <seealso cref="IsSizeLimited"/> | ||
168 | /// <seealso cref="IsTimeLimited"/> | ||
169 | bool IsCountLimited { get; } | ||
170 | |||
171 | /// <summary> | ||
172 | /// Gets a value indicating whether <see cref="ICnmCache{TKey,TValue}"/> is limiting size of elements. | ||
173 | /// </summary> | ||
174 | /// <value> | ||
175 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> total size of elements is limited; | ||
176 | /// otherwise, <see langword="false"/>. | ||
177 | /// </value> | ||
178 | /// <remarks> | ||
179 | /// <para> | ||
180 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
181 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
182 | /// </para> | ||
183 | /// </remarks> | ||
184 | /// <seealso cref="MaxElementSize"/> | ||
185 | /// <seealso cref="Size"/> | ||
186 | /// <seealso cref="MaxSize"/> | ||
187 | /// <seealso cref="IsCountLimited"/> | ||
188 | /// <seealso cref="IsTimeLimited"/> | ||
189 | bool IsSizeLimited { get; } | ||
190 | |||
191 | /// <summary> | ||
192 | /// Gets a value indicating whether elements stored to <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time. | ||
193 | /// </summary> | ||
194 | /// <value> | ||
195 | /// <see langword="true"/> if the <see cref="ICnmCache{TKey,TValue}"/> has a fixed total size of elements; | ||
196 | /// otherwise, <see langword="false"/>. | ||
197 | /// </value> | ||
198 | /// <remarks> | ||
199 | /// If <see cref="ICnmCache{TKey,TValue}"/> have limited inactivity time and element is not accessed through <see cref="Set"/> | ||
200 | /// or <see cref="TryGetValue"/> methods in <see cref="ExpirationTime"/> , then element is automatically removed from | ||
201 | /// the cache. Depending on implementation of the <see cref="ICnmCache{TKey,TValue}"/>, some of the elements may | ||
202 | /// stay longer in cache. | ||
203 | /// </remarks> | ||
204 | /// <seealso cref="ExpirationTime"/> | ||
205 | /// <seealso cref="PurgeExpired"/> | ||
206 | /// <seealso cref="IsCountLimited"/> | ||
207 | /// <seealso cref="IsSizeLimited"/> | ||
208 | bool IsTimeLimited { get; } | ||
209 | |||
210 | /// <summary> | ||
211 | /// Gets or sets maximal allowed count of elements that can be stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
212 | /// </summary> | ||
213 | /// <value> | ||
214 | /// <see cref="int.MaxValue"/>, if <see cref="ICnmCache{TKey,TValue}"/> is not limited by count of elements; | ||
215 | /// otherwise maximal allowed count of elements. | ||
216 | /// </value> | ||
217 | /// <remarks> | ||
218 | /// <para> | ||
219 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
220 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
221 | /// </para> | ||
222 | /// </remarks> | ||
223 | int MaxCount { get; set; } | ||
224 | |||
225 | /// <summary> | ||
226 | /// <para>Gets maximal allowed element size.</para> | ||
227 | /// </summary> | ||
228 | /// <value> | ||
229 | /// Maximal allowed element size. | ||
230 | /// </value> | ||
231 | /// <remarks> | ||
232 | /// <para> | ||
233 | /// If element's size is larger than <see cref="MaxElementSize"/>, then element is | ||
234 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
235 | /// </para> | ||
236 | /// </remarks> | ||
237 | /// <seealso cref="Set"/> | ||
238 | /// <seealso cref="IsSizeLimited"/> | ||
239 | /// <seealso cref="Size"/> | ||
240 | /// <seealso cref="MaxSize"/> | ||
241 | long MaxElementSize { get; } | ||
242 | |||
243 | /// <summary> | ||
244 | /// Gets or sets maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
245 | /// </summary> | ||
246 | /// <value> | ||
247 | /// Maximal allowed total size for elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
248 | /// </value> | ||
249 | /// <remarks> | ||
250 | /// <para> | ||
251 | /// Normally size is total bytes used by elements in the cache. But it can be any other suitable unit of measure. | ||
252 | /// </para> | ||
253 | /// <para> | ||
254 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
255 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
256 | /// </para> | ||
257 | /// </remarks> | ||
258 | /// <exception cref="ArgumentOutOfRangeException">value is less than 0.</exception> | ||
259 | /// <seealso cref="MaxElementSize"/> | ||
260 | /// <seealso cref="IsSizeLimited"/> | ||
261 | /// <seealso cref="Size"/> | ||
262 | long MaxSize { get; set; } | ||
263 | |||
264 | /// <summary> | ||
265 | /// Gets total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
266 | /// </summary> | ||
267 | /// <value> | ||
268 | /// Total size of elements stored to <see cref="ICnmCache{TKey,TValue}"/>. | ||
269 | /// </value> | ||
270 | /// <remarks> | ||
271 | /// <para> | ||
272 | /// Normally bytes, but can be any suitable unit of measure. | ||
273 | /// </para> | ||
274 | /// <para> | ||
275 | /// Element's size is given when element is added or replaced by <see cref="Set"/> method. | ||
276 | /// </para> | ||
277 | /// <para> | ||
278 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
279 | /// <see cref="ICnmCache{TKey,TValue}"/> will remove less recently used elements until it can fit an new element. | ||
280 | /// </para> | ||
281 | /// </remarks> | ||
282 | /// <seealso cref="MaxElementSize"/> | ||
283 | /// <seealso cref="IsSizeLimited"/> | ||
284 | /// <seealso cref="MaxSize"/> | ||
285 | /// <seealso cref="IsCountLimited"/> | ||
286 | /// <seealso cref="ExpirationTime"/> | ||
287 | long Size { get; } | ||
288 | |||
289 | /// <summary> | ||
290 | /// Gets an object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
291 | /// </summary> | ||
292 | /// <value> | ||
293 | /// An object that can be used to synchronize access to the <see cref="ICnmCache{TKey,TValue}"/>. | ||
294 | /// </value> | ||
295 | /// <remarks> | ||
296 | /// <para> | ||
297 | /// To get synchronized (thread safe) access to <see cref="ICnmCache{TKey,TValue}"/>, use <see cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
298 | /// method <see cref="CnmSynchronizedCache{TKey,TValue}.Synchronized"/> to retrieve synchronized wrapper interface to | ||
299 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
300 | /// </para> | ||
301 | /// </remarks> | ||
302 | /// <seealso cref="IsSynchronized"/> | ||
303 | /// <seealso cref="CnmSynchronizedCache{TKey,TValue}"/> | ||
304 | object SyncRoot { get; } | ||
305 | |||
306 | /// <summary> | ||
307 | /// Removes all elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
308 | /// </summary> | ||
309 | /// <seealso cref="Set"/> | ||
310 | /// <seealso cref="Remove"/> | ||
311 | /// <seealso cref="RemoveRange"/> | ||
312 | /// <seealso cref="TryGetValue"/> | ||
313 | /// <seealso cref="PurgeExpired"/> | ||
314 | void Clear(); | ||
315 | |||
316 | /// <summary> | ||
317 | /// Purge expired elements from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
318 | /// </summary> | ||
319 | /// <remarks> | ||
320 | /// <para> | ||
321 | /// Element becomes expired when last access time to it has been longer time than <see cref="ExpirationTime"/>. | ||
322 | /// </para> | ||
323 | /// <para> | ||
324 | /// Depending on <see cref="ICnmCache{TKey,TValue}"/> implementation, some of expired elements | ||
325 | /// may stay longer than <see cref="ExpirationTime"/> in the cache. | ||
326 | /// </para> | ||
327 | /// </remarks> | ||
328 | /// <seealso cref="IsTimeLimited"/> | ||
329 | /// <seealso cref="ExpirationTime"/> | ||
330 | /// <seealso cref="Set"/> | ||
331 | /// <seealso cref="Remove"/> | ||
332 | /// <seealso cref="RemoveRange"/> | ||
333 | /// <seealso cref="TryGetValue"/> | ||
334 | /// <seealso cref="Clear"/> | ||
335 | void PurgeExpired(); | ||
336 | |||
337 | /// <summary> | ||
338 | /// Removes element associated with <paramref name="key"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
339 | /// </summary> | ||
340 | /// <param name="key"> | ||
341 | /// The key that is associated with element to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
342 | /// </param> | ||
343 | /// <exception cref="ArgumentNullException"> | ||
344 | /// <paramref name="key"/> is <see langword="null"/>. | ||
345 | /// </exception> | ||
346 | /// <seealso cref="Set"/> | ||
347 | /// <seealso cref="RemoveRange"/> | ||
348 | /// <seealso cref="TryGetValue"/> | ||
349 | /// <seealso cref="Clear"/> | ||
350 | /// <seealso cref="PurgeExpired"/> | ||
351 | void Remove( TKey key ); | ||
352 | |||
353 | /// <summary> | ||
354 | /// Removes elements that are associated with one of <paramref name="keys"/> from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
355 | /// </summary> | ||
356 | /// <param name="keys"> | ||
357 | /// The keys that are associated with elements to remove from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
358 | /// </param> | ||
359 | /// <exception cref="ArgumentNullException"> | ||
360 | /// <paramref name="keys"/> is <see langword="null"/>. | ||
361 | /// </exception> | ||
362 | /// <seealso cref="Set"/> | ||
363 | /// <seealso cref="Remove"/> | ||
364 | /// <seealso cref="TryGetValue"/> | ||
365 | /// <seealso cref="Clear"/> | ||
366 | /// <seealso cref="PurgeExpired"/> | ||
367 | void RemoveRange( IEnumerable<TKey> keys ); | ||
368 | |||
369 | /// <summary> | ||
370 | /// Add or replace an element with the provided <paramref name="key"/>, <paramref name="value"/> and <paramref name="size"/> to | ||
371 | /// <see cref="ICnmCache{TKey,TValue}"/>. | ||
372 | /// </summary> | ||
373 | /// <param name="key"> | ||
374 | /// The object used as the key of the element. Can't be <see langword="null"/> reference. | ||
375 | /// </param> | ||
376 | /// <param name="value"> | ||
377 | /// The object used as the value of the element to add or replace. <see langword="null"/> is allowed. | ||
378 | /// </param> | ||
379 | /// <param name="size"> | ||
380 | /// The element's size. Normally bytes, but can be any suitable unit of measure. | ||
381 | /// </param> | ||
382 | /// <returns> | ||
383 | /// <see langword="true"/> if element has been added successfully to the <see cref="ICnmCache{TKey,TValue}"/>; | ||
384 | /// otherwise <see langword="false"/>. | ||
385 | /// </returns> | ||
386 | /// <exception cref="ArgumentNullException"> | ||
387 | /// <paramref name="key"/>is <see langword="null"/>. | ||
388 | /// </exception> | ||
389 | /// <exception cref="ArgumentOutOfRangeException"> | ||
390 | /// The element's <paramref name="size"/> is less than 0. | ||
391 | /// </exception> | ||
392 | /// <remarks> | ||
393 | /// <para> | ||
394 | /// If element's <paramref name="size"/> is larger than <see cref="MaxElementSize"/>, then element is | ||
395 | /// not added to the <see cref="ICnmCache{TKey,TValue}"/>, however - possible older element is | ||
396 | /// removed from the <see cref="ICnmCache{TKey,TValue}"/>. | ||
397 | /// </para> | ||
398 | /// <para> | ||
399 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting total size of elements, | ||
400 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
401 | /// </para> | ||
402 | /// <para> | ||
403 | /// When adding an new element to <see cref="ICnmCache{TKey,TValue}"/> that is limiting element count, | ||
404 | /// <see cref="ICnmCache{TKey,TValue}"/>will remove less recently used elements until it can fit an new element. | ||
405 | /// </para> | ||
406 | /// </remarks> | ||
407 | /// <seealso cref="IsSizeLimited"/> | ||
408 | /// <seealso cref="IsCountLimited"/> | ||
409 | /// <seealso cref="Remove"/> | ||
410 | /// <seealso cref="RemoveRange"/> | ||
411 | /// <seealso cref="TryGetValue"/> | ||
412 | /// <seealso cref="Clear"/> | ||
413 | /// <seealso cref="PurgeExpired"/> | ||
414 | bool Set( TKey key, TValue value, long size ); | ||
415 | |||
416 | /// <summary> | ||
417 | /// Gets the <paramref name="value"/> associated with the specified <paramref name="key"/>. | ||
418 | /// </summary> | ||
419 | /// <returns> | ||
420 | /// <see langword="true"/>if the <see cref="ICnmCache{TKey,TValue}"/> contains an element with | ||
421 | /// the specified key; otherwise, <see langword="false"/>. | ||
422 | /// </returns> | ||
423 | /// <param name="key"> | ||
424 | /// The key whose <paramref name="value"/> to get. | ||
425 | /// </param> | ||
426 | /// <param name="value"> | ||
427 | /// When this method returns, the value associated with the specified <paramref name="key"/>, | ||
428 | /// if the <paramref name="key"/> is found; otherwise, the | ||
429 | /// default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized. | ||
430 | /// </param> | ||
431 | /// <exception cref="ArgumentNullException"> | ||
432 | /// <paramref name="key"/>is <see langword="null"/>. | ||
433 | /// </exception> | ||
434 | /// <seealso cref="Set"/> | ||
435 | /// <seealso cref="Remove"/> | ||
436 | /// <seealso cref="RemoveRange"/> | ||
437 | /// <seealso cref="Clear"/> | ||
438 | /// <seealso cref="PurgeExpired"/> | ||
439 | bool TryGetValue( TKey key, out TValue value ); | ||
440 | } | ||
441 | } | ||
diff --git a/OpenSim/Framework/PrimeNumberHelper.cs b/OpenSim/Framework/PrimeNumberHelper.cs new file mode 100644 index 0000000..e4bf615 --- /dev/null +++ b/OpenSim/Framework/PrimeNumberHelper.cs | |||
@@ -0,0 +1,98 @@ | |||
1 | // -------------------------------------------------------------------------------------------------------------------- | ||
2 | // <copyright company="" file="PrimeNumberHelper.cs"> | ||
3 | // | ||
4 | // </copyright> | ||
5 | // <summary> | ||
6 | // | ||
7 | // </summary> | ||
8 | // | ||
9 | // -------------------------------------------------------------------------------------------------------------------- | ||
10 | |||
11 | using System; | ||
12 | |||
13 | namespace OpenSim.Framework | ||
14 | { | ||
15 | /// <summary> | ||
16 | /// Utility class that is used to find small prime numbers and test is number prime number. | ||
17 | /// </summary> | ||
18 | public static class PrimeNumberHelper | ||
19 | { | ||
20 | /// <summary> | ||
21 | /// Precalculated prime numbers. | ||
22 | /// </summary> | ||
23 | private static readonly int[] Primes = new int[] | ||
24 | { | ||
25 | 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, | ||
26 | 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333, | ||
27 | 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, | ||
28 | 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, | ||
29 | 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449, | ||
30 | 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, | ||
31 | 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, | ||
32 | 5999471, 7199369 | ||
33 | }; | ||
34 | |||
35 | /// <summary> | ||
36 | /// Get prime number that is equal or larger than <see cref="min"/>. | ||
37 | /// </summary> | ||
38 | /// <param name="min"> | ||
39 | /// Minimal returned prime number. | ||
40 | /// </param> | ||
41 | /// <returns> | ||
42 | /// Primer number that is equal or larger than <see cref="min"/>. If <see cref="min"/> is too large, return -1. | ||
43 | /// </returns> | ||
44 | public static int GetPrime( int min ) | ||
45 | { | ||
46 | if( min <= 2 ) | ||
47 | return 2; | ||
48 | |||
49 | if( Primes[ Primes.Length - 1 ] < min ) | ||
50 | { | ||
51 | for( int i = min | 1 ; i < 0x7FFFFFFF ; i += 2 ) | ||
52 | { | ||
53 | if( IsPrime( i ) ) | ||
54 | return i; | ||
55 | } | ||
56 | |||
57 | return -1; | ||
58 | } | ||
59 | |||
60 | for( int i = Primes.Length - 2 ; i >= 0 ; i-- ) | ||
61 | { | ||
62 | if( min == Primes[ i ] ) | ||
63 | return min; | ||
64 | |||
65 | if( min > Primes[ i ] ) | ||
66 | return Primes[ i + 1 ]; | ||
67 | } | ||
68 | |||
69 | return 2; | ||
70 | } | ||
71 | |||
72 | /// <summary> | ||
73 | /// Just basic Sieve of Eratosthenes prime number test. | ||
74 | /// </summary> | ||
75 | /// <param name="candinate"> | ||
76 | /// Number that is tested. | ||
77 | /// </param> | ||
78 | /// <returns> | ||
79 | /// true, if <see cref="candinate"/> is prime number; otherwise false. | ||
80 | /// </returns> | ||
81 | public static bool IsPrime( int candinate ) | ||
82 | { | ||
83 | if( (candinate & 1) == 0 ) | ||
84 | |||
85 | // Even number - only prime if 2 | ||
86 | return candinate == 2; | ||
87 | |||
88 | int upperBound = (int) Math.Sqrt( candinate ); | ||
89 | for( int i = 3 ; i < upperBound ; i += 2 ) | ||
90 | { | ||
91 | if( candinate % i == 0 ) | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | return true; | ||
96 | } | ||
97 | } | ||
98 | } | ||
diff --git a/OpenSim/Region/CoreModules/Asset/CenomeAssetCache.cs b/OpenSim/Region/CoreModules/Asset/CenomeAssetCache.cs new file mode 100644 index 0000000..00a8143 --- /dev/null +++ b/OpenSim/Region/CoreModules/Asset/CenomeAssetCache.cs | |||
@@ -0,0 +1,382 @@ | |||
1 | // -------------------------------------------------------------------------------------------------------------------- | ||
2 | // <copyright company="" file="CenomeAssetCache.cs"> | ||
3 | // | ||
4 | // </copyright> | ||
5 | // <summary> | ||
6 | // | ||
7 | // </summary> | ||
8 | // | ||
9 | // -------------------------------------------------------------------------------------------------------------------- | ||
10 | using System; | ||
11 | using System.Reflection; | ||
12 | using log4net; | ||
13 | using Nini.Config; | ||
14 | using OpenSim.Framework; | ||
15 | using OpenSim.Region.Framework.Interfaces; | ||
16 | using OpenSim.Region.Framework.Scenes; | ||
17 | |||
18 | namespace OpenSim.Region.CoreModules.Asset | ||
19 | { | ||
20 | /// <summary> | ||
21 | /// Cenome memory asset cache. | ||
22 | /// </summary> | ||
23 | /// <remarks> | ||
24 | /// <para> | ||
25 | /// Cache is enabled by setting "AssetCaching" configuration to value "CenomeMemoryAssetCache". | ||
26 | /// When cache is successfully enable log should have message | ||
27 | /// "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = XXX bytes, MaxCount = XXX, ExpirationTime = XXX)". | ||
28 | /// </para> | ||
29 | /// <para> | ||
30 | /// Cache's size is limited by two parameters: | ||
31 | /// maximal allowed size in bytes and maximal allowed asset count. When new asset | ||
32 | /// is added to cache that have achieved either size or count limitation, cache | ||
33 | /// will automatically remove less recently used assets from cache. Additionally | ||
34 | /// asset's lifetime is controlled by expiration time. | ||
35 | /// </para> | ||
36 | /// <para> | ||
37 | /// <list type="table"> | ||
38 | /// <listheader> | ||
39 | /// <term>Configuration</term> | ||
40 | /// <description>Description</description> | ||
41 | /// </listheader> | ||
42 | /// <item> | ||
43 | /// <term>MaxSize</term> | ||
44 | /// <description>Maximal size of the cache in bytes. Default value: 128MB (134 217 728 bytes).</description> | ||
45 | /// </item> | ||
46 | /// <item> | ||
47 | /// <term>MaxCount</term> | ||
48 | /// <description>Maximal count of assets stored to cache. Default value: 4096 assets.</description> | ||
49 | /// </item> | ||
50 | /// <item> | ||
51 | /// <term>ExpirationTime</term> | ||
52 | /// <description>Asset's expiration time in minutes. Default value: 30 minutes.</description> | ||
53 | /// </item> | ||
54 | /// </list> | ||
55 | /// </para> | ||
56 | /// </remarks> | ||
57 | /// <example> | ||
58 | /// Enabling Cenome Asset Cache: | ||
59 | /// <code> | ||
60 | /// [Modules] | ||
61 | /// AssetCaching = "CenomeMemoryAssetCache" | ||
62 | /// </code> | ||
63 | /// Setting size and expiration time limitations: | ||
64 | /// <code> | ||
65 | /// [AssetService] | ||
66 | /// ; 256 MB (default: 134217728) | ||
67 | /// MaxSize = 268435456 | ||
68 | /// ; How many assets it is possible to store cache (default: 4096) | ||
69 | /// MaxCount = 16384 | ||
70 | /// ; Expiration time - 1 hour (default: 30 minutes) | ||
71 | /// ExpirationTime = 60 | ||
72 | /// </code> | ||
73 | /// </example> | ||
74 | public class CenomeMemoryAssetCache : IImprovedAssetCache, ISharedRegionModule | ||
75 | { | ||
76 | /// <summary> | ||
77 | /// Cache's default maximal asset count. | ||
78 | /// </summary> | ||
79 | /// <remarks> | ||
80 | /// <para> | ||
81 | /// Assuming that average asset size is about 32768 bytes. | ||
82 | /// </para> | ||
83 | /// </remarks> | ||
84 | public const int DefaultMaxCount = 4096; | ||
85 | |||
86 | /// <summary> | ||
87 | /// Default maximal size of the cache in bytes | ||
88 | /// </summary> | ||
89 | /// <remarks> | ||
90 | /// <para> | ||
91 | /// 128MB = 128 * 1024^2 = 134 217 728 bytes. | ||
92 | /// </para> | ||
93 | /// </remarks> | ||
94 | public const long DefaultMaxSize = 134217728; | ||
95 | |||
96 | /// <summary> | ||
97 | /// Asset's default expiration time in the cache. | ||
98 | /// </summary> | ||
99 | public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes( 30.0 ); | ||
100 | |||
101 | /// <summary> | ||
102 | /// Log manager instance. | ||
103 | /// </summary> | ||
104 | private static readonly ILog Log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType ); | ||
105 | |||
106 | /// <summary> | ||
107 | /// Cache object. | ||
108 | /// </summary> | ||
109 | private ICnmCache<string, AssetBase> m_cache; | ||
110 | |||
111 | /// <summary> | ||
112 | /// Count of cache commands | ||
113 | /// </summary> | ||
114 | private int m_cachedCount; | ||
115 | |||
116 | /// <summary> | ||
117 | /// How many gets before dumping statistics | ||
118 | /// </summary> | ||
119 | /// <remarks> | ||
120 | /// If 0 or less, then disabled. | ||
121 | /// </remarks> | ||
122 | private int m_debugEpoch; | ||
123 | |||
124 | /// <summary> | ||
125 | /// Is Cenome asset cache enabled. | ||
126 | /// </summary> | ||
127 | private bool m_enabled; | ||
128 | |||
129 | /// <summary> | ||
130 | /// Count of get requests | ||
131 | /// </summary> | ||
132 | private int m_getCount; | ||
133 | |||
134 | /// <summary> | ||
135 | /// How many hits | ||
136 | /// </summary> | ||
137 | private int m_hitCount; | ||
138 | |||
139 | /// <summary> | ||
140 | /// Initialize asset cache module with default parameters. | ||
141 | /// </summary> | ||
142 | public void Initialize() | ||
143 | { | ||
144 | Initialize( DefaultMaxSize, DefaultMaxCount, DefaultExpirationTime ); | ||
145 | } | ||
146 | |||
147 | /// <summary> | ||
148 | /// Initialize asset cache module, with custom parameters. | ||
149 | /// </summary> | ||
150 | /// <param name="maximalSize"> | ||
151 | /// Cache's maximal size in bytes. | ||
152 | /// </param> | ||
153 | /// <param name="maximalCount"> | ||
154 | /// Cache's maximal count of assets. | ||
155 | /// </param> | ||
156 | /// <param name="expirationTime"> | ||
157 | /// Asset's expiration time. | ||
158 | /// </param> | ||
159 | public void Initialize( long maximalSize, int maximalCount, TimeSpan expirationTime ) | ||
160 | { | ||
161 | if( maximalSize <= 0 || maximalCount <= 0 ) | ||
162 | { | ||
163 | Log.Info( "[ASSET CACHE]: Cenome asset cache is not enabled." ); | ||
164 | m_enabled = false; | ||
165 | return; | ||
166 | } | ||
167 | |||
168 | if( expirationTime <= TimeSpan.Zero ) | ||
169 | { | ||
170 | // Disable expiration time | ||
171 | expirationTime = TimeSpan.MaxValue; | ||
172 | } | ||
173 | |||
174 | // Create cache and add synchronization wrapper over it | ||
175 | m_cache = | ||
176 | CnmSynchronizedCache<string, AssetBase>.Synchronized( new CnmMemoryCache<string, AssetBase>( | ||
177 | maximalSize, maximalCount, expirationTime ) ); | ||
178 | m_enabled = true; | ||
179 | Log.InfoFormat( | ||
180 | "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = {0} bytes, MaxCount = {1}, ExpirationTime = {2})", | ||
181 | maximalSize, | ||
182 | maximalCount, | ||
183 | expirationTime ); | ||
184 | } | ||
185 | |||
186 | #region IImprovedAssetCache Members | ||
187 | |||
188 | /// <summary> | ||
189 | /// Cache asset. | ||
190 | /// </summary> | ||
191 | /// <param name="asset"> | ||
192 | /// The asset that is being cached. | ||
193 | /// </param> | ||
194 | public void Cache( AssetBase asset ) | ||
195 | { | ||
196 | long size = asset.Data != null ? asset.Data.Length : 1; | ||
197 | m_cache.Set( asset.ID, asset, size ); | ||
198 | m_cachedCount++; | ||
199 | } | ||
200 | |||
201 | /// <summary> | ||
202 | /// Clear asset cache. | ||
203 | /// </summary> | ||
204 | public void Clear() | ||
205 | { | ||
206 | m_cache.Clear(); | ||
207 | } | ||
208 | |||
209 | /// <summary> | ||
210 | /// Expire (remove) asset stored to cache. | ||
211 | /// </summary> | ||
212 | /// <param name="id"> | ||
213 | /// The expired asset's id. | ||
214 | /// </param> | ||
215 | public void Expire( string id ) | ||
216 | { | ||
217 | m_cache.Remove( id ); | ||
218 | } | ||
219 | |||
220 | /// <summary> | ||
221 | /// Get asset stored | ||
222 | /// </summary> | ||
223 | /// <param name="id"> | ||
224 | /// The asset's id. | ||
225 | /// </param> | ||
226 | /// <returns> | ||
227 | /// Asset if it is found from cache; otherwise <see langword="null"/>. | ||
228 | /// </returns> | ||
229 | /// <remarks> | ||
230 | /// <para> | ||
231 | /// Caller should always check that is return value <see langword="null"/>. | ||
232 | /// Cache doesn't guarantee in any situation that asset is stored to it. | ||
233 | /// </para> | ||
234 | /// </remarks> | ||
235 | public AssetBase Get( string id ) | ||
236 | { | ||
237 | m_getCount++; | ||
238 | AssetBase assetBase; | ||
239 | if( m_cache.TryGetValue( id, out assetBase ) ) | ||
240 | m_hitCount++; | ||
241 | |||
242 | if( m_getCount == m_debugEpoch ) | ||
243 | { | ||
244 | Log.InfoFormat( | ||
245 | "[ASSET CACHE]: Cached = {0}, Get = {1}, Hits = {2}%, Size = {3} bytes, Avg. A. Size = {4} bytes", | ||
246 | m_cachedCount, | ||
247 | m_getCount, | ||
248 | ((double) m_hitCount / m_getCount) * 100.0, | ||
249 | m_cache.Size, | ||
250 | m_cache.Size / m_cache.Count ); | ||
251 | m_getCount = 0; | ||
252 | m_hitCount = 0; | ||
253 | m_cachedCount = 0; | ||
254 | } | ||
255 | |||
256 | return assetBase; | ||
257 | } | ||
258 | |||
259 | #endregion | ||
260 | |||
261 | #region ISharedRegionModule Members | ||
262 | |||
263 | /// <summary> | ||
264 | /// Gets region module's name. | ||
265 | /// </summary> | ||
266 | public string Name | ||
267 | { | ||
268 | get { return "CenomeMemoryAssetCache"; } | ||
269 | } | ||
270 | |||
271 | /// <summary> | ||
272 | /// New region is being added to server. | ||
273 | /// </summary> | ||
274 | /// <param name="scene"> | ||
275 | /// Region's scene. | ||
276 | /// </param> | ||
277 | public void AddRegion( Scene scene ) | ||
278 | { | ||
279 | if( m_enabled ) | ||
280 | scene.RegisterModuleInterface<IImprovedAssetCache>( this ); | ||
281 | } | ||
282 | |||
283 | /// <summary> | ||
284 | /// Close region module. | ||
285 | /// </summary> | ||
286 | public void Close() | ||
287 | { | ||
288 | m_enabled = false; | ||
289 | m_cache.Clear(); | ||
290 | m_cache = null; | ||
291 | } | ||
292 | |||
293 | /// <summary> | ||
294 | /// Initialize region module. | ||
295 | /// </summary> | ||
296 | /// <param name="source"> | ||
297 | /// Configuration source. | ||
298 | /// </param> | ||
299 | public void Initialise( IConfigSource source ) | ||
300 | { | ||
301 | m_cache = null; | ||
302 | m_enabled = false; | ||
303 | |||
304 | IConfig moduleConfig = source.Configs[ "Modules" ]; | ||
305 | if( moduleConfig == null ) | ||
306 | return; | ||
307 | |||
308 | string name = moduleConfig.GetString( "AssetCaching" ); | ||
309 | Log.DebugFormat( "[XXX] name = {0} (this module's name: {1}", name, Name ); | ||
310 | |||
311 | if( name != Name ) | ||
312 | return; | ||
313 | |||
314 | // This module is used | ||
315 | long maxSize = DefaultMaxSize; | ||
316 | int maxCount = DefaultMaxCount; | ||
317 | TimeSpan expirationTime = DefaultExpirationTime; | ||
318 | |||
319 | IConfig assetConfig = source.Configs[ "AssetCache" ]; | ||
320 | if( assetConfig != null ) | ||
321 | { | ||
322 | // Get optional configurations | ||
323 | maxSize = assetConfig.GetLong( "MaxSize", DefaultMaxSize ); | ||
324 | maxCount = assetConfig.GetInt( "MaxCount", DefaultMaxCount ); | ||
325 | expirationTime = | ||
326 | TimeSpan.FromMinutes( assetConfig.GetInt( "ExpirationTime", (int) DefaultExpirationTime.TotalMinutes ) ); | ||
327 | |||
328 | // Debugging purposes only | ||
329 | m_debugEpoch = assetConfig.GetInt( "DebugEpoch", 0 ); | ||
330 | } | ||
331 | |||
332 | Initialize( maxSize, maxCount, expirationTime ); | ||
333 | } | ||
334 | |||
335 | /// <summary> | ||
336 | /// Initialization post handling. | ||
337 | /// </summary> | ||
338 | /// <remarks> | ||
339 | /// <para> | ||
340 | /// Modules can use this to initialize connection with other modules. | ||
341 | /// </para> | ||
342 | /// </remarks> | ||
343 | public void PostInitialise() | ||
344 | { | ||
345 | } | ||
346 | |||
347 | /// <summary> | ||
348 | /// Region has been loaded. | ||
349 | /// </summary> | ||
350 | /// <param name="scene"> | ||
351 | /// Region's scene. | ||
352 | /// </param> | ||
353 | /// <remarks> | ||
354 | /// <para> | ||
355 | /// This is needed for all module types. Modules will register | ||
356 | /// Interfaces with scene in AddScene, and will also need a means | ||
357 | /// to access interfaces registered by other modules. Without | ||
358 | /// this extra method, a module attempting to use another modules' | ||
359 | /// interface would be successful only depending on load order, | ||
360 | /// which can't be depended upon, or modules would need to resort | ||
361 | /// to ugly kludges to attempt to request interfaces when needed | ||
362 | /// and unnecessary caching logic repeated in all modules. | ||
363 | /// The extra function stub is just that much cleaner. | ||
364 | /// </para> | ||
365 | /// </remarks> | ||
366 | public void RegionLoaded( Scene scene ) | ||
367 | { | ||
368 | } | ||
369 | |||
370 | /// <summary> | ||
371 | /// Region is being removed. | ||
372 | /// </summary> | ||
373 | /// <param name="scene"> | ||
374 | /// Region scene that is being removed. | ||
375 | /// </param> | ||
376 | public void RemoveRegion( Scene scene ) | ||
377 | { | ||
378 | } | ||
379 | |||
380 | #endregion | ||
381 | } | ||
382 | } | ||
diff --git a/OpenSim/Region/CoreModules/Resources/CoreModulePlugin.addin.xml b/OpenSim/Region/CoreModules/Resources/CoreModulePlugin.addin.xml index 393f340..9969ebe 100644 --- a/OpenSim/Region/CoreModules/Resources/CoreModulePlugin.addin.xml +++ b/OpenSim/Region/CoreModules/Resources/CoreModulePlugin.addin.xml | |||
@@ -19,6 +19,7 @@ | |||
19 | <RegionModule id="HGAssetBroker" type="OpenSim.Region.CoreModules.ServiceConnectors.Asset.HGAssetBroker" /> | 19 | <RegionModule id="HGAssetBroker" type="OpenSim.Region.CoreModules.ServiceConnectors.Asset.HGAssetBroker" /> |
20 | <RegionModule id="CoreAssetCache" type="OpenSim.Region.CoreModules.Asset.CoreAssetCache" /> | 20 | <RegionModule id="CoreAssetCache" type="OpenSim.Region.CoreModules.Asset.CoreAssetCache" /> |
21 | <RegionModule id="CoreAssetCache" type="OpenSim.Region.CoreModules.Asset.GlynnTuckerAssetCache" /> | 21 | <RegionModule id="CoreAssetCache" type="OpenSim.Region.CoreModules.Asset.GlynnTuckerAssetCache" /> |
22 | <RegionModule id="CenomeMemoryAssetCache" type="OpenSim.Region.CoreModules.Asset.CenomeMemoryAssetCache"/> | ||
22 | <RegionModule id="UrlModule" type="OpenSim.Region.CoreModules.Scripting.LSLHttp.UrlModule" /> | 23 | <RegionModule id="UrlModule" type="OpenSim.Region.CoreModules.Scripting.LSLHttp.UrlModule" /> |
23 | <RegionModule id="Chat" type="OpenSim.Region.CoreModules.Avatar.Chat.ChatModule" /> | 24 | <RegionModule id="Chat" type="OpenSim.Region.CoreModules.Avatar.Chat.ChatModule" /> |
24 | </Extension> | 25 | </Extension> |
diff --git a/bin/config-include/GridCommon.ini.example b/bin/config-include/GridCommon.ini.example index 8a95776..3825374 100644 --- a/bin/config-include/GridCommon.ini.example +++ b/bin/config-include/GridCommon.ini.example | |||
@@ -1,6 +1,7 @@ | |||
1 | [Modules] | 1 | [Modules] |
2 | ;AssetCaching = "CoreAssetCache" | 2 | ;AssetCaching = "CoreAssetCache" |
3 | AssetCaching = "GlynnTuckerAssetCache" | 3 | AssetCaching = "GlynnTuckerAssetCache" |
4 | ;AssetCaching = "CenomeMemoryAssetCache" | ||
4 | 5 | ||
5 | [AssetCache] | 6 | [AssetCache] |
6 | ; Number of buckets for assets | 7 | ; Number of buckets for assets |
@@ -16,3 +17,10 @@ | |||
16 | ; | 17 | ; |
17 | AssetServerURI = "http://myassetserver.com:8003" | 18 | AssetServerURI = "http://myassetserver.com:8003" |
18 | 19 | ||
20 | ; Optional configurations for CenomeMemoryAssetCache | ||
21 | ; Cache size 128 MB (default: 134217728) | ||
22 | ; MaxSize = 134217728 | ||
23 | ; Maximal asset count | ||
24 | ; MaxCount = 4096 | ||
25 | ; Asset's expiration time (minutes) | ||
26 | ; ExpirationTime = 30 \ No newline at end of file | ||
diff --git a/bin/config-include/StandaloneCommon.ini.example b/bin/config-include/StandaloneCommon.ini.example index d607092..d772256 100644 --- a/bin/config-include/StandaloneCommon.ini.example +++ b/bin/config-include/StandaloneCommon.ini.example | |||
@@ -1,6 +1,7 @@ | |||
1 | [Modules] | 1 | [Modules] |
2 | ;AssetCaching = "CoreAssetCache" | 2 | ;AssetCaching = "CoreAssetCache" |
3 | AssetCaching = "GlynnTuckerAssetCache" | 3 | AssetCaching = "GlynnTuckerAssetCache" |
4 | ;AssetCaching = "CenomeMemoryAssetCache" | ||
4 | 5 | ||
5 | [AssetCache] | 6 | [AssetCache] |
6 | ; Number of buckets for assets | 7 | ; Number of buckets for assets |
@@ -20,3 +21,10 @@ | |||
20 | AssetLoaderArgs = "assets/AssetSets.xml" | 21 | AssetLoaderArgs = "assets/AssetSets.xml" |
21 | 22 | ||
22 | 23 | ||
24 | ; Optional configurations for CenomeMemoryAssetCache | ||
25 | ; Cache size 128 MB (default: 134217728) | ||
26 | ; MaxSize = 134217728 | ||
27 | ; Maximal asset count | ||
28 | ; MaxCount = 4096 | ||
29 | ; Asset's expiration time (minutes) | ||
30 | ; ExpirationTime = 30 \ No newline at end of file | ||