-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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(Util): add SweptCollection for auto sweeping of caches #6110
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.
Also, just one tiny question... we need to access to the Client
instance, in an object that is used to build the instance itself, do you have an example on how to use this?
From the discord, my suggestions on the interface:
export class SweptCollection<K, V> extends Collection<K, V> {
public constructor(options: SweptCollectionOptions<K, V>, iterable?: Iterable<readonly [K, V]>);
public maxSize: number;
public shouldKeep: ((value: V) => boolean) | null;
public sweepInterval: number | null;
public sweepFilter: (() => (value: V) => boolean) | null;
}
export interface SweptCollectionOptions<K, V> {
maxSize?: number;
shouldKeep?: (value: V) => boolean;
sweepInterval?: number;
sweepFilter?: () => (value: V) => boolean;
} Usage example: new SweptCollection({
sweepFilter: () => {
const now = Date.now();
return (t) => {
if (!t.createdTimestamp) return false;
if (!t.archived) return false;
return now - t.createdTimestamp > lifetimeMs;
};
}
});
// Can make some utilities for users:
new SweptCollection({
sweepFilter: SweptCollection.filterByLifetime({
maxLifetime: lifetimeMs,
getCreationTime: t => t.createdTimestamp,
shouldKeep: t => !t.archived
})
});
public static cacheByManager(fs: Record<string, () => Collection<any, any>>): CacheFactory { ... } |
Did most of what comp suggested, couple of name changes to make it clearer what these things do. The only thing I didn't touch:
What's the point in this, this seems harder than just implementing your own makeCache function. In fact, the only difference is that you're not building the default case. |
Co-Authored-By: DTrombett <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: Antonio Román <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]> Co-Authored-By: NotSugden <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: Shino <[email protected]>
Co-Authored-By: SpaceEEC <[email protected]>
BREAKING: updates node version requirement and bumps ecma version to 2021 Co-Authored-By: SpaceEEC <[email protected]>
Co-Authored-By: Noel <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Co-Authored-By: 1Computer1 <[email protected]>
Please describe the changes this PR makes and why it should be merged:
Eslint config has been updated to target es2021 as well.
Adds SweptCollection (mainly for use with threads but has other uses). This can replace Client#sweepMessages, however with the loss of debug events, which is why it was not removed.
Other breaking change is the change in ThreadManager, to automatically sweep archived threads older than 4 hours every hour (default sweep settings)
I'm open to changes for these defaults, kinda just picked them at random.
Status and versioning classification: