From 65d59c3819f6130867d32fbafa1305e9dc4e6daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 24 Aug 2024 00:36:57 +0200 Subject: [PATCH 1/2] fix(cache): don't force nitro prefix --- src/runtime/cache/server/api/_hub/cache/[...key].delete.ts | 2 +- src/runtime/cache/server/api/_hub/cache/[...key].get.ts | 4 ++-- .../cache/server/api/_hub/cache/batch-delete.post.ts | 2 +- .../cache/server/api/_hub/cache/clear/[...base].delete.ts | 2 +- src/runtime/cache/server/api/_hub/cache/index.get.ts | 7 ++----- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/runtime/cache/server/api/_hub/cache/[...key].delete.ts b/src/runtime/cache/server/api/_hub/cache/[...key].delete.ts index eba77ce1..bbec0715 100644 --- a/src/runtime/cache/server/api/_hub/cache/[...key].delete.ts +++ b/src/runtime/cache/server/api/_hub/cache/[...key].delete.ts @@ -15,7 +15,7 @@ export default eventHandler(async (event) => { message: 'Invalid key' }) } - const storage = useStorage('cache:nitro') + const storage = useStorage('cache') await storage.removeItem(key) return sendNoContent(event) diff --git a/src/runtime/cache/server/api/_hub/cache/[...key].get.ts b/src/runtime/cache/server/api/_hub/cache/[...key].get.ts index fa66f98e..b34ac269 100644 --- a/src/runtime/cache/server/api/_hub/cache/[...key].get.ts +++ b/src/runtime/cache/server/api/_hub/cache/[...key].get.ts @@ -12,13 +12,13 @@ export default eventHandler(async (event) => { const key = getRouterParam(event, 'key') || '' // If ends with an extension if (/\.[a-z0-9]{2,5}$/i.test(key)) { - const item = await useStorage('cache:nitro').getItem(key) + const item = await useStorage('cache').getItem(key) if (item) { return item } // Ignore if item is not found, treat the key as a prefix and look for children } - const storage = useStorage(`cache:nitro:${key}`) + const storage = useStorage(`cache:${key}`) const keys = await storage.getKeys() const stats = { diff --git a/src/runtime/cache/server/api/_hub/cache/batch-delete.post.ts b/src/runtime/cache/server/api/_hub/cache/batch-delete.post.ts index 8eca594e..65fa7015 100644 --- a/src/runtime/cache/server/api/_hub/cache/batch-delete.post.ts +++ b/src/runtime/cache/server/api/_hub/cache/batch-delete.post.ts @@ -13,7 +13,7 @@ export default eventHandler(async (event) => { keys: z.array(z.string().min(1)).min(1) }).parse) - const storage = useStorage('cache:nitro') + const storage = useStorage('cache') // delete with batch of 25 keys do { const keysToDelete = keys.splice(0, 25) diff --git a/src/runtime/cache/server/api/_hub/cache/clear/[...base].delete.ts b/src/runtime/cache/server/api/_hub/cache/clear/[...base].delete.ts index ff668e8f..b5e386f3 100644 --- a/src/runtime/cache/server/api/_hub/cache/clear/[...base].delete.ts +++ b/src/runtime/cache/server/api/_hub/cache/clear/[...base].delete.ts @@ -16,7 +16,7 @@ export default eventHandler(async (event) => { }) } - const storage = useStorage(`cache:nitro:${base}`) + const storage = useStorage(`cache:${base}`) const keys = await storage.getKeys() // delete with batch of 25 keys do { diff --git a/src/runtime/cache/server/api/_hub/cache/index.get.ts b/src/runtime/cache/server/api/_hub/cache/index.get.ts index 2f7f35d9..8517f720 100644 --- a/src/runtime/cache/server/api/_hub/cache/index.get.ts +++ b/src/runtime/cache/server/api/_hub/cache/index.get.ts @@ -8,12 +8,9 @@ export default eventHandler(async (event) => { await requireNuxtHubAuthorization(event) requireNuxtHubFeature('cache') - const cache = await useStorage('cache:nitro').getKeys() + const cache = await useStorage('cache').getKeys() - const stats: Record = { - handlers: 0, - functions: 0 - } + const stats: Record = {} for (const key of cache) { if (!key.includes(':')) continue From ac1bd56409011f8a1c0d1f30fd8090adc868c8ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 24 Aug 2024 00:43:37 +0200 Subject: [PATCH 2/2] update test --- test/cache.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/cache.test.ts b/test/cache.test.ts index dc5d42b0..044ab6cf 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -30,7 +30,7 @@ describe('KV', async () => { it('Fetch Keys List (empty)', async () => { const result = await $fetch('/api/_hub/cache') - expect(result).toMatchObject({ functions: 0, handlers: 0 }) + expect(result).toMatchObject({}) }) describe('Trigger Cached functions & handlers', () => { @@ -38,13 +38,16 @@ describe('KV', async () => { const result = await $fetch('/api/cached') expect(result).toMatchObject({ hello: 'world' }) - const result2 = await $fetch('/api/_hub/cache') - expect(result2).toMatchObject({ functions: 0, handlers: 1 }) + const entries = await $fetch('/api/_hub/cache') + expect(entries).toMatchObject({ nitro: 1 }) - const handlers = await $fetch('/api/_hub/cache/handlers') + const nitro = await $fetch('/api/_hub/cache/nitro') + expect(nitro).toMatchObject({ cache: [], groups: { handlers: 1 } }) + + const handlers = await $fetch('/api/_hub/cache/nitro/handlers') expect(handlers).toMatchObject({ cache: [], groups: { _: 1 } }) - const handlers_ = await $fetch>('/api/_hub/cache/handlers/_') + const handlers_ = await $fetch>('/api/_hub/cache/nitro/handlers/_') expect(handlers_.cache.length).greaterThan(0) cacheListFields.forEach(key => expect(handlers_.cache[0]).toHaveProperty(key))