diff --git a/src/cache/adapters/limited.ts b/src/cache/adapters/limited.ts index 3bc58fe..2e2773b 100644 --- a/src/cache/adapters/limited.ts +++ b/src/cache/adapters/limited.ts @@ -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]); - break; - } - } - } + 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()); 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) { @@ -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 }); } } } diff --git a/src/collection.ts b/src/collection.ts index dac2e79..ac0e831 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -194,10 +194,10 @@ export class Collection extends Map { type LimitedCollectionData = { expire: number; expireOn: number; value: V }; -export interface LimitedCollectionOptions { +export interface LimitedCollectionOptions { limit: number; expire: number; - onDelete?: (key: K) => void; + onDelete?: (key: K, value: V) => void; resetOnDemand: boolean; } @@ -215,7 +215,7 @@ export interface LimitedCollectionOptions { * console.log(mappedArray); // Output: ['1: one', '2: two', '3: three'] */ export class LimitedCollection { - static readonly default: LimitedCollectionOptions = { + static readonly default: LimitedCollectionOptions = { resetOnDemand: false, limit: Number.POSITIVE_INFINITY, expire: 0, @@ -223,10 +223,10 @@ export class LimitedCollection { private readonly data = new Map>(); - private readonly options: LimitedCollectionOptions; + private readonly options: LimitedCollectionOptions; private timeout: NodeJS.Timeout | undefined = undefined; - constructor(options: Partial> = {}) { + constructor(options: Partial> = {}) { this.options = MergeOptions(LimitedCollection.default, options); } @@ -331,8 +331,10 @@ export class LimitedCollection { */ 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 { continue; } if (Date.now() >= value.expireOn) { - this.options.onDelete?.(key); + this.options.onDelete?.(key, value.value); this.data.delete(key); } }