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

This commit is contained in:
ckohen
2021-08-06 05:54:19 -07:00
committed by GitHub
parent a0974fdbbb
commit 3725dcafc0
6 changed files with 37 additions and 7 deletions

View File

@@ -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.`,

View File

@@ -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.`,

View File

@@ -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

View File

@@ -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,
@@ -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: {

View File

@@ -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;