Skip to content

Namespace clear doesn't work with Redis L2 keyPrefix #67

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

Closed
ifullgaz opened this issue Apr 29, 2025 · 0 comments
Closed

Namespace clear doesn't work with Redis L2 keyPrefix #67

ifullgaz opened this issue Apr 29, 2025 · 0 comments

Comments

@ifullgaz
Copy link
Contributor

I'm using Bentocache 1.2.2 with @adonisjs/cache 1.1.3.
I've configured my cache like so:

const cacheConfig = defineConfig({
  default: 'redis',

  prefix: '',

  stores: {
    memoryOnly: store().useL1Layer(drivers.memory()),

    redis: store()
      .useL1Layer(drivers.memory({
        maxSize: '100mb',
      }))
      .useL2Layer(drivers.redis({
        connectionName: 'main',
      }))
      .useBus(drivers.redisBus({
        connectionName: 'main'
      })),
  }
})

Redis is configured like so:

const redisConfig = defineConfig({
  connection: 'main',

  connections: {
    main: {
      host: env.get('REDIS_HOST'),
      port: env.get('REDIS_PORT'),
      password: env.get('REDIS_PASSWORD', ''),
      db: parseInt(env.get('REDIS_DB', '')),
      keyPrefix: env.get('REDIS_PREFIX', ''),
      retryStrategy(times) {
        return times > 10 ? null : times * 50
      },
      enableReadyCheck: true
    },
  },
})

Setting values using namespace works well:

const namespace = cache.namespace('test')
namespace.set({key, value})

Result: a key with the Redis L2 prefix and the namespace (for instance dev:test:my_key)

But

const namespace = cache.namespace('test')
namespace.clear()

Scans for test* keys only, it ignores the Redis L2 keyPrefix config.
Also, it's scanning for test* instead of test:*, which will catch namespaces like test2, testAbc...

I came up with a fix in drivers/redis.ts as follows:

  async clear() {
    let cursor = '0'
    const COUNT = 1000
    const keyPrefix = this.#connection.options.keyPrefix
    do {
      const [newCursor, keys] = await this.#connection.scan(
        cursor,
        'MATCH',
        `${keyPrefix}${this.prefix}:*`,
        'COUNT',
        COUNT,
      )

      if (keys.length)
        this.#connection.unlink(keys.map((key) => key.slice(keyPrefix.length)));

      cursor = newCursor
    } while (cursor !== '0')
  }

I'd be grateful if you could have a look at this and determine if it's suitable for inclusion to fix the bug.

Thanks!

ifullgaz added a commit to ifullgaz/bentocache that referenced this issue Apr 29, 2025
ifullgaz added a commit to ifullgaz/bentocache that referenced this issue Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants