Caching results
RAW supports caching of execution results. This allows results to be served faster but also helps alleviate load on the source systems where data is read from.
Caching should be used when it is acceptable from a business logic standpoint to serve slightly older results rather than systematically executing the code and reading the freshest data.
To cache results you must set the desired cache expiration in the "Cache Expiration in seconds" under the "Settings" tab when deploying an endpoint. The endpoint results during the first execution. Those results will be valid and reused until the specific cache expiration is reached.
For instance, if you define an endpoint with cache expiration of 10 minutes:
- The first time it is used, the results are computed and cached;
- Any requests during the next 10 minutes will return the same exact results. These will be served from the cache and no computation will be performed;
- Any request after 10 minutes will trigger exactly one recomputation of the results, which will again remain in cache for 10 minutes. If two parallel requests are received, they will be "merged" into a single request as to guarantee a single execution.
Example
For instance, you can deploy the following ISS Location endpoint, which tells you the current location of the International Space Station. After deploying it, remember to set the cache duration to 600 seconds. You will then see that the results will be computed during the first invocation and then reused for 10 minutes, after which they will be recomputed once more when requested.
If you want to try this example, you can deploy the following endpoint, but remember to set the "Cache Expiration in seconds" as discussed above:
ISS location
- Overview
- Code
Usage:
/examples/iss
let
iss = Json.InferAndRead("http://api.open-notify.org/iss-now.json"),
latitude = iss.iss_position.latitude,
longitude = iss.iss_position.longitude,
geoQuery = latitude + "," + longitude,
key = Environment.Secret("opencagedata"),
httpQuery = Http.Get(
"https://api.opencagedata.com/geocode/v1/json",
args = [{"q", geoQuery}, {"key", key}, {"no_annotations", "1"}]
),
issGeoLoc = Json.InferAndRead(httpQuery, preferNulls = true)
in
issGeoLoc.results.formatted