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