Skip to content

Commit

Permalink
fix(Caching): sweep archived threads in all channel caches (#6312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Aug 6, 2021
1 parent a0974fd commit 3725dca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/managers/ChannelManager.js
Expand Up @@ -13,7 +13,11 @@ let cacheWarningEmitted = false;
class ChannelManager extends CachedManager {
constructor(client, iterable) {
super(client, Channel, iterable);
if (!cacheWarningEmitted && this._cache.constructor.name !== 'Collection') {
const defaultCaching =
this._cache.constructor.name === 'Collection' ||
((this._cache.maxSize === undefined || this._cache.maxSize === Infinity) &&
(this._cache.sweepFilter === undefined || this._cache.sweepFilter.isDefault));
if (!cacheWarningEmitted && !defaultCaching) {
cacheWarningEmitted = true;
process.emitWarning(
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
Expand Down
6 changes: 5 additions & 1 deletion src/managers/GuildChannelManager.js
Expand Up @@ -18,7 +18,11 @@ let cacheWarningEmitted = false;
class GuildChannelManager extends CachedManager {
constructor(guild, iterable) {
super(guild.client, GuildChannel, iterable);
if (!cacheWarningEmitted && this._cache.constructor.name !== 'Collection') {
const defaultCaching =
this._cache.constructor.name === 'Collection' ||
((this._cache.maxSize === undefined || this._cache.maxSize === Infinity) &&
(this._cache.sweepFilter === undefined || this._cache.sweepFilter.isDefault));
if (!cacheWarningEmitted && !defaultCaching) {
cacheWarningEmitted = true;
process.emitWarning(
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
Expand Down
2 changes: 1 addition & 1 deletion src/util/LimitedCollection.js
Expand Up @@ -15,7 +15,7 @@ const { TypeError } = require('../errors/DJSError.js');
/**
* Options for defining the behavior of a LimitedCollection
* @typedef {Object} LimitedCollectionOptions
* @property {?number} [maxSize=0] The maximum size of the Collection
* @property {?number} [maxSize=Infinity] The maximum size of the Collection
* @property {?Function} [keepOverLimit=null] A function, which is passed the value and key of an entry, ran to decide
* to keep an entry past the maximum size
* @property {?SweepFilter} [sweepFilter=null] A function ran every `sweepInterval` to determine how to sweep
Expand Down
14 changes: 10 additions & 4 deletions src/util/Options.js
Expand Up @@ -104,12 +104,17 @@ class Options extends null {
shardCount: 1,
makeCache: this.cacheWithLimits({
MessageManager: 200,
ChannelManager: {
sweepInterval: 3600,
sweepFilter: require('./Util').archivedThreadSweepFilter(),
},
GuildChannelManager: {
sweepInterval: 3600,
sweepFilter: require('./Util').archivedThreadSweepFilter(),
},
ThreadManager: {
sweepInterval: 3600,
sweepFilter: require('./LimitedCollection').filterByLifetime({
getComparisonTimestamp: e => e.archiveTimestamp,
excludeFromSweep: e => !e.archived,
}),
sweepFilter: require('./Util').archivedThreadSweepFilter(),
},
}),
messageCacheLifetime: 0,
Expand Down Expand Up @@ -154,6 +159,7 @@ class Options extends null {
* @returns {CacheFactory}
* @example
* // Store up to 200 messages per channel and discard archived threads if they were archived more than 4 hours ago.
* // Note archived threads will remain in the guild and client caches with these settings
* Options.cacheWithLimits({
* MessageManager: 200,
* ThreadManager: {
Expand Down
15 changes: 15 additions & 0 deletions src/util/Util.js
Expand Up @@ -635,6 +635,21 @@ class Util extends null {
setTimeout(resolve, ms);
});
}

/**
* Creates a sweep filter that sweeps archived threads
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
* @returns {SweepFilter}
*/
static archivedThreadSweepFilter(lifetime = 14400) {
const filter = require('./LimitedCollection').filterByLifetime({
lifetime,
getComparisonTimestamp: e => e.archiveTimestamp,
excludeFromSweep: e => !e.archived,
});
filter.isDefault = true;
return filter;
}
}

module.exports = Util;
1 change: 1 addition & 0 deletions typings/index.d.ts
Expand Up @@ -1852,6 +1852,7 @@ export class UserFlags extends BitField<UserFlagsString> {

export class Util extends null {
private constructor();
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
public static basename(path: string, ext?: string): string;
public static binaryToId(num: string): Snowflake;
public static cleanContent(str: string, channel: Channel): string;
Expand Down

0 comments on commit 3725dca

Please sign in to comment.