Cache is a temporary storage layer that keeps frequently accessed data readily available for quick access. In Seyfert, the cache system stores Discord data in memory by default, though it can be configured to use other storage solutions like Redis.
Resources
All entities supported by Seyfert’s cache are resources, such as channels, users, members, etc. Each of these resources is managed in the same way, but they can be modified and handled differently depending on the Adapter.
Disabling
Seyfert allows you to disable these resources separately.
Seyfert allows you to provide your own adapter for the cache, which you can think of as a driver to let Seyfert use an unsupported tool. By default, Seyfert includes MemoryAdapter and LimitedMemoryAdapter, both of which operate in RAM. Additionally, Seyfert has official Redis support through the Redis Adapter.
Building Your Own Cache
Custom Resource
A custom resource is just a new cache entity, so integrating it is relatively simple. Let’s take the example of the Cooldown resource from the cooldown package.
It’s important to note that Seyfert provides a base for three types of resources:
BaseResource: a basic entity, which should be completely independent
GuildBaseResource: an entity linked to a guild (like bans)
GuildRelatedResource: an entity that may or may not be linked to a guild (like messages)
Don’t like storing the cache in memory or Redis? Or maybe you just want to do it your own way?
Here, you’ll learn how to create your own cache adapter.
Before You Start
Consider whether your adapter might be asynchronous; if it is, you’ll need to specify it:
import {
(alias) interfaceAdapter
importAdapter
Adapter } from'seyfert';
class
classMyAdapter
MyAdapterimplements
(alias) interfaceAdapter
importAdapter
Adapter {
MyAdapter.isAsync: boolean
isAsync=true;
async
MyAdapter.start(): Promise<void>
start() {
// This function will run before starting the bot
}
}
This guide is for creating an asynchronous adapter. If you want a synchronous one, simply do not return a promise in any of the methods (the start method can be asynchronous).
Storing Data
In Seyfert’s cache, there are relationships, so you can know who a resource belongs to.
There are four methods you must implement in your adapter to store values: set, patch, bulkPatch, and bulkSet.
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
@param ― limit A value used to limit the number of elements returned in the array.
split('.');
// Your client will likely have a more optimized way to do this.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
@param ― limit A value used to limit the number of elements returned in the array.
Determines whether all the members of an array satisfy the specified test.
@param ― predicate A function that accepts up to three arguments. The every method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value false, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.
every((
value: string
value,
i: number
i) => (
constsq:string[]
sq[
i: number
i] ==='*'?!!
value: string
value:
constsq:string[]
sq[
i: number
i] ===
value: string
value));
if (
constmatch:boolean
match) {
constvalues:unknown[]
values.
Array<unknown>.push(...items: unknown[]): number
Appends new elements to the end of an array, and returns the new length of the array.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map(
key: string
key=>`${
to: string
to}.${
key: string
key}`);
}
async
MyAdapter.values(to: string): Promise<any[]>
values(
to: string
to:string) {
const
constarray:any[]
array:any[] = [];
const
constkeys:string[]
keys=awaitthis.
MyAdapter.keys(to: string): Promise<string[]>
keys(
to: string
to);
for (const
constkey:string
keyof
constkeys:string[]
keys) {
const
constcontent:any
content=awaitthis.
MyAdapter.get(key: string): Promise<any>
get(
constkey:string
key);
if (
constcontent:any
content) {
constarray:any[]
array.
Array<any>.push(...items: any[]): number
Appends new elements to the end of an array, and returns the new length of the array.