Overview
This article explains how to set a custom lifetime for variables stored in Action’s cache.
Applies To
- Custom Lifetime
- Actions
Solution
The “ttl” parameter sets the lifetime of the cached variable. By default, the caching period is 15 minutes, but a TTL of up to 24 hours can be set.
Here is a simple Action to test the Cache behavior:
- The “token” is an arbitrary name. The Action stores a fixed-length secret in the cache for 23 hours for the token. If the token is returned from the cache, it is a CACHE HIT; otherwise, it is a CACHE MISS.
- Using a TTL with a longer timeout will help get more hits from Action’s cache.
exports.onExecutePostLogin = async (event, api) => {
let token = "xxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzz.xxxxxxxxxyyyyyyy\
xxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyy\
zzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxx\
yyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzz\
xxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzz.xxxxxxxxxxyyyyyyy\
yyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxx\
xxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyyzzzzzzzzxxxxxxxxxxyyyyyyyyyyy";
let tokenFromCache = api.cache.get("tokenLogin");
if (tokenFromCache?.value === token) {
console.log("CACHE HIT");
} else {
let result = api.cache.set("token", token, {ttl: 23*60*60*1000});
console.log("CACHE MISS: ", result);
}
};