mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 21:16:09 +00:00
feat: save data using guild_id as key
This commit is contained in:
parent
fd16a15b09
commit
6ea91315c3
46
src/cache/adapters/limited.ts
vendored
46
src/cache/adapters/limited.ts
vendored
@ -1,7 +1,7 @@
|
||||
import { LimitedCollection } from '../..';
|
||||
import { MergeOptions, type MakeRequired } from '../../common';
|
||||
import type { Adapter } from './types';
|
||||
|
||||
//TODO: optimizar esto
|
||||
export interface ResourceLimitedMemoryAdapter {
|
||||
expire?: number;
|
||||
limit?: number;
|
||||
@ -62,31 +62,35 @@ export class LimitedMemoryAdapter implements Adapter {
|
||||
get(keys: string[]): any[];
|
||||
get(keys: string | string[]) {
|
||||
if (!Array.isArray(keys)) {
|
||||
const data = this.storage.get(keys.split('.')[0])?.get(keys);
|
||||
const data = [...this.storage.values()].find(x => x.has(keys))?.get(keys);
|
||||
return data ? JSON.parse(data) : null;
|
||||
}
|
||||
return keys
|
||||
.map(x => {
|
||||
const data = this.storage.get(x.split('.')[0])?.get(x);
|
||||
.map(key => {
|
||||
const data = [...this.storage.values()].find(x => x.has(key))?.get(key);
|
||||
return data ? JSON.parse(data) : null;
|
||||
})
|
||||
.filter(x => x);
|
||||
}
|
||||
|
||||
private __set(key: string, data: any) {
|
||||
const namespace = key.split('.')[0];
|
||||
const __guildId = Array.isArray(data) ? data[0].guild_id : data.guild_id;
|
||||
const namespace = `${key.split('.')[0]}${__guildId ? `.${__guildId}` : ''}`;
|
||||
const self = this;
|
||||
if (!this.storage.has(namespace)) {
|
||||
this.storage.set(
|
||||
namespace,
|
||||
new LimitedCollection({
|
||||
expire: this.options[namespace as keyof LimitedMemoryAdapterOptions]?.expire ?? this.options.default.expire,
|
||||
limit: this.options[namespace as keyof LimitedMemoryAdapterOptions]?.limit ?? this.options.default.limit,
|
||||
expire:
|
||||
this.options[key.split('.')[0] as keyof LimitedMemoryAdapterOptions]?.expire ?? this.options.default.expire,
|
||||
limit:
|
||||
this.options[key.split('.')[0] as keyof LimitedMemoryAdapterOptions]?.limit ?? this.options.default.limit,
|
||||
resetOnDemand: true,
|
||||
onDelete(k) {
|
||||
const relation = self.relationships.get(namespace);
|
||||
const relationshipNamespace = key.split('.')[0];
|
||||
const relation = self.relationships.get(relationshipNamespace);
|
||||
if (relation) {
|
||||
switch (namespace) {
|
||||
switch (relationshipNamespace) {
|
||||
case 'guild':
|
||||
case 'user':
|
||||
self.removeToRelationship(namespace, k.split('.')[1]);
|
||||
@ -100,21 +104,15 @@ export class LimitedMemoryAdapter implements Adapter {
|
||||
break;
|
||||
case 'channel':
|
||||
case 'emoji':
|
||||
case 'overwrite':
|
||||
case 'presence':
|
||||
case 'role':
|
||||
case 'stage_instance':
|
||||
case 'sticker':
|
||||
case 'thread':
|
||||
{
|
||||
const split = k.split('.');
|
||||
for (const i of relation.entries()) {
|
||||
if (i[1].includes(split[1])) {
|
||||
self.removeToRelationship(i[0], split[1]);
|
||||
self.removeToRelationship(namespace, k.split('.')[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
case 'overwrite':
|
||||
self.removeToRelationship(namespace, k.split('.')[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -202,10 +200,11 @@ export class LimitedMemoryAdapter implements Adapter {
|
||||
const key = to.split('.')[0];
|
||||
if (!this.relationships.has(key)) this.relationships.set(key, new Map<string, string[]>());
|
||||
const relation = this.relationships.get(key)!;
|
||||
if (!relation.has(to)) {
|
||||
relation.set(to, []);
|
||||
const subrelationKey = to.split('.')[1] ?? '*';
|
||||
if (!relation.has(subrelationKey)) {
|
||||
relation.set(subrelationKey, []);
|
||||
}
|
||||
return relation!.get(to)!;
|
||||
return relation!.get(subrelationKey)!;
|
||||
}
|
||||
|
||||
bulkAddToRelationShip(data: Record<string, string[]>) {
|
||||
@ -231,11 +230,16 @@ export class LimitedMemoryAdapter implements Adapter {
|
||||
|
||||
removeToRelationship(to: string, keys: string | string[]) {
|
||||
const data = this.getToRelationship(to);
|
||||
// console.log({ data, to })
|
||||
if (data) {
|
||||
for (const key of Array.isArray(keys) ? keys : [keys]) {
|
||||
// console.log(data, key, '????')
|
||||
const idx = data.indexOf(key);
|
||||
if (idx !== -1) {
|
||||
console.log('borrado');
|
||||
data.splice(idx, 1);
|
||||
} else {
|
||||
console.log({ to, keys });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -194,10 +194,10 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
|
||||
type LimitedCollectionData<V> = { expire: number; expireOn: number; value: V };
|
||||
|
||||
export interface LimitedCollectionOptions<K> {
|
||||
export interface LimitedCollectionOptions<K, V> {
|
||||
limit: number;
|
||||
expire: number;
|
||||
onDelete?: (key: K) => void;
|
||||
onDelete?: (key: K, value: V) => void;
|
||||
resetOnDemand: boolean;
|
||||
}
|
||||
|
||||
@ -215,7 +215,7 @@ export interface LimitedCollectionOptions<K> {
|
||||
* console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
|
||||
*/
|
||||
export class LimitedCollection<K, V> {
|
||||
static readonly default: LimitedCollectionOptions<any> = {
|
||||
static readonly default: LimitedCollectionOptions<any, any> = {
|
||||
resetOnDemand: false,
|
||||
limit: Number.POSITIVE_INFINITY,
|
||||
expire: 0,
|
||||
@ -223,10 +223,10 @@ export class LimitedCollection<K, V> {
|
||||
|
||||
private readonly data = new Map<K, LimitedCollectionData<V>>();
|
||||
|
||||
private readonly options: LimitedCollectionOptions<K>;
|
||||
private readonly options: LimitedCollectionOptions<K, V>;
|
||||
private timeout: NodeJS.Timeout | undefined = undefined;
|
||||
|
||||
constructor(options: Partial<LimitedCollectionOptions<K>> = {}) {
|
||||
constructor(options: Partial<LimitedCollectionOptions<K, V>> = {}) {
|
||||
this.options = MergeOptions(LimitedCollection.default, options);
|
||||
}
|
||||
|
||||
@ -331,8 +331,10 @@ export class LimitedCollection<K, V> {
|
||||
*/
|
||||
delete(key: K) {
|
||||
const value = this.raw(key);
|
||||
if (value && value.expireOn === this.closer?.expireOn) setImmediate(() => this.resetTimeout());
|
||||
this.options.onDelete?.(key);
|
||||
if (value) {
|
||||
if (value.expireOn === this.closer?.expireOn) setImmediate(() => this.resetTimeout());
|
||||
this.options.onDelete?.(key, value.value);
|
||||
}
|
||||
return this.data.delete(key);
|
||||
}
|
||||
|
||||
@ -424,7 +426,7 @@ export class LimitedCollection<K, V> {
|
||||
continue;
|
||||
}
|
||||
if (Date.now() >= value.expireOn) {
|
||||
this.options.onDelete?.(key);
|
||||
this.options.onDelete?.(key, value.value);
|
||||
this.data.delete(key);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user