미리 계산된 작업 만들기
이 문서에서는 Task.FromResult 메서드를 사용하여 캐시에 보관된 비동기 다운로드 작업의 결과를 검색하는 방법을 알아봅니다. FromResult 메서드는 제공된 값을 Task<TResult> 속성으로 포함하는 완료된 Result 개체를 반환합니다. 이 메서드는 Task<TResult> 개체가 반환되는 비동기 작업을 수행하고 해당 Task<TResult> 개체의 결과가 계산되어 있을 때 유용합니다.
예시
다음 예제에서는 웹에서 문자열을 다운로드합니다. 이 예제에서는 DownloadStringAsync
메서드를 정의합니다. 이 메서드는 웹에서 문자열을 비동기적으로 다운로드합니다. 또한 이 예제에서는 ConcurrentDictionary<TKey,TValue>개체를 사용하여 이전 작업의 결과를 캐시합니다. 입력 주소가 이 캐시에 저장된 경우 DownloadStringAsync
는 FromResult 메서드를 사용하여 해당 주소의 콘텐츠를 포함하는 Task<TResult> 개체를 생성합니다. 그렇지 않은 경우에는 DownloadStringAsync
가 웹에서 파일을 다운로드하고 결과를 캐시에 추가합니다.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public static class DownloadCache
{
private static readonly ConcurrentDictionary<string, string> s_cachedDownloads = new();
private static readonly HttpClient s_httpClient = new();
public static Task<string> DownloadStringAsync(string address)
{
if (s_cachedDownloads.TryGetValue(address, out string? content))
{
return Task.FromResult(content);
}
return Task.Run(async () =>
{
content = await s_httpClient.GetStringAsync(address);
s_cachedDownloads.TryAdd(address, content);
return content;
});
}
public static async Task Main()
{
string[] urls = new[]
{
"https://learn.microsoft.com/aspnet/core",
"https://learn.microsoft.com/dotnet",
"https://learn.microsoft.com/dotnet/architecture/dapr-for-net-developers",
"https://learn.microsoft.com/dotnet/azure",
"https://learn.microsoft.com/dotnet/desktop/wpf",
"https://learn.microsoft.com/dotnet/devops/create-dotnet-github-action",
"https://learn.microsoft.com/dotnet/machine-learning",
"https://learn.microsoft.com/xamarin",
"https://dotnet.microsoft.com/",
"https://www.microsoft.com"
};
Stopwatch stopwatch = Stopwatch.StartNew();
IEnumerable<Task<string>> downloads = urls.Select(DownloadStringAsync);
static void StopAndLogElapsedTime(
int attemptNumber, Stopwatch stopwatch, Task<string[]> downloadTasks)
{
stopwatch.Stop();
int charCount = downloadTasks.Result.Sum(result => result.Length);
long elapsedMs = stopwatch.ElapsedMilliseconds;
Console.WriteLine(
$"Attempt number: {attemptNumber}\n" +
$"Retrieved characters: {charCount:#,0}\n" +
$"Elapsed retrieval time: {elapsedMs:#,0} milliseconds.\n");
}
await Task.WhenAll(downloads).ContinueWith(
downloadTasks => StopAndLogElapsedTime(1, stopwatch, downloadTasks));
// Perform the same operation a second time. The time required
// should be shorter because the results are held in the cache.
stopwatch.Restart();
downloads = urls.Select(DownloadStringAsync);
await Task.WhenAll(downloads).ContinueWith(
downloadTasks => StopAndLogElapsedTime(2, stopwatch, downloadTasks));
}
// Sample output:
// Attempt number: 1
// Retrieved characters: 754,585
// Elapsed retrieval time: 2,857 milliseconds.
// Attempt number: 2
// Retrieved characters: 754,585
// Elapsed retrieval time: 1 milliseconds.
}
Imports System.Collections.Concurrent
Imports System.Net.Http
Module Snippets
Class DownloadCache
Private Shared ReadOnly s_cachedDownloads As ConcurrentDictionary(Of String, String) =
New ConcurrentDictionary(Of String, String)()
Private Shared ReadOnly s_httpClient As HttpClient = New HttpClient()
Public Function DownloadStringAsync(address As String) As Task(Of String)
Dim content As String = Nothing
If s_cachedDownloads.TryGetValue(address, content) Then
Return Task.FromResult(Of String)(content)
End If
Return Task.Run(
Async Function()
content = Await s_httpClient.GetStringAsync(address)
s_cachedDownloads.TryAdd(address, content)
Return content
End Function)
End Function
End Class
Public Sub StopAndLogElapsedTime(
attemptNumber As Integer,
stopwatch As Stopwatch,
downloadTasks As Task(Of String()))
stopwatch.Stop()
Dim charCount As Integer = downloadTasks.Result.Sum(Function(result) result.Length)
Dim elapsedMs As Long = stopwatch.ElapsedMilliseconds
Console.WriteLine(
$"Attempt number: {attemptNumber}{vbCrLf}" &
$"Retrieved characters: {charCount:#,0}{vbCrLf}" &
$"Elapsed retrieval time: {elapsedMs:#,0} milliseconds.{vbCrLf}")
End Sub
Sub Main()
Dim cache As DownloadCache = New DownloadCache()
Dim urls As String() = {
"https://learn.microsoft.com/aspnet/core",
"https://learn.microsoft.com/dotnet",
"https://learn.microsoft.com/dotnet/architecture/dapr-for-net-developers",
"https://learn.microsoft.com/dotnet/azure",
"https://learn.microsoft.com/dotnet/desktop/wpf",
"https://learn.microsoft.com/dotnet/devops/create-dotnet-github-action",
"https://learn.microsoft.com/dotnet/machine-learning",
"https://learn.microsoft.com/xamarin",
"https://dotnet.microsoft.com/",
"https://www.microsoft.com"
}
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
Dim downloads As IEnumerable(Of Task(Of String)) =
urls.Select(AddressOf cache.DownloadStringAsync)
Task.WhenAll(downloads).ContinueWith(
Sub(downloadTasks)
StopAndLogElapsedTime(1, stopwatch, downloadTasks)
End Sub).Wait()
stopwatch.Restart()
downloads = urls.Select(AddressOf cache.DownloadStringAsync)
Task.WhenAll(downloads).ContinueWith(
Sub(downloadTasks)
StopAndLogElapsedTime(2, stopwatch, downloadTasks)
End Sub).Wait()
End Sub
' Sample output:
' Attempt number 1
' Retrieved characters: 754,585
' Elapsed retrieval time: 2,857 milliseconds.
'
' Attempt number 2
' Retrieved characters: 754,585
' Elapsed retrieval time: 1 milliseconds.
End Module
앞의 예에서는 각 URL이 처음 다운로드될 때 해당 값이 캐시에 저장됩니다. FromResult 메서드는 DownloadStringAsync
메서드가 이러한 미리 계산된 결과를 포함하는 Task<TResult> 개체를 만들 수 있도록 합니다. 문자열을 다운로드하기 위한 후속 호출은 캐시된 값을 반환하며 훨씬 빠릅니다.
참고 항목
.NET