Skip to content
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

fix(Caching): sweep archived threads in all channel caches #6312

Merged
merged 1 commit into from Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' ||
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
((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