feat(Util): add SweptCollection for auto sweeping of caches (#6110)

Co-authored-by: DTrombett <73136330+DTrombett@users.noreply.github.com>
Co-authored-by: 1Computer1 <22125769+1Computer1@users.noreply.github.com>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Co-authored-by: NotSugden <28943913+NotSugden@users.noreply.github.com>
Co-authored-by: Shino <shinotheshino@gmail.com>
Co-authored-by: SpaceEEC <24881032+SpaceEEC@users.noreply.github.com>
Co-authored-by: Noel <icrawltogo@gmail.com>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
ckohen
2021-07-30 14:57:46 -07:00
committed by GitHub
parent 2675b0866c
commit dbb59ba1b2
16 changed files with 281 additions and 31 deletions

View File

@@ -35,10 +35,11 @@
* (e.g. recommended shard count, shard count of the ShardingManager)
* @property {CacheFactory} [makeCache] Function to create a cache.
* You can use your own function, or the {@link Options} class to customize the Collection used for the cache.
* @property {number} [messageCacheLifetime=0] How long a message should stay in the cache until it is considered
* sweepable (in seconds, 0 for forever)
* @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than
* the message cache lifetime (in seconds, 0 for never)
* @property {number} [messageCacheLifetime=0] DEPRECATED: Use `makeCache` with a `SweptCollection` instead.
* How long a message should stay in the cache until it is considered sweepable (in seconds, 0 for forever)
* @property {number} [messageSweepInterval=0] DEPRECATED: Use `makeCache` with a `SweptCollection` instead.
* How frequently to remove messages from the cache that are older than the message cache lifetime
* (in seconds, 0 for never)
* @property {MessageMentionOptions} [allowedMentions] Default value for {@link MessageOptions#allowedMentions}
* @property {number} [invalidRequestWarningInterval=0] The number of invalid REST requests (those that return
* 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings). That is, if set to 500,
@@ -99,7 +100,15 @@ class Options extends null {
static createDefault() {
return {
shardCount: 1,
makeCache: this.cacheWithLimits({ MessageManager: 200 }),
makeCache: this.cacheWithLimits({
MessageManager: 200,
ThreadManager: {
sweepFilter: require('./SweptCollection').filterByLifetime({
getComparisonTimestamp: e => e.archiveTimestamp,
excludeFromSweep: e => !e.archived,
}),
},
}),
messageCacheLifetime: 0,
messageSweepInterval: 0,
invalidRequestWarningInterval: 0,
@@ -134,20 +143,52 @@ class Options extends null {
}
/**
* Create a cache factory using predefined limits.
* @param {Record<string, number>} [limits={}] Limits for structures.
* Create a cache factory using predefined settings to sweep or limit.
* @param {Object<string, SweptCollectionOptions|number>} [settings={}] Settings passed to the relevant constructor.
* If no setting is provided for a manager, it uses Collection.
* If SweptCollectionOptions are provided for a manager, it uses those settings to form a SweptCollection
* If a number is provided for a manager, it uses that number as the max size for a LimitedCollection
* @returns {CacheFactory}
* @example
* // Store up to 200 messages per channel and discard archived threads if they were archived more than 4 hours ago.
* Options.cacheWithLimits({
* MessageManager: 200,
* ThreadManager: {
* sweepFilter: SweptCollection.filterByLifetime({
* getComparisonTimestamp: e => e.archiveTimestamp,
* excludeFromSweep: e => !e.archived,
* }),
* },
* });
* @example
* // Sweep messages every 5 minutes, removing messages that have not been edited or created in the last 30 minutes
* Options.cacheWithLimits({
* MessageManager: {
* sweepInterval: 300,
* sweepFilter: SweptCollection.filterByLifetime({
* lifetime: 1800,
* getComparisonTimestamp: e => e.editedTimestamp ?? e.createdTimestamp,
* })
* }
* });
*/
static cacheWithLimits(limits = {}) {
static cacheWithLimits(settings = {}) {
const Collection = require('./Collection');
const LimitedCollection = require('./LimitedCollection');
const SweptCollection = require('./SweptCollection');
return manager => {
const limit = limits[manager.name];
if (limit === null || limit === undefined || limit === Infinity) {
const setting = settings[manager.name];
if (typeof setting === 'number' && setting !== Infinity) return new LimitedCollection(setting);
if (
/* eslint-disable-next-line eqeqeq */
(setting?.sweepInterval == null && setting?.sweepFilter == null) ||
setting.sweepInterval <= 0 ||
setting.sweepInterval === Infinity
) {
return new Collection();
}
return new LimitedCollection(limit);
return new SweptCollection(setting);
};
}