From 32246319f6acc5629bfbeb7155ed4538db362e0a Mon Sep 17 00:00:00 2001 From: Paul Yuknewicz Date: Tue, 14 Nov 2023 09:57:05 -0800 Subject: [PATCH 1/2] added state management and ttl Signed-off-by: Paul Yuknewicz --- .../AspireWithDapr.Web/WeatherApiClient.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs index 9092390e0..e48f5b9cd 100644 --- a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs +++ b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs @@ -1,12 +1,29 @@ using Dapr.Client; + namespace AspireWithDapr.Web; public class WeatherApiClient(DaprClient daprClient) { public async Task GetWeatherAsync() { - return await daprClient.InvokeMethodAsync(HttpMethod.Get, "api", "weatherforecast"); + + // Get the weather from the state store if it exists + var weather = await daprClient.GetStateAsync("statestore", "weather"); + + if (weather is null) + { + // If it doesn't exist, get it from the weather service + weather = await daprClient.InvokeMethodAsync(HttpMethod.Get, "api", "weatherforecast"); + + await daprClient.SaveStateAsync("statestore", "weather", weather, metadata: new Dictionary() { + { + "ttlInSeconds", "120" + } + }); + } + + return weather; } } From cc58f239f3ae78ece95a1bd04440aab37799363c Mon Sep 17 00:00:00 2001 From: Paul Yuknewicz Date: Tue, 14 Nov 2023 10:05:14 -0800 Subject: [PATCH 2/2] created some constants for readability Signed-off-by: Paul Yuknewicz --- .../AspireWithDapr.Web/WeatherApiClient.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs index e48f5b9cd..316e8cf5a 100644 --- a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs +++ b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs @@ -3,27 +3,32 @@ namespace AspireWithDapr.Web; + public class WeatherApiClient(DaprClient daprClient) { + const string stateStore = "statestore"; + const string stateTTL = "120"; + const string apiAppId = "api"; + public async Task GetWeatherAsync() { // Get the weather from the state store if it exists - var weather = await daprClient.GetStateAsync("statestore", "weather"); + var weatherData = await daprClient.GetStateAsync(stateStore, "weather"); - if (weather is null) + if (weatherData is null) { // If it doesn't exist, get it from the weather service - weather = await daprClient.InvokeMethodAsync(HttpMethod.Get, "api", "weatherforecast"); + weatherData = await daprClient.InvokeMethodAsync(HttpMethod.Get, apiAppId, "weatherforecast"); - await daprClient.SaveStateAsync("statestore", "weather", weather, metadata: new Dictionary() { + await daprClient.SaveStateAsync(stateStore, "weather", weatherData, metadata: new Dictionary() { { - "ttlInSeconds", "120" + "ttlInSeconds", stateTTL } }); } - return weather; + return weatherData; } }