jaredwray/cacheable

cache.wrap is shared between cache instances

Closed this issue · 1 comments

Describe the bug
When invoking cache.wrap() simultaneously with the same key on two different instances, the result from the first method is returned for both invocations. This has major implications when the two caches store different data, or data-structures, which can result in wrong result or worst case reference error.

The culprit is promise-coalesce which stores all invocations in a global Map, regardless of which cache the invocation came from.

A fix can be include a per cache prefix to all coalesceAsync invocations.

How To Reproduce (best to provide workable code or tests!)

import { createCache, memoryStore } from 'cache-manager'

const cache1 = createCache(
    memoryStore({
        max: 50,
        ttl: 60000,
    }),
)

const cache2 = createCache(
    memoryStore({
        max: 50,
        ttl: 60000,
    }),
)

const [result1, result2] = await Promise.all([
    cache1.wrap('THE_SAME_KEY', () => Promise.resolve({ value: 1 })),
    cache2.wrap('THE_SAME_KEY', () => Promise.resolve({ value: 2 })),
])

console.log(result1) // { value: 1 }, correct
console.log(result2) // { value: 1 }, expected { value: 2 }

This has been released