Skip to content

[question] Invalidating cache bases #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
remihuigen opened this issue Mar 24, 2025 · 4 comments
Open

[question] Invalidating cache bases #515

remihuigen opened this issue Mar 24, 2025 · 4 comments
Labels
question Further information is requested

Comments

@remihuigen
Copy link

I was wondering what the most efficient way is to clear cache bases.

I was looking at this bit of code, where you're clearing a base 100 keys at a time

I've been trying to make unstorage's clear() method work , but with no luck (which is probably why nuxthub uses batches of 100 keys).

I have two questions related to this:

  • Why doesnt the clear method work?
  • What if a want to invalidate thousands of keys at a time? I would likely run into these limits: https://arc.net/l/quote/omawzasz
@atinux
Copy link
Contributor

atinux commented Mar 28, 2025

Hi @remihuigen

The biggest issue is that they don't allow to bulk delete using the binding:

Image

Actually I should change the batch to 10,000 keys so it will avoid making many calls, it won't run into the limits at using the batch delete.

How did you try to use the clear() method from unstorage?

I could expose a hubCache().clear(base) to make it work both locally, remotely & in production, would that help?

@atinux atinux added the question Further information is requested label Mar 28, 2025
@remihuigen
Copy link
Author

That would be great!

I think i tried both await useStorage('cache').clear(base) and await useStorage(`cache:${base}`).clear()

my current implementation is practically the same as yours

export default defineEventHandler(async (event) => {
  authenticateRequest(event)

  const { bases } = getQuery(event)

  let cacheBases: string[] = typeof bases === 'string' ? bases.split(',') : Array.isArray(bases) ? bases : []

  if (!cacheBases?.length) {
    cacheBases = [
      'nitro:routes',
      'pages',
      'kennisbank',
      'media',
      'navigator',
      'routes'
    ]
  }

  const baseKeys = cacheBases.map(base => !base.startsWith('cache:') ? `cache:${base}` : base)

  // Code is similar to https://github.com/nuxt-hub/core/blob/main/src/runtime/cache/server/api/_hub/cache/clear/%5B...base%5D.delete.ts
  // Except were clearing multiple bases at once
  await Promise.all(baseKeys.map(async (base) => {
    const storage = useStorage(base)
    const keys = await storage.getKeys()
    do {
      const keysToDelete = keys.splice(0, 100)
      await Promise.all(keysToDelete.map(async key => await storage.removeItem(key)))
    } while (keys.length)
  }))
  return {
    message: `Cache invalidated.`,
    bases: baseKeys
  }
})

@remihuigen
Copy link
Author

I also just now noted that await hubKV().clear(prefix) doesn't seem to work. Using await hubKV().clear() without a prefix clears all KV entries

I'm using it as documented here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants
@atinux @remihuigen and others