|
3 | 3 | // See the License.txt file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System; |
6 | | -using System.Collections; |
7 | 6 | using System.Collections.Generic; |
8 | | -using System.Diagnostics; |
9 | 7 | using BitFaster.Caching.Lru; |
10 | | -using Xtensive.Collections; |
11 | | -using Xtensive.Conversion; |
12 | 8 | using Xtensive.Core; |
13 | 9 |
|
14 | 10 |
|
15 | 11 | namespace Xtensive.Caching |
16 | 12 | { |
17 | 13 | /// <summary> |
18 | | - /// A set of items limited by the maximal amount of memory it can use, or by any other measure. |
19 | | - /// Stores as many most frequently accessed items in memory as long as it is possible |
20 | | - /// while maintaining the total size of cached items less or equal to <see cref="MaxSize"/>. |
| 14 | + /// An adapter for <see cref="BitFaster.Caching.Lru.FastConcurrentLru{K, V}"/> type. |
21 | 15 | /// </summary> |
22 | 16 | /// <typeparam name="TKey">The key of the item.</typeparam> |
23 | 17 | /// <typeparam name="TItem">The type of the item to cache.</typeparam> |
24 | 18 | public class FastConcurrentLruCache<TKey, TItem> : |
25 | 19 | CacheBase<TKey, TItem> |
26 | 20 | { |
27 | | - private FastConcurrentLru<TKey, TItem> imp; |
| 21 | + private FastConcurrentLru<TKey, TItem> realCache; |
28 | 22 |
|
29 | 23 | /// <inheritdoc/> |
30 | | - public override int Count => imp.Count; |
| 24 | + public override int Count => realCache.Count; |
31 | 25 |
|
32 | 26 | /// <inheritdoc/> |
33 | 27 | public long MaxSize { get; private set; } |
34 | | - |
35 | | - /// <inheritdoc/> |
36 | | - public override void Clear() => //TODO: Change to imp.Clear() after updating BitFaster.Caching package to 1.0.4 |
37 | | - imp = new FastConcurrentLru<TKey, TItem>((int)MaxSize); |
38 | 28 |
|
39 | 29 | /// <inheritdoc/> |
40 | | - public override bool TryGetItem(TKey key, bool markAsHit, out TItem item) => imp.TryGet(key, out item); |
| 30 | + public override bool TryGetItem(TKey key, bool markAsHit, out TItem item) => realCache.TryGet(key, out item); |
41 | 31 |
|
42 | 32 | /// <inheritdoc/> |
43 | | - public override bool ContainsKey(TKey key) => imp.TryGet(key, out var _); |
| 33 | + public override bool ContainsKey(TKey key) => realCache.TryGet(key, out var _); |
44 | 34 |
|
45 | 35 | /// <inheritdoc/> |
46 | 36 | public override TItem Add(TItem item, bool replaceIfExists) |
47 | 37 | { |
48 | 38 | ArgumentValidator.EnsureArgumentNotNull(item, "item"); |
49 | 39 | var key = KeyExtractor(item); |
50 | 40 | if (replaceIfExists) { |
51 | | - imp.AddOrUpdate(key, item); |
| 41 | + realCache.AddOrUpdate(key, item); |
52 | 42 | return item; |
53 | 43 | } |
54 | 44 | else { |
55 | | - return imp.GetOrAdd(key, _ => item); |
| 45 | + return realCache.GetOrAdd(key, _ => item); |
56 | 46 | } |
57 | 47 | } |
58 | 48 |
|
59 | 49 | /// <inheritdoc/> |
60 | | - public override void RemoveKey(TKey key) => imp.TryRemove(key); |
| 50 | + public override void RemoveKey(TKey key) => realCache.TryRemove(key); |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + public override void RemoveKey(TKey key, bool removeCompletely) => realCache.TryRemove(key); |
61 | 54 |
|
62 | 55 | /// <inheritdoc/> |
63 | | - public override void RemoveKey(TKey key, bool removeCompletely) => imp.TryRemove(key); |
| 56 | + public override void Clear() => |
| 57 | + //TODO: Change to imp.Clear() after updating BitFaster.Caching package to 1.0.4 |
| 58 | + realCache = new FastConcurrentLru<TKey, TItem>((int) MaxSize); |
64 | 59 |
|
65 | 60 | /// <inheritdoc/> |
| 61 | + /// <exception cref="NotImplementedException"/> |
66 | 62 | public override IEnumerator<TItem> GetEnumerator() => throw new NotImplementedException(); |
67 | 63 |
|
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Initializes new instance of this type. |
| 67 | + /// </summary> |
| 68 | + /// <param name="maxSize">Max size of the original cache. Ideally it should be devisible by 3</param> |
| 69 | + /// <param name="keyExtractor">Extracts key value from caching item.</param> |
| 70 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxSize"/> is less than 3.</exception> |
68 | 71 | public FastConcurrentLruCache(int maxSize, Converter<TItem, TKey> keyExtractor) |
69 | 72 | { |
70 | | - if (maxSize <= 0) |
71 | | - ArgumentValidator.EnsureArgumentIsInRange(maxSize, 1, int.MaxValue, "maxSize"); |
| 73 | + ArgumentValidator.EnsureArgumentIsGreaterThanOrEqual(maxSize, 3, nameof(maxSize)); |
72 | 74 | MaxSize = maxSize; |
73 | 75 | KeyExtractor = keyExtractor; |
74 | | - imp = new FastConcurrentLru<TKey, TItem>(maxSize); |
| 76 | + realCache = new FastConcurrentLru<TKey, TItem>(maxSize); |
75 | 77 | } |
76 | 78 | } |
77 | 79 | } |
0 commit comments