Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
using Dapr.Client;


namespace AspireWithDapr.Web;


public class WeatherApiClient(DaprClient daprClient)
{
const string stateStore = "statestore";
const string stateTTL = "120";
const string apiAppId = "api";

public async Task<WeatherForecast[]> GetWeatherAsync()
{
return await daprClient.InvokeMethodAsync<WeatherForecast[]>(HttpMethod.Get, "api", "weatherforecast");

// Get the weather from the state store if it exists
var weatherData = await daprClient.GetStateAsync<WeatherForecast[]>(stateStore, "weather");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This app also uses output caching at the UI layer. Does it make sense to use both? Should we remove the output caching and use this instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good point. Technically they're caching at different levels (API response vs. full HTML response) so could still be argued it's valuable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MOAR Caching


if (weatherData is null)
{
// If it doesn't exist, get it from the weather service
weatherData = await daprClient.InvokeMethodAsync<WeatherForecast[]>(HttpMethod.Get, apiAppId, "weatherforecast");

await daprClient.SaveStateAsync(stateStore, "weather", weatherData, metadata: new Dictionary<string, string>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the metadata parameter typed as Dictionary<string, string>? If so, we can simplify to use target-typed new here. If not, oh well 😄

{
"ttlInSeconds", stateTTL
}
});
}

return weatherData;
}
}

Expand Down