type-cacheable icon indicating copy to clipboard operation
type-cacheable copied to clipboard

[info] how do clear all keys in a cache using @CacheClear without specifying every key?

Open paulfrench opened this issue 3 years ago • 3 comments

How do clear all keys in a cache using @CacheClear without specifying every key?

It is sometimes useful to blow away the whole cache when an update/delete occurs

paulfrench avatar Nov 12 '20 12:11 paulfrench

I could see the use case for implementing this more properly, but for now you could accomplish this by doing the following (I think):

@CacheClear({ ..., isPattern: true, cacheKey: '.' })

This should work because the '.' will match all of your keys.

joshuaslate avatar Nov 13 '20 02:11 joshuaslate

I tried... @CacheClear({client: lruClient, isPattern: true, cacheKey: '.'}) @CacheClear({client: lruClient, isPattern: true, cacheKey: '.*'}) @CacheClear({client: lruClient, isPattern: true, cacheKey: '^.*$'})

Can't get it to clear the cache? Any ideas?

paulfrench avatar Nov 17 '20 15:11 paulfrench

For anyone coming to this from Redis, I think the pattern you use is dependant on the client you're using. For example, if using ioredis, your pattern is ultimately used as the pattern in a MATCH option for a SCAN operation.

https://github.com/joshuaslate/type-cacheable/blob/3e853576c04295158e246d079155c360d16f50fd/packages/ioredis-adapter/lib/index.ts#L74

In this case, you're limited mostly to * as a greedy wildcard on either side of your pattern (https://redis.io/commands/scan).

If you're using the node-cache client, your pattern is used to create a RegExp object in JavaScript - so your matching language is much more flexible.

https://github.com/joshuaslate/type-cacheable/blob/3e853576c04295158e246d079155c360d16f50fd/packages/node-cache-adapter/lib/index.ts#L51


From your snippets of what you've tried @paulfrench, I'm making the assumption you're using the lruClient. As such, your pattern is used to create a RegExp object much like node-cache.

atkinchris avatar Jul 06 '21 19:07 atkinchris