my cache loader function is being called every tim...
# ask-stanley-ai
b
my cache loader function is being called every time i call the get function
c
AI generated answer (using internal Slack threads):
Based on the conversations, it seems like you're dealing with a situation where your cache loader function is being called every time you call the get function. This is expected behavior in the context of
N/cache
in SuiteScript. When you use the
get
method that specifies the
loader
, it has the
ttl
(time to live). When the
ttl
expires, the lookup value would be null so it should then use the
loader
function to fetch the data again. This is a cycle that repeats itself: get data -> ttl expires -> use loader to fetch data again. Here's a simplified example of how you might use `N/cache`:
Copy code
javascript
const CACHE_KEY = 'active_locations';
const CACHE_DURATION = 3600; // Cache duration in seconds (e.g., 1 hour)

let locationCache = cache.getCache({
  name: CACHE_DURATION,
  scope: cache.Scope.PROTECTED
});

let cachedLocations = locationCache.get({ key: CACHE_KEY, loader: getActiveLocations });

function getActiveLocations() {
  // Your logic to fetch active locations
  // This function is called when the cache is empty or expired
}
In this example,
getActiveLocations
is the loader function. It's called when the cache is empty or expired. The
get
method checks the cache for the data associated with
CACHE_KEY
. If the data is not in the cache or has expired, it calls the
loader
function to fetch the data. Remember, the cache duration (
ttl
) is just a suggestion. Depending on what else is going on in your account, the cache might not last that long. So, you need to implement a failsafe check if empty and reload if it is. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.792821407
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.790243745
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.789340079
Please provide your feedback on this answer.