Skip to content

Commit 4ebc940

Browse files
authored
Update README.md
1 parent af7049a commit 4ebc940

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,43 @@ High performance, thread-safe in-memory caching primitives for .NET.
1818

1919
# Usage
2020

21-
## LRU: ClassicLru, ConcurrentLru, ConcurrentTLru
21+
## LRU: ConcurrentLru, ConcurrentTLru
2222

23-
LRU implementations are intended as an alternative to the System.Runtime.Caching.MemoryCache family of classes (e.g. HttpRuntime.Cache, System.Web.Caching et. al.).
23+
LRU implementations are intended as a drop in replacement for ConcurrentDictionary, and a much faster alternative to the System.Runtime.Caching.MemoryCache family of classes (e.g. HttpRuntime.Cache, System.Web.Caching et. al.).
24+
25+
```csharp
26+
int concurrency = 4;
27+
int capacity = 666;
28+
var lru = new ConcurrentLru<int, SomeItem>(concurrency, capacity, EqualityComparer<int>.Default);
29+
30+
var value = lru.GetOrAdd(1, (k) => new SomeItem(k));
31+
```
2432

2533

2634
## Caching IDisposable objects
2735

2836
All cache classes in BitFaster.Caching own the lifetime of cached values, and will automatically dispose values when they are evicted.
2937

30-
To avoid races using objects after they have been disposed by the cache, wrap them with `Scoped`. The call to `CreateLifetime` creates a `Lifetime` that guarantees the scoped object will not be disposed until the lifetime is disposed. `Scoped` is thread safe, and lifetimes are valid for concurrent callers.
38+
To avoid races using objects after they have been disposed by the cache, wrap them with `Scoped`. The call to `CreateLifetime` creates a `Lifetime` that guarantees the scoped object will not be disposed until the lifetime is disposed. `Scoped` is thread safe, and guarantees correct disposal for concurrent lifetimes.
3139

3240
```csharp
33-
var lru = new ConcurrentLru<int, Scoped<SomeDisposable>>(2, 9, EqualityComparer<int>.Default);
41+
int concurrency = 4;
42+
int capacity = 666;
43+
var lru = new ConcurrentLru<int, Scoped<SomeDisposable>>(concurrency, capacity, EqualityComparer<int>.Default);
3444
var valueFactory = new SomeDisposableValueFactory();
3545

3646
using (var lifetime = lru.GetOrAdd(1, valueFactory.Create).CreateLifetime())
3747
{
3848
// lifetime.Value is guaranteed to be alive until the lifetime is disposed
3949
}
50+
51+
class SomeDisposableValueFactory
52+
{
53+
public Scoped<SomeDisposable>> Create(int key)
54+
{
55+
return new Scoped<SomeDisposable>(new SomeDisposable(key));
56+
}
57+
}
4058
```
4159

4260
## Caching Singletons by key

0 commit comments

Comments
 (0)