I have a method that performs a long action using async task
Now I want to add a caching mechanism that will be transparent in the same method. Now I could always extract the result of the cache and transfer it using the Task so that it would "work", but I want to prevent the context switch that I get.
Here is an example of what I have:
var result = await LongTask(); private async Task<string> LongTask() { return await DoSomethingLong(); }
And here is an example of what I want:
var result = await LongTask(); private async Task<string> LongTask() { if(isInCache) { return cachedValue(); // cache value is a const string you can do return "1" instead. } // else do the long thing and return my Task<string> return await DoSomethingLong(); }
Now I am surprised to see that this compiles and works. Something tells me that I am not doing it right.
Here is another example that I tested:
private async Task<string> DownloadString(bool sync) { using (WebClient wc = new WebClient()) { var task = wc.DownloadStringTaskAsync("http://www.nba.com"); if(sync) return task.Result; return await task; } }
And here is the code:
var res = DownloadString(true); string str1 = await res; var res2 = DownloadString(false); string str2 = await res2;
From what I read here task.Result executes the task synchronously and returns string . Now I see the request through Fiddler, and my program gets stuck in the return task.Result line, although I see 200 OK , and I have been waiting a long time.
Bottom line:
- What is the best \ right way to use caching inside an asynchronous method (for example, to do something synchronously in some cases without creating a
context switch overhead ?
- Why is my second block of code stuck with
DownloadString ?
source share