Use Cache
Sometimes you need to store temporary values such as tokens or deduplication keys, just use jiant.get.cache as follow :
Reference
jiant.get.cache() -> object
Get cache functions: tryGet, get, set.
js
// const { tryGet, get, set } = jiant.get.cache()
const cache = jiant.get.cache()
const tempKey = "custom_temp_key"
const maxExpireAgeInSecond = 3600 // in seconds
const getValueFunc = async ()=>{
await jiant.action.sleep(1000) // in milliseconds
// ...
return 'some_value'
}
// Fetch value with function then save it in cache
let value = await cache.tryGet(tempKey, getValueFunc, maxExpireAgeInSecond)
// output: 'some_value'
// Set value with key
await cache.set(tempKey, 'another_value', maxExpireAgeInSecond)
// Get value with key
value = await cache.get(tempKey)
// ouput: 'another_value'
// Cache will expire after expire time which set when saving.
await jiant.action.sleep(1000 * maxExpireAgeInSecond)
value = await cache.get(tempKey)
// output: nulltryGet(key, getValueFunc, maxAge, renewExpire, forceReload) -> Promise
| Param | Type | Required | Description |
|---|---|---|---|
| key | string | ✅ | Key to store and fetch cached value |
| getValueFunc | function | ✅ | async/sync function returns value to be cached when no value existed or previous value expired. |
| maxAge | number | ❌ | Default: 3600. Max age in seconds before value expired. |
| renewExpire | boolean | ❌ | Default: false. Whether to renew expiration with maxAge |
| forceReload | boolean | ❌ | Default: false. Whether to ignore current value and reload with getValueFunc. |
