Caching Issues with Pre User Registration Action

Hello!

In my Post Login Action, I have code that generates and caches a specific value that can be reused across multiple requests. This works well—when a new login request comes in, I can retrieve the cached value instead of regenerating it.

However, I’m now trying to use the same caching logic in a Pre User Registration Action. While Auth0’s documentation says caching is supported in this action, it appears that cached values are only retained for the duration of a single request. When a new registration request comes in, the cached value is no longer available. I am using a new and different cache key to avoid any collision issues as well, but the value still gets lost across multiple requests.

Is there anyone that can answer why this occurs? Is there any way to cache values across multiple requests like the Post Login action does?

exports.onExecutePreUserRegistration = async (event, api) => {
    console.log('*** Test Cache ***');
	console.log(`Test Key At Start value: ${api.cache.get('test')?.value}`);
    api.cache.set('test', 'iamasecretvalue', { ttl: 9999999999 });
	console.log(`Test Key After Being Set value: ${api.cache.get('test')?.value}`);
    console.log('************************')


	console.log("************** End of Pre-Registration Action ***************");
};

When this action gets hit multiple times across multiple requests, the cache does not contain the value for the test key. It is only present in the cache once set for the single request.

Hello,

Your TTL value 9999999999 is extremely large; if persistent caching was available, it would keep the value for a very long time.

Best Regard,
Theresa

Hi @Titorr2001

Welcome to the Auth0 Community!

As mentioned in our documentation in regards to the PreUserRegistration Trigger API Object:

api.cache.get(key)

Retrieve a record describing a cached value at the supplied key, if it exists. If a record is found, the cached value can be found at the value property of the returned object.

Returns a cache record if an item is found in the cache for the supplied key. Cache records are objects with a value property holding the cached value as well as an expires_at property indicating the maximum expiry of the record in milliseconds since the Unix epoch.

Important: This cache is designed for short-lived, ephemeral data. Items may not be available in later transactions even if they are within their supplied their lifetime.

It appears that even if you set a prolonged lifetime for the cached value, it is intended behaviour for the cache to not be available in later transactions.

Also, for the api.cache.set(key, value, [options]) parameters, it appears that the ttl value is available under the options.ttl parameter, you might want to try changing that in your code and see if it affects the end result. You can also try changing the options.expires_at value.

You can read more about these here.

If you have any other questions, let me know!

Kind Regards,
Nik