-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Managers): add customizable caching for managers (#6013)
- Loading branch information
1 parent
ec06ba7
commit 8c7cb0e
Showing
35 changed files
with
498 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const DataManager = require('./DataManager'); | ||
|
||
/** | ||
* Manages the API methods of a data model with a mutable cache of instances. | ||
* @extends {DataManager} | ||
* @abstract | ||
*/ | ||
class CachedManager extends DataManager { | ||
constructor(client, holds, iterable) { | ||
super(client, holds); | ||
|
||
Object.defineProperty(this, '_cache', { value: this.client.options.makeCache(this.constructor, this.holds) }); | ||
|
||
if (iterable) { | ||
for (const item of iterable) { | ||
this.add(item); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* The cache of items for this manager. | ||
* @type {Collection} | ||
* @abstract | ||
*/ | ||
get cache() { | ||
return this._cache; | ||
} | ||
|
||
add(data, cache = true, { id, extras = [] } = {}) { | ||
const existing = this.cache.get(id ?? data.id); | ||
if (cache) existing?._patch(data); | ||
if (existing) return existing; | ||
|
||
const entry = this.holds ? new this.holds(this.client, data, ...extras) : data; | ||
if (cache) this.cache.set(id ?? entry.id, entry); | ||
return entry; | ||
} | ||
} | ||
|
||
module.exports = CachedManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
const BaseManager = require('./BaseManager'); | ||
const { Error } = require('../errors'); | ||
|
||
let Structures; | ||
|
||
/** | ||
* Manages the API methods of a data model along with a collection of instances. | ||
* @extends {BaseManager} | ||
* @abstract | ||
*/ | ||
class DataManager extends BaseManager { | ||
constructor(client, holds) { | ||
super(client); | ||
|
||
if (!Structures) Structures = require('../util/Structures'); | ||
|
||
/** | ||
* The data structure belonging to this manager. | ||
* @name DataManager#holds | ||
* @type {Function} | ||
* @private | ||
* @readonly | ||
*/ | ||
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) ?? holds }); | ||
} | ||
|
||
/** | ||
* The cache of items for this manager. | ||
* @type {Collection} | ||
* @abstract | ||
*/ | ||
get cache() { | ||
throw new Error('NOT_IMPLEMENTED', 'get cache', this.constructor.name); | ||
} | ||
|
||
/** | ||
* Resolves a data entry to a data Object. | ||
* @param {string|Object} idOrInstance The id or instance of something in this Manager | ||
* @returns {?Object} An instance from this Manager | ||
*/ | ||
resolve(idOrInstance) { | ||
if (idOrInstance instanceof this.holds) return idOrInstance; | ||
if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) ?? null; | ||
return null; | ||
} | ||
|
||
/** | ||
* Resolves a data entry to an instance ID. | ||
* @param {string|Object} idOrInstance The id or instance of something in this Manager | ||
* @returns {?Snowflake} | ||
*/ | ||
resolveID(idOrInstance) { | ||
if (idOrInstance instanceof this.holds) return idOrInstance.id; | ||
if (typeof idOrInstance === 'string') return idOrInstance; | ||
return null; | ||
} | ||
|
||
valueOf() { | ||
return this.cache; | ||
} | ||
} | ||
|
||
module.exports = DataManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.