fix: dont encode cache data by default

This commit is contained in:
MARCROCK22 2024-09-20 03:42:22 +00:00
parent 6395dd5938
commit 24cfd39001
3 changed files with 67 additions and 12 deletions

View File

@ -14,10 +14,10 @@ export class MemoryAdapter<T> implements Adapter {
constructor( constructor(
public options: MemoryAdapterOptions<T> = { public options: MemoryAdapterOptions<T> = {
encode(data) { encode(data) {
return JSON.stringify(data) as T; return data;
}, },
decode(data) { decode(data) {
return JSON.parse(data as string); return data;
}, },
}, },
) {} ) {}

View File

@ -47,10 +47,10 @@ export class LimitedMemoryAdapter<T> implements Adapter {
limit: Number.POSITIVE_INFINITY, limit: Number.POSITIVE_INFINITY,
}, },
encode(data) { encode(data) {
return JSON.stringify(data) as T; return data;
}, },
decode(data) { decode(data) {
return JSON.parse(data as string); return data;
}, },
} satisfies LimitedMemoryAdapterOptions<T>, } satisfies LimitedMemoryAdapterOptions<T>,
options, options,
@ -111,10 +111,6 @@ export class LimitedMemoryAdapter<T> implements Adapter {
const existsRelation = self.relationships.has(relationshipNamespace); const existsRelation = self.relationships.has(relationshipNamespace);
if (existsRelation) { if (existsRelation) {
switch (relationshipNamespace) { switch (relationshipNamespace) {
case 'guild':
case 'user':
self.removeToRelationship(namespace, k.split('.')[1]);
break;
case 'ban': case 'ban':
case 'member': case 'member':
case 'voice_state': case 'voice_state':
@ -134,6 +130,11 @@ export class LimitedMemoryAdapter<T> implements Adapter {
case 'message': case 'message':
self.removeToRelationship(namespace, k.split('.')[1]); self.removeToRelationship(namespace, k.split('.')[1]);
break; break;
// case 'guild':
// case 'user':
default:
self.removeToRelationship(namespace, k.split('.')[1]);
break;
} }
} }
}, },
@ -214,10 +215,6 @@ export class LimitedMemoryAdapter<T> implements Adapter {
?.delete(`${parentNamespace}.${keySplit.at(1)}.${keySplit.at(2)}`); ?.delete(`${parentNamespace}.${keySplit.at(1)}.${keySplit.at(2)}`);
} }
break; break;
case 'user':
case 'guild':
this.storage.get(parentNamespace)?.delete(`${parentNamespace}.${keySplit.at(1)}`);
break;
case 'channel': case 'channel':
case 'emoji': case 'emoji':
case 'presence': case 'presence':
@ -237,6 +234,11 @@ export class LimitedMemoryAdapter<T> implements Adapter {
} }
} }
break; break;
// case 'user':
// case 'guild':
default:
this.storage.get(parentNamespace)?.delete(`${parentNamespace}.${keySplit.at(1)}`);
break;
} }
} }

53
tests/cache.test.js Normal file
View File

@ -0,0 +1,53 @@
//@ts-check
const { describe, test } = require('node:test');
const { Client } = require('../lib/index');
const { LimitedMemoryAdapter, MemoryAdapter } = require('../lib/index');
// all intents
const intents = 53608447;
describe('test memory cache adapter', () => {
const adapter = new MemoryAdapter();
test('discord cache', () => {
const client = new Client({
getRC: () => ({
locations: {
base: '',
output: '',
},
intents,
token: '',
}),
});
client.setServices({
cache: {
adapter,
},
});
return client.cache.testAdapter();
});
});
describe('test limited memory cache adapter', () => {
const adapter = new LimitedMemoryAdapter();
test('discord cache', () => {
const client = new Client({
getRC: () => ({
locations: {
base: '',
output: '',
},
intents,
token: '',
}),
});
client.setServices({
cache: {
adapter,
},
});
return client.cache.testAdapter();
});
});