Skip to content

Commit 97d9bd1

Browse files
authored
consistent names (#6)
1 parent 22b82ea commit 97d9bd1

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

Lightweight.Caching.Benchmarks/LruGetOrAddTest.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public void DictionaryGetOrAdd()
3838
dictionary.GetOrAdd(1, func);
3939
}
4040

41+
[Benchmark()]
42+
public DateTime DateTimeUtcNow()
43+
{
44+
return DateTime.UtcNow;
45+
}
46+
4147
[Benchmark]
4248
public void SegmentedLruGetOrAdd()
4349
{
@@ -46,28 +52,28 @@ public void SegmentedLruGetOrAdd()
4652
}
4753

4854
[Benchmark]
49-
public void OldLru2GetOrAdd()
55+
public void ClassNoTtlPolicyGetOrAdd()
5056
{
5157
Func<int, int> func = x => x;
5258
segmentedLru2.GetOrAdd(1, func);
5359
}
5460

5561
[Benchmark()]
56-
public void ConcurrentLruGetOrAdd()
62+
public void ConcurrentLruTemplGetOrAdd()
5763
{
5864
Func<int, int> func = x => x;
5965
concurrentLru.GetOrAdd(1, func);
6066
}
6167

6268
[Benchmark()]
63-
public void ConcurrentLruHitGetOrAdd()
69+
public void ConcurrentLruTemplHitGetOrAdd()
6470
{
6571
Func<int, int> func = x => x;
6672
concurrentLruHit.GetOrAdd(1, func);
6773
}
6874

6975
[Benchmark()]
70-
public void ConcurrentLruNoExpireGetOrAdd()
76+
public void ConcurrentLruGetOrAdd()
7177
{
7278
Func<int, int> func = x => x;
7379
concurrentLruNoExpire.GetOrAdd(1, func);
@@ -80,12 +86,6 @@ public void ConcurrentLruExpireGetOrAdd()
8086
concurrentLruExpire.GetOrAdd(1, func);
8187
}
8288

83-
[Benchmark()]
84-
public DateTime Now()
85-
{
86-
return DateTime.UtcNow;
87-
}
88-
8989
private int MyFunc(int i)
9090
{
9191
return i;

Lightweight.Caching/SingletonCache.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ public class SingletonCache<TKey, TValue>
1414

1515
public Handle Acquire(TKey key)
1616
{
17-
ReferenceCount lockObjectHolder = this.cache.AddOrUpdate(key,
17+
ReferenceCount refCount = this.cache.AddOrUpdate(key,
1818
(_) => new ReferenceCount(),
19-
(_, lockObjectHolder2) => lockObjectHolder2.PlusOneReferenceCountCopy());
19+
(_, existingRefCount) => existingRefCount.IncrementCopy());
2020

21-
return new Handle(key, lockObjectHolder.Value, this);
21+
return new Handle(key, refCount.Value, this);
2222
}
2323

2424
private void Release(TKey key)
2525
{
2626
while (true)
2727
{
28-
ReferenceCount oldLockObjectHolder = this.cache[key];
29-
ReferenceCount newLockObjectHolder = oldLockObjectHolder.MinusOneReferenceCountCopy();
30-
if (this.cache.TryUpdate(key, newLockObjectHolder, oldLockObjectHolder))
28+
ReferenceCount oldRefCount = this.cache[key];
29+
ReferenceCount newRefCount = oldRefCount.DecrementCopy();
30+
if (this.cache.TryUpdate(key, newRefCount, oldRefCount))
3131
{
32-
if (newLockObjectHolder.Count == 0)
32+
if (newRefCount.Count == 0)
3333
{
3434
// This will remove from dictionary only if key and the value with ReferenceCount (== 0) matches (under a lock)
35-
if (((IDictionary<TKey, ReferenceCount>)this.cache).Remove(new KeyValuePair<TKey, ReferenceCount>(key, newLockObjectHolder)))
35+
if (((IDictionary<TKey, ReferenceCount>)this.cache).Remove(new KeyValuePair<TKey, ReferenceCount>(key, newRefCount)))
3636
{
37-
if (newLockObjectHolder.Value is IDisposable d)
37+
if (newRefCount.Value is IDisposable d)
3838
{
3939
d.Dispose();
4040
}
@@ -89,12 +89,12 @@ public override bool Equals(object obj)
8989
return refCount != null && refCount.Value != null && refCount.Value.Equals(this.value) && refCount.referenceCount == this.referenceCount;
9090
}
9191

92-
public ReferenceCount PlusOneReferenceCountCopy()
92+
public ReferenceCount IncrementCopy()
9393
{
9494
return new ReferenceCount(this.value, this.referenceCount + 1);
9595
}
9696

97-
public ReferenceCount MinusOneReferenceCountCopy()
97+
public ReferenceCount DecrementCopy()
9898
{
9999
return new ReferenceCount(this.value, this.referenceCount - 1);
100100
}
@@ -104,13 +104,13 @@ public sealed class Handle : IDisposable
104104
{
105105
private TKey key;
106106
private TValue value;
107-
private SingletonCache<TKey, TValue> lockObjectCache;
107+
private SingletonCache<TKey, TValue> cache;
108108

109-
public Handle(TKey key, TValue lockObject, SingletonCache<TKey, TValue> lockObjectCache)
109+
public Handle(TKey key, TValue value, SingletonCache<TKey, TValue> cache)
110110
{
111111
this.key = key;
112-
this.value = lockObject;
113-
this.lockObjectCache = lockObjectCache;
112+
this.value = value;
113+
this.cache = cache;
114114
}
115115

116116
public TValue Value
@@ -123,10 +123,10 @@ public TValue Value
123123

124124
public void Dispose()
125125
{
126-
if (this.lockObjectCache != null)
126+
if (this.cache != null)
127127
{
128-
this.lockObjectCache.Release(this.key);
129-
this.lockObjectCache = null;
128+
this.cache.Release(this.key);
129+
this.cache = null;
130130
}
131131
}
132132
}

0 commit comments

Comments
 (0)