-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat: refactor cache functions #3079
feat: refactor cache functions #3079
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed this decorator so no one will use the evil scan on the cache by mistake.
public async clearCache({ | ||
storeKeyPrefix, | ||
credentials, | ||
}: { | ||
storeKeyPrefix: CacheKeyPrefixEnum | CacheKeyPrefixEnum[]; | ||
credentials: { _id: string; _environmentId: string } | Record<string, unknown>; | ||
}) { | ||
Logger.log('Clearing the cache of keys with the specified prefixes'); | ||
Logger.debug('StoreKeyPrefix(s) are: ' + storeKeyPrefix); | ||
Logger.debug('Credentials are: ' + (credentials._id as string)); | ||
if (!this.cacheService?.cacheEnabled()) { | ||
Logger.verbose('Cashing service is not enabled to clear Cache.'); | ||
|
||
return; | ||
} | ||
|
||
if (Array.isArray(storeKeyPrefix)) { | ||
Logger.verbose('Mapping all keys to flush'); | ||
const invalidatePromises = storeKeyPrefix.map((prefix) => { | ||
return this.clearByPattern(prefix, credentials); | ||
}); | ||
|
||
Logger.debug('invalidate promises are: ' + invalidatePromises); | ||
Logger.verbose('Removing all keys with prefix'); | ||
await Promise.all(invalidatePromises); | ||
} else { | ||
Logger.warn('StoreKeyPrefix is not in array format'); | ||
await this.clearByPattern(storeKeyPrefix, credentials); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here removed this function so no one will use the evil scan on the cache by mistake.
I still left the 'clearByPattern' function because it is private, I may delete it in near future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split the entities keys builder functions to separate files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split the quieries keys builder functions to separate files.
Still did not have the time to check if we can optimize the notification templates invalidation. hopefully will look into it this week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great refactor. Even if it is quite subtle.
👏🏻
buildUserKey({ | ||
_id: command._id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one.
@@ -1,3 +1,2 @@ | |||
export * from './invalidate-cache.service'; | |||
export * from './cache.service'; | |||
export { CacheKeyPrefixEnum } from './keys'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would export the key builders so the imports would be simplified in the rest of the codebase.
type: CacheKeyTypeEnum.ENTITY, | ||
keyEntity: CacheKeyPrefixEnum.SUBSCRIBER, | ||
environmentId: _environmentId, | ||
identifierPrefix: 's', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would create constants for the different identifier prefixes. Or an enum.
And export them as we are reusing them in different places and can led to misalignment.
What change does this PR introduce?
General refactor i will add comments on the code in order to explain what and why.
Why was this change needed?
Other information (Screenshots)