mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
fix(Caching): sweep archived threads in all channel caches (#6312)
This commit is contained in:
@@ -13,7 +13,11 @@ let cacheWarningEmitted = false;
|
|||||||
class ChannelManager extends CachedManager {
|
class ChannelManager extends CachedManager {
|
||||||
constructor(client, iterable) {
|
constructor(client, iterable) {
|
||||||
super(client, Channel, 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;
|
cacheWarningEmitted = true;
|
||||||
process.emitWarning(
|
process.emitWarning(
|
||||||
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
|
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
|
||||||
|
|||||||
@@ -18,7 +18,11 @@ let cacheWarningEmitted = false;
|
|||||||
class GuildChannelManager extends CachedManager {
|
class GuildChannelManager extends CachedManager {
|
||||||
constructor(guild, iterable) {
|
constructor(guild, iterable) {
|
||||||
super(guild.client, GuildChannel, 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;
|
cacheWarningEmitted = true;
|
||||||
process.emitWarning(
|
process.emitWarning(
|
||||||
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
|
`Overriding the cache handling for ${this.constructor.name} is unsupported and breaks functionality.`,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const { TypeError } = require('../errors/DJSError.js');
|
|||||||
/**
|
/**
|
||||||
* Options for defining the behavior of a LimitedCollection
|
* Options for defining the behavior of a LimitedCollection
|
||||||
* @typedef {Object} LimitedCollectionOptions
|
* @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
|
* @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
|
* to keep an entry past the maximum size
|
||||||
* @property {?SweepFilter} [sweepFilter=null] A function ran every `sweepInterval` to determine how to sweep
|
* @property {?SweepFilter} [sweepFilter=null] A function ran every `sweepInterval` to determine how to sweep
|
||||||
|
|||||||
@@ -104,12 +104,17 @@ class Options extends null {
|
|||||||
shardCount: 1,
|
shardCount: 1,
|
||||||
makeCache: this.cacheWithLimits({
|
makeCache: this.cacheWithLimits({
|
||||||
MessageManager: 200,
|
MessageManager: 200,
|
||||||
|
ChannelManager: {
|
||||||
|
sweepInterval: 3600,
|
||||||
|
sweepFilter: require('./Util').archivedThreadSweepFilter(),
|
||||||
|
},
|
||||||
|
GuildChannelManager: {
|
||||||
|
sweepInterval: 3600,
|
||||||
|
sweepFilter: require('./Util').archivedThreadSweepFilter(),
|
||||||
|
},
|
||||||
ThreadManager: {
|
ThreadManager: {
|
||||||
sweepInterval: 3600,
|
sweepInterval: 3600,
|
||||||
sweepFilter: require('./LimitedCollection').filterByLifetime({
|
sweepFilter: require('./Util').archivedThreadSweepFilter(),
|
||||||
getComparisonTimestamp: e => e.archiveTimestamp,
|
|
||||||
excludeFromSweep: e => !e.archived,
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
messageCacheLifetime: 0,
|
messageCacheLifetime: 0,
|
||||||
@@ -154,6 +159,7 @@ class Options extends null {
|
|||||||
* @returns {CacheFactory}
|
* @returns {CacheFactory}
|
||||||
* @example
|
* @example
|
||||||
* // Store up to 200 messages per channel and discard archived threads if they were archived more than 4 hours ago.
|
* // 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({
|
* Options.cacheWithLimits({
|
||||||
* MessageManager: 200,
|
* MessageManager: 200,
|
||||||
* ThreadManager: {
|
* ThreadManager: {
|
||||||
|
|||||||
@@ -635,6 +635,21 @@ class Util extends null {
|
|||||||
setTimeout(resolve, ms);
|
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;
|
module.exports = Util;
|
||||||
|
|||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -1852,6 +1852,7 @@ export class UserFlags extends BitField<UserFlagsString> {
|
|||||||
|
|
||||||
export class Util extends null {
|
export class Util extends null {
|
||||||
private constructor();
|
private constructor();
|
||||||
|
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
|
||||||
public static basename(path: string, ext?: string): string;
|
public static basename(path: string, ext?: string): string;
|
||||||
public static binaryToId(num: string): Snowflake;
|
public static binaryToId(num: string): Snowflake;
|
||||||
public static cleanContent(str: string, channel: Channel): string;
|
public static cleanContent(str: string, channel: Channel): string;
|
||||||
|
|||||||
Reference in New Issue
Block a user