#cache key

1 messages · Page 1 of 1 (latest)

wet ice
#

is there a max cache key length?

const getCachedUser = unstable_cache(async (email) => backendGetUserFromEmail(email), [`pb-user-email-${email}`]);
not sure if adding th ewhole email wioll break it

hollow galleonBOT
#

Hi @wet ice. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, there are certain limitations when it comes to cache key lengths.

In Vercel's Edge Network, an error INTERNAL_CACHE_KEY_TOO_LONG can occur when the cache key is too long. This error can be caused by a request URL that is too long. However, the exact maximum length is not specified in the provided sources. If you encounter this error, you may need to shorten your cache key or contact Vercel support for further assistance (source (https://vercel.com/docs/errors/INTERNAL_CACHE_KEY_TOO_LONG#internal_cache_key_too_long)).

In terms of the key name for each item in your Edge Config, the maximum length is also not specified, but the key name must adhere to the regex pattern ^**A-z0-9_-]+$ and should not contain any special characters except for - or _ ([source** (https://vercel.com/docs/storage/edge-config/edge-config-limits#maximum-item-key-name-length)).

As for the unstable_cache function in Next.js, it doesn't specify a maximum length for the cache key. However, the keyParts parameter, which identifies the cached key, must contain globally unique values that together identify the key of the data being cached (source (https://nextjs.org/docs/app/api-reference/functions/unstable_cache)).

So, if you're using an email as part of your cache key, as long as it adheres to the regex pattern and doesn't cause the INTERNAL_CACHE_KEY_TOO_LONG error, it should be fine. However, keep in mind that the unstable_cache API is unstable and may change in the future.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wet ice kapa.ai is still learning and improving, please let us know how it did by reacting below

wet ice
#

@hollow galleon so im saving user email as a cache tag

const getCachedUser = unstable_cache(async (email) => backendGetUserFromEmail(email), ["pb-user-email"], {
        tags: ["pb-user-email"], // this cahce tag can be used to control cache invalidation (making it refresh the data)
        revalidate: false,
      });```
to make the tag for each email should i add the email to the tag? and then a general tag of all use remaisl?
hollow galleonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can add the email to the tag to create a unique tag for each user. This will allow you to control cache invalidation for individual users. Here's how you can do it:

const getCachedUser = unstable_cache(async (email) => backendGetUserFromEmail(email), [`pb-user-email-${email}`], {
  tags: [`pb-user-email-${email}`], // this cache tag can be used to control cache invalidation (making it refresh the data)
  revalidate: false,
});

In this case, pb-user-email-${email} will create a unique tag for each user based on their email.

If you want to have a general tag for all users, you can add another tag like pb-user-email-all:

const getCachedUser = unstable_cache(async (email) => backendGetUserFromEmail(email), [`pb-user-email-${email}`], {
  tags: [`pb-user-email-${email}`, 'pb-user-email-all'], // this cache tag can be used to control cache invalidation (making it refresh the data)
  revalidate: false,
});

In this case, pb-user-email-all can be used to invalidate the cache for all users at once.

Remember, you can use revalidateTag to purge the cache entries associated with a specific tag. For example, revalidateTag('pb-user-email-all') will invalidate the cache for all users (source (https://nextjs.org/docs/app/building-your-application/caching#fetch-optionsnexttags-and-revalidatetag)).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wet ice kapa.ai is still learning and improving, please let us know how it did by reacting below

wet ice
#

@hollow galleon but can i store email symbols in the cache? and whats the length limit

hollow galleonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The cache key and tag names should adhere to certain rules. In Vercel's Edge Config, the key name must adhere to the regex pattern ^**A-z0-9_-]+$ and should not contain any special characters except for - or _ ([source** (https://vercel.com/docs/storage/edge-config/edge-config-limits#maximum-item-key-name-length)). This means that email symbols like @ and . are not allowed in the cache key or tag names.

As for the length limit, the maximum size of an item in the cache is 2 MB (source (https://vercel.com/docs/infrastructure/data-cache/limits-and-pricing#limits)). However, the exact maximum length of the cache key or tag names is not specified in the provided sources.

If you want to use an email as part of your cache key or tag, you might need to encode it in a way that adheres to the regex pattern. For example, you could replace the @ symbol with -at- and the . symbol with -dot-.

Remember to decode it when you need to use the email again. Here's an example:

const encodedEmail = email.replace('@', '-at-').replace('.', '-dot-');
const getCachedUser = unstable_cache(async (email) => backendGetUserFromEmail(email), [`pb-user-email-${{encodedEmail}}`], {{
  tags: [`pb-user-email-${{encodedEmail}}`], // this cache tag can be used to control cache invalidation (making it refresh the data)
  revalidate: false,
}});

In this case, pb-user-email-${{encodedEmail}} will create a unique tag for each user based on their encoded email.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wet ice kapa.ai is still learning and improving, please let us know how it did by reacting below

wet ice
#

@hollow galleon then in the part where i actuall invalidate the cahce, id need to be able to invalidate the cache for the previousEncodedEmail. so can i use that same funcrtion

hollow galleonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can use the same function to invalidate the cache for the previousEncodedEmail. You would just need to call revalidateTag with the encoded email tag that you want to invalidate. Here's an example:

const previousEncodedEmail = previousEmail.replace('@', '-at-').replace('.', '-dot-');
revalidateTag(`pb-user-email-${{previousEncodedEmail}}`);

In this case, revalidateTag(pb-user-email-${{previousEncodedEmail}}) will invalidate the cache for the user with the previousEncodedEmail.

Remember, revalidateTag is used to purge the cache entries associated with a specific tag (source (https://nextjs.org/docs/app/building-your-application/caching#fetch-optionsnexttags-and-revalidatetag)).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: