type-cacheable
type-cacheable copied to clipboard
[info] how do clear all keys in a cache using @CacheClear without specifying every key?
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
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.
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?
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
.