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

[Bug?]: deleteCacheKey() does not format key like cache() & cacheFindMany() #7346

Closed
1 task
ched-dev opened this issue Jan 14, 2023 · 3 comments · Fixed by #7362
Closed
1 task

[Bug?]: deleteCacheKey() does not format key like cache() & cacheFindMany() #7346

ched-dev opened this issue Jan 14, 2023 · 3 comments · Fixed by #7362
Labels
bug/needs-info More information is needed for reproduction

Comments

@ched-dev
Copy link
Contributor

What's not working?

When I call deleteCacheKey([key, id]) it cannot find the key even though it is the same value passed to cache([key, id]) or cacheFindMany(key).

Code References:

const cache = async <TResult>(
key: CacheKey,
input: () => TResult | Promise<TResult>,
options?: CacheOptions
): Promise<any> => {
const cacheKey = formatCacheKey(key, prefix)

const cacheFindMany = async <TDelegate extends GenericDelegate>(
key: CacheKey,
model: TDelegate,
options: CacheFindManyOptions<Parameters<TDelegate['findMany']>[0]> = {}
) => {
const { conditions, ...rest } = options
const cacheKey = formatCacheKey(key, prefix)

const deleteCacheKey = async (key: CacheKey) => {
let result
try {
await Promise.race([
(result = client.del(key as string)),
wait(timeout).then(() => {
throw new CacheTimeoutError()
}),
])
logger?.debug(`[Cache] DEL '${key}'`)
return result
} catch (e: any) {
logger?.error(`[Cache] Error DEL '${key}': ${e.message}`)
return false
}
}

How do we reproduce the bug?

Using deleteCacheKey([key, id]) with a global prefix

  • Add a global prefix to your /api/src/lib/cache.ts file
export const { cache, cacheFindMany, cacheClient, deleteCacheKey } = createCache(client, {
  logger,
+  prefix: `prod`,
  timeout: 500,
})
  • Cache a query with an array of strings
export const productCategory: QueryResolvers['productCategory'] = ({ id }) => {
  return cache(['productCategories', id], () => {
    return db.productCategory.findUnique({
      where: { id },
    })
  })
}
  • When loaded next it will properly create the cache item with a key similar to prod-productCategories-GUID
  • Add a matching deleteCacheKey() for your update query
export const updateProductCategory: MutationResolvers['updateProductCategory'] =
  async ({ id, input }) => {
    await deleteCacheKey(['productCategories', id])

    return db.productCategory.update({
      data: input,
      where: { id },
    })
  }
  • Update the same item you previously viewed (productCategories.GUID)
  • Console will throw error because it cannot find the key
[Cache] Error DEL 'productCategories,GUID': Invalid argument type

What's your environment? (If it applies)

System:
    OS: macOS 12.0.1
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /private/var/folders/.../node
    Yarn: 3.2.0 - /private/var/folders/.../yarn
  Databases:
    SQLite: 3.36.0 - /usr/bin/sqlite3
  Browsers:
    Chrome: 109.0.5414.87
    Firefox: 107.0
    Safari: 15.1
  npmPackages:
    @redwoodjs/core: ^3.8.0 => 3.8.0

Are you interested in working on this?

  • I'm interested in working on this
@ched-dev ched-dev added the bug/needs-info More information is needed for reproduction label Jan 14, 2023
@arimendelow
Copy link
Contributor

just ran into the same issue :) thanks @cannikin for your great work on this!

@cannikin
Copy link
Member

cannikin commented Mar 2, 2023

Oh geez, I dunno why that wasn’t merged by now! I’ll take a look tomorrow.

@cannikin
Copy link
Member

cannikin commented Mar 2, 2023

Merged! Should be in 4.2.3 or 4.3.0, whichever comes next!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/needs-info More information is needed for reproduction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants