14. Patterns: Cached Result

The cached result pattern is used to alleviate load on an underlying system by returning a result that is stored in Flowgear for a specific request. Typical reasons to use this pattern cases where:

  • A large number of similar requests come in over a short period of time
  • Querying the underlying service is slow
  • Querying the underlying service is expensive in some way. For example, it might cause a lot of memory to be allocated or a lot of CPU to be used

Under the pattern, the Workflow looks up the parameters for the request to see if there is a recent result for those parameters. If one is found, that stored result is returned instead of querying the underlying service. If no match is found, the underlying service is invoked and the result is stored (cached) so that it can be returned the next time the same request is received.

Exercise 19: Cached Result

In this exercise, we'll revisit the weather service we used in an earlier exercise but this time, we'll use caching to return the temperature of a city when it has been previously requested.

  1. Add Web Request 2, rename it Service and connect Start.RunNow → Service.

  2. Set Service.Url to http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={appid}&units=metric.

  3. Add Service.city and Service.appid.

  4. Add a Variable Bar, rename it Inputs.

  5. Add Inputs.city and Inputs.appid as Output Properties. Set appid to be a Hidden Property.

  6. Set the Inputs.appid Property value to c95dadb707ef003ac19b0236e63e348a.

  7. Set Inputs.city to a city name, for example atlanta.

  8. Connect Inputs.city → Service.city and Inputs.appid → Service.appid.

    Run the Workflow to check it's executing correctly.

  9. Add a Variable Bar to the right of Service, rename it to Outputs.

  10. Add Outputs.temperature.

  11. Connect Service.ResponseBody → Outputs.temperature, set the Data Mapping Expression to main.temp.

    Next, we will add a Key/Value to store the temperature of a city that has been queried.

  12. Move Outputs one block to the right and add Set Key-Value 2, rename it Store Temp, connect Service → Store Temp.

  13. Set Store Temp.Group to temperature.

  14. Connect Inputs.city → Store Temp.Key.

  15. Connect Service.ResponseBody → Store Temp.Value, set the Data Mapping Expression to main.temp.

    Run the Workflow and confirm the Set Key/Value step is storing the temperature correctly.

    Now we will introduce a test to determine whether the temperature for a city is already in the Key/Value store which is acting as our cache.

  16. Move Service three blocks to the right, in the space created, add Get Key-Value 2, rename to Query Temp, add DateTime Compare, rename to Compare and add If.

  17. Reconnect Start.RunNow → Query Temp, Query Temp → Compare, Compare → If and If.False → Service.

  18. Connect Query Temp.Missing → Service.

  19. Set Query Temp.MatchGroup to temperature.

  20. Connect Inputs.city → Query Temp.Key.

  21. Connect Query Temp.DateTime → Compare.DateTime1.

  22. Set Compare.TimeSpanUnit to Seconds.

    The DateTime Compare Node returns the difference between two dates and times. If one of the DateTime Properties is left empty, it defaults to the current UTC time for the comparison.

  23. Connect Compare.TimeSpan → If.Value.

  24. Set If.Expression to Value < 60.

    We are setting 60 seconds as the cache expiry period. This means that we will serve a temperature for a city from cache until that record is 60 seconds old.

  25. Connect Query Temp.Value → Outputs.temperature.

    Run the Workflow to see it re-cache the temperature for Atlanta, then run it again and it should return the cached temperature. Wait 60 seconds to see the cache invalidate and query the weather service again.

    Note that we don't need to connect any further steps to the If Node True Output because we already injected the temperature to Outputs.temperature from Query Temp.Value.

    Notice also that the Workflow runs very quickly when operating through the cache since the "expensive" step of invoking the weather service is skipped.

Save and run your Workflow, then click Submit Exercise to grade it.