Skip to content
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

fix(cache): don't force nitro prefix #239

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/cache/server/api/_hub/cache/[...key].delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/cache/server/api/_hub/cache/[...key].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions src/runtime/cache/server/api/_hub/cache/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> = {
handlers: 0,
functions: 0
}
const stats: Record<string, number> = {}

for (const key of cache) {
if (!key.includes(':')) continue
Expand Down
13 changes: 8 additions & 5 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@ 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', () => {
it('Cached function', 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<Record<string, any>>('/api/_hub/cache/handlers/_')
const handlers_ = await $fetch<Record<string, any>>('/api/_hub/cache/nitro/handlers/_')
expect(handlers_.cache.length).greaterThan(0)

cacheListFields.forEach(key => expect(handlers_.cache[0]).toHaveProperty(key))
Expand Down