Skip to content

Commit 6323c5e

Browse files
authored
Address customer feedback, use more meaningful method names (#27456)
1 parent 0162d9d commit 6323c5e

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

docs/core/extensions/caching.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Caching in .NET
33
description: Learn how to use various in-memory and distributed caching mechanisms in .NET.
44
author: IEvangelist
55
ms.author: dapine
6-
ms.date: 11/12/2021
6+
ms.date: 12/08/2021
77
---
88

99
# Caching in .NET
@@ -55,9 +55,9 @@ Depending on your .NET workload, you may access the `IMemoryCache` differently;
5555

5656
:::code source="snippets/caching/memory-apis/Program.cs" range="9-10":::
5757

58-
With in-memory caching services registered, and resolved through DI — you're ready to start caching. This sample iterates through the letters in the English alphabet 'A' through 'Z'. There is a `record` that holds the reference to the letter, and generates a message.
58+
With in-memory caching services registered, and resolved through DI — you're ready to start caching. This sample iterates through the letters in the English alphabet 'A' through 'Z'. The `record AlphabetLetter` type holds the reference to the letter, and generates a message.
5959

60-
:::code source="snippets/caching/memory-apis/Program.cs" range="68-72":::
60+
:::code source="snippets/caching/memory-apis/Program.cs" range="70-74":::
6161

6262
The sample includes a helper function that iterates through the alphabet letters:
6363

@@ -70,11 +70,11 @@ In the preceding C# code:
7070

7171
To add items to the cache call one of the `Create`, or `Set` APIs:
7272

73-
:::code source="snippets/caching/memory-apis/Program.cs" range="35-53" highlight="12-13":::
73+
:::code source="snippets/caching/memory-apis/Program.cs" range="35-54" highlight="12-13":::
7474

7575
In the preceding C# code:
7676

77-
- The invocation of `IterateAlphabetAsync` is awaited.
77+
- The variable `addLettersToCacheTask` delegates to `IterateAlphabetAsync` and is awaited.
7878
- The `Func<char, Task> asyncFunc` is argued with a lambda.
7979
- The `MemoryCacheEntryOptions` is instantiated with an absolute expiration relative to now.
8080
- A post eviction callback is registered.
@@ -90,7 +90,7 @@ The post eviction callback writes the details of the value that was evicted to t
9090

9191
Now that the cache is populated, another call to `IterateAlphabetAsync` is awaited, but this time you'll call <xref:Microsoft.Extensions.Caching.Memory.IMemoryCache.TryGetValue%2A?displayProperty=nameWithType>:
9292

93-
:::code source="snippets/caching/memory-apis/Program.cs" range="55-64":::
93+
:::code source="snippets/caching/memory-apis/Program.cs" range="56-66":::
9494

9595
If the `cache` contains the `letter` key, and the `value` is an instance of an `AlphabetLetter` it's written to the console. When the `letter` key is not in the cache, it was evicted and its post eviction callback was invoked.
9696

@@ -143,29 +143,29 @@ X was cached.
143143
Y was cached.
144144
Z was cached.
145145

146-
Q is still in cache. The 'Q' character is the 17 letter in the English alphabet.
147-
R is still in cache. The 'R' character is the 18 letter in the English alphabet.
148-
S is still in cache. The 'S' character is the 19 letter in the English alphabet.
149-
T is still in cache. The 'T' character is the 20 letter in the English alphabet.
150-
U is still in cache. The 'U' character is the 21 letter in the English alphabet.
151-
D was evicted for Expired.
146+
A was evicted for Expired.
152147
C was evicted for Expired.
153-
G was evicted for Expired.
148+
B was evicted for Expired.
154149
E was evicted for Expired.
150+
D was evicted for Expired.
155151
F was evicted for Expired.
156-
B was evicted for Expired.
157-
M was evicted for Expired.
158-
V is still in cache. The 'V' character is the 22 letter in the English alphabet.
159152
H was evicted for Expired.
160-
I was evicted for Expired.
161-
J was evicted for Expired.
162153
K was evicted for Expired.
163154
L was evicted for Expired.
164-
A was evicted for Expired.
155+
J was evicted for Expired.
156+
G was evicted for Expired.
157+
M was evicted for Expired.
165158
N was evicted for Expired.
166-
W is still in cache. The 'W' character is the 23 letter in the English alphabet.
167-
O was evicted for Expired.
159+
I was evicted for Expired.
168160
P was evicted for Expired.
161+
R was evicted for Expired.
162+
O was evicted for Expired.
163+
Q was evicted for Expired.
164+
S is still in cache. The 'S' character is the 19 letter in the English alphabet.
165+
T is still in cache. The 'T' character is the 20 letter in the English alphabet.
166+
U is still in cache. The 'U' character is the 21 letter in the English alphabet.
167+
V is still in cache. The 'V' character is the 22 letter in the English alphabet.
168+
W is still in cache. The 'W' character is the 23 letter in the English alphabet.
169169
X is still in cache. The 'X' character is the 24 letter in the English alphabet.
170170
Y is still in cache. The 'Y' character is the 25 letter in the English alphabet.
171171
Z is still in cache. The 'Z' character is the 26 letter in the English alphabet.

docs/core/extensions/snippets/caching/memory-apis/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static async ValueTask IterateAlphabetAsync(
3232
Console.WriteLine();
3333
}
3434

35-
await IterateAlphabetAsync(letter =>
35+
var addLettersToCacheTask = IterateAlphabetAsync(letter =>
3636
{
3737
MemoryCacheEntryOptions options = new()
3838
{
@@ -51,8 +51,9 @@ await IterateAlphabetAsync(letter =>
5151
return Task.Delay(
5252
TimeSpan.FromMilliseconds(MillisecondsDelayAfterAdd));
5353
});
54+
await addLettersToCacheTask;
5455

55-
await IterateAlphabetAsync(letter =>
56+
var readLettersFromCacheTask = IterateAlphabetAsync(letter =>
5657
{
5758
if (cache.TryGetValue(letter, out object? value) &&
5859
value is AlphabetLetter alphabetLetter)
@@ -62,6 +63,7 @@ await IterateAlphabetAsync(letter =>
6263

6364
return Task.CompletedTask;
6465
});
66+
await readLettersFromCacheTask;
6567

6668
await host.RunAsync();
6769

0 commit comments

Comments
 (0)