Skip to content

Commit b1ec189

Browse files
committed
FastConcurrentLruCache imrovements
1 parent 8261200 commit b1ec189

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

Orm/Xtensive.Orm/Caching/FastConcurrentLruCache{TKey, TItem}.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,77 @@
33
// See the License.txt file in the project root for more information.
44

55
using System;
6-
using System.Collections;
76
using System.Collections.Generic;
8-
using System.Diagnostics;
97
using BitFaster.Caching.Lru;
10-
using Xtensive.Collections;
11-
using Xtensive.Conversion;
128
using Xtensive.Core;
139

1410

1511
namespace Xtensive.Caching
1612
{
1713
/// <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.
2115
/// </summary>
2216
/// <typeparam name="TKey">The key of the item.</typeparam>
2317
/// <typeparam name="TItem">The type of the item to cache.</typeparam>
2418
public class FastConcurrentLruCache<TKey, TItem> :
2519
CacheBase<TKey, TItem>
2620
{
27-
private FastConcurrentLru<TKey, TItem> imp;
21+
private FastConcurrentLru<TKey, TItem> realCache;
2822

2923
/// <inheritdoc/>
30-
public override int Count => imp.Count;
24+
public override int Count => realCache.Count;
3125

3226
/// <inheritdoc/>
3327
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);
3828

3929
/// <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);
4131

4232
/// <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 _);
4434

4535
/// <inheritdoc/>
4636
public override TItem Add(TItem item, bool replaceIfExists)
4737
{
4838
ArgumentValidator.EnsureArgumentNotNull(item, "item");
4939
var key = KeyExtractor(item);
5040
if (replaceIfExists) {
51-
imp.AddOrUpdate(key, item);
41+
realCache.AddOrUpdate(key, item);
5242
return item;
5343
}
5444
else {
55-
return imp.GetOrAdd(key, _ => item);
45+
return realCache.GetOrAdd(key, _ => item);
5646
}
5747
}
5848

5949
/// <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);
6154

6255
/// <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);
6459

6560
/// <inheritdoc/>
61+
/// <exception cref="NotImplementedException"/>
6662
public override IEnumerator<TItem> GetEnumerator() => throw new NotImplementedException();
6763

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>
6871
public FastConcurrentLruCache(int maxSize, Converter<TItem, TKey> keyExtractor)
6972
{
70-
if (maxSize <= 0)
71-
ArgumentValidator.EnsureArgumentIsInRange(maxSize, 1, int.MaxValue, "maxSize");
73+
ArgumentValidator.EnsureArgumentIsGreaterThanOrEqual(maxSize, 3, nameof(maxSize));
7274
MaxSize = maxSize;
7375
KeyExtractor = keyExtractor;
74-
imp = new FastConcurrentLru<TKey, TItem>(maxSize);
76+
realCache = new FastConcurrentLru<TKey, TItem>(maxSize);
7577
}
7678
}
7779
}

0 commit comments

Comments
 (0)