-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathValueTasks.cs
38 lines (33 loc) · 966 Bytes
/
ValueTasks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
class ValueTasks : IRunnable
{
public Task Run()
{
return this.LoopTenTimesAndSumResult(async i =>
{
ValueTask<int> valueTask = Get("Foo");
if (valueTask.IsCompleted)
{
this.PrintFastPath(i);
return valueTask.Result;
}
else
{
this.PrintAsyncPath(i);
return await valueTask.AsTask();
}
});
}
ValueTask<int> Get(string key)
{
if (cachedValues.TryGetValue(key, out int value))
{
return new ValueTask<int>(value);
}
return new ValueTask<int>(this.LoadFromFileAndCache(key));
}
internal ConcurrentDictionary<string, int> cachedValues = new ConcurrentDictionary<string, int>();
}