Skip to content

Namespaced cache with memory driver revert serialize to true #121

Description

@JosephMarotte

I’m hitting the following issue when I’m using bentocache 1.6.1 as a memory cache with serialize to false.
Namespaced cache created from the initial cache do not preserve the serialize property

A possible workaround is to use tags and not rely on namespaces

You can find below a test generated by LLM that exhibit this behavior.

import { test } from '@japa/runner'
import { BentoCache, bentostore } from 'bentocache'
import { memoryDriver } from 'bentocache/drivers/memory'

test.group('bentocache namespace() + serialize:false interaction', () => {
  test('non-namespaced getOrSet preserves a Map value across a cache hit', async ({ assert }) => {
    const cache = new BentoCache({
      default: 'memory',
      stores: {
        memory: bentostore().useL1Layer(memoryDriver({ maxItems: 100, serialize: false })),
      },
    }).use('memory')

    await cache.getOrSet({ key: 'foo', factory: async () => new Map([['a', 1]]) })
    const hit = await cache.getOrSet({
      key: 'foo',
      factory: async () => {
        throw new Error('should have been served from cache')
      },
    })

    assert.instanceOf(hit, Map)
  })

  test('KNOWN BUG: namespaced getOrSet does NOT preserve a Map value across a cache hit', async ({
    assert,
  }) => {
    const cache = new BentoCache({
      default: 'memory',
      stores: {
        memory: bentostore().useL1Layer(memoryDriver({ maxItems: 100, serialize: false })),
      },
    }).use('memory')

    const nsCache = cache.namespace("ns");

    await nsCache.getOrSet({ key: 'bar', factory: async () => new Map([['a', 1]]) })
    const hit = await nsCache.getOrSet({
      key: 'bar',
      factory: async () => {
        throw new Error('should have been served from cache')
      },
    })

    // Expected outcome given the bug: the Map got JSON-round-tripped into `{}`.
    assert.notInstanceOf(hit, Map)
  })
})

The cache-driver page have information about not setting serialize: false with an L2 cache but I have none set-up here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions