Skip to content

Commit

Permalink
feat: save data using guild_id as key
Browse files Browse the repository at this point in the history
  • Loading branch information
MARCROCK22 committed Apr 16, 2024
1 parent fd16a15 commit 6ea9131
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
48 changes: 26 additions & 22 deletions src/cache/adapters/limited.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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[]>) {
Expand All @@ -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 });
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -215,18 +215,18 @@ 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,
};

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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 6ea9131

Please sign in to comment.