isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.38k stars 353 forks source link

Not working in redux #232

Closed therohanchoudhary closed 2 years ago

therohanchoudhary commented 2 years ago

Reducer function:

const setValueInCache = (state, { key, convert }) => {
    const { lruCache } = state
    if (isUndefinedOrNullOrEmptyObject(lruCache)) {
        const newLruCache = new LRUCache(defaultCacheParams)
        const newValue = convert(key)
        newLruCache.set(key, newValue)
        return state.merge({ lruCache: newLruCache })
    }

    // error encountered here -> TypeError: lruCache.get is not a function at setValueInCache
    const value = lruCache.get(key)

    if (!isUndefinedOrNull(value)) {
        return state
    }

    const newValue = convert(key)
    lruCache.set(key, newValue)
    return state.merge({ lruCache })
}

Calling from redux:

const someFunction = (payload) => {
    const finalObject = {}
    Object.keys(payload).forEach((key) => {
        GLOBAL.store.dispatch(MiscActions.setValueInCache(
            key, 'someNamespace', (k) => k.split(' ').join('_'),
        ))
        const { misc: { lruCache } } = GLOBAL.store.getState()
        const newKey = lruCache.get(key)
        finalObject[newKey] = payload[key]
    })
    return finalObject
}
isaacs commented 2 years ago

Sorry, I don't know anything about redux.

    // error encountered here -> TypeError: lruCache.get is not a function at setValueInCache
    const value = lruCache.get(key)

So what is lruCache there? If it doesn't have a get function, it's not an LRUCache.

isaacs commented 2 years ago

Closing because this does not appear to be a LRUCache issue. Happy to share anything I see if you can provide more info, but I'm just a bystander. Good luck.