adonis5-cache icon indicating copy to clipboard operation
adonis5-cache copied to clipboard

Flushing cache with tags only removes tags record

Open evoactivity opened this issue 1 year ago • 0 comments

Flushing with tags only causes the tag record to be removed. The actual records are not removed.

Before Flush After Flush
image image

The expiration time is not being set correctly.

If I add to the cache at 2024-02-14 00:16:47 and the record will expire in one hour, the expiration time in the tag is 2024-02-14 00:16:47, not 2024-02-14 01:16:47

{
  "expirationTime": "2024-02-14T00:16:47.308Z",
  "keys": [
    "programmes:{\"filter\":{\"epgId\":\"abc.us\",\"startAt\":\"2024-02-14T00:00:00.000+00:00\"}}"
  ]
}

My cache config is this

import { CacheConfig } from '@ioc:Adonis/Addons/Adonis5-Cache';

export default {
  recordTTL: 60, // record ttl in minutes,

  ttlUnits: 'm', // time units for ttl record

  currentCacheStorage: 'redis', // storages which used as default cache storage

  enabledCacheStorages: ['in-memory', 'redis'], // storages which will be loaded

  cacheKeyPrefix: 'cache_record_', // prefix for keys, which will be stored in cache storage

  enabledEvents: {
    'cache-record:read': false,
    'cache-record:written': false,
    'cache-record:missed': false,
    'cache-record:forgotten': false,
  },
} as CacheConfig;

This is getting the TTL as 60, instead of it being converted to ms, so it's only adding 60ms to the expiraton instead of 3600000ms. https://github.com/reg2005/adonis5-cache/blob/cc28fd52616fd634defffc3436ab5e92f91db515/src/TaggableCacheManager.ts#L57-L65

Changing this to

protected async saveTaggedKeys(keys: string[], ttl: number = this.cacheManager.recordTTL) {
	const tagPayload = JSON.stringify({
		expirationTime: dayjs().add(ttl, this.cacheManager.cacheConfig.ttlUnits).toISOString(),
		keys,
	})
	await Promise.all(
		this._tags.map((tag) => this.storage.addTag(this.buildTagKey(tag), tagPayload))
	)
}

Fixes the issue.

Package version

1.3.1

Node.js and npm version

v18.17.0

Sample Code (to reproduce the issue)

await Cache.tags('programmes').put(cacheKey, programmes);

await Cache.tags('programmes').flush()

evoactivity avatar Feb 14 '24 00:02 evoactivity