mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-04 05:56:09 +00:00
feat: endcode & decode cache adapters option
This commit is contained in:
parent
449be8ea38
commit
c3ea1a394a
34
src/cache/adapters/default.ts
vendored
34
src/cache/adapters/default.ts
vendored
@ -1,11 +1,27 @@
|
|||||||
import type { Adapter } from './types';
|
import type { Adapter } from './types';
|
||||||
|
|
||||||
export class MemoryAdapter implements Adapter {
|
export interface MemoryAdapterOptions<T> {
|
||||||
|
encode(data: any): T;
|
||||||
|
decode(data: T): unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MemoryAdapter<T> implements Adapter {
|
||||||
isAsync = false;
|
isAsync = false;
|
||||||
|
|
||||||
readonly storage = new Map<string, string>();
|
readonly storage = new Map<string, T>();
|
||||||
readonly relationships = new Map<string, string[]>();
|
readonly relationships = new Map<string, string[]>();
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public options: MemoryAdapterOptions<T> = {
|
||||||
|
encode(data) {
|
||||||
|
return JSON.stringify(data) as T;
|
||||||
|
},
|
||||||
|
decode(data) {
|
||||||
|
return JSON.parse(data as string);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
scan(query: string, keys?: false): any[];
|
scan(query: string, keys?: false): any[];
|
||||||
scan(query: string, keys: true): string[];
|
scan(query: string, keys: true): string[];
|
||||||
scan(query: string, keys = false) {
|
scan(query: string, keys = false) {
|
||||||
@ -13,7 +29,7 @@ export class MemoryAdapter implements Adapter {
|
|||||||
const sq = query.split('.');
|
const sq = query.split('.');
|
||||||
for (const [key, value] of this.storage.entries()) {
|
for (const [key, value] of this.storage.entries()) {
|
||||||
if (key.split('.').every((value, i) => (sq[i] === '*' ? !!value : sq[i] === value))) {
|
if (key.split('.').every((value, i) => (sq[i] === '*' ? !!value : sq[i] === value))) {
|
||||||
values.push(keys ? key : JSON.parse(value));
|
values.push(keys ? key : this.options.decode(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,24 +40,24 @@ export class MemoryAdapter implements Adapter {
|
|||||||
return keys
|
return keys
|
||||||
.map(x => {
|
.map(x => {
|
||||||
const data = this.storage.get(x);
|
const data = this.storage.get(x);
|
||||||
return data ? JSON.parse(data) : null;
|
return data ? this.options.decode(data) : null;
|
||||||
})
|
})
|
||||||
.filter(x => x);
|
.filter(x => x);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(keys: string) {
|
get(keys: string) {
|
||||||
const data = this.storage.get(keys);
|
const data = this.storage.get(keys);
|
||||||
return data ? JSON.parse(data) : null;
|
return data ? this.options.decode(data) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkSet(keys: [string, any][]) {
|
bulkSet(keys: [string, any][]) {
|
||||||
for (const [key, value] of keys) {
|
for (const [key, value] of keys) {
|
||||||
this.storage.set(key, JSON.stringify(value));
|
this.storage.set(key, this.options.encode(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key: string, data: any) {
|
set(key: string, data: any) {
|
||||||
this.storage.set(key, JSON.stringify(data));
|
this.storage.set(key, this.options.encode(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkPatch(updateOnly: boolean, keys: [string, any][]) {
|
bulkPatch(updateOnly: boolean, keys: [string, any][]) {
|
||||||
@ -52,7 +68,7 @@ export class MemoryAdapter implements Adapter {
|
|||||||
}
|
}
|
||||||
this.storage.set(
|
this.storage.set(
|
||||||
key,
|
key,
|
||||||
Array.isArray(value) ? JSON.stringify(value) : JSON.stringify({ ...(oldData ?? {}), ...value }),
|
Array.isArray(value) ? this.options.encode(value) : this.options.encode({ ...(oldData ?? {}), ...value }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,7 +80,7 @@ export class MemoryAdapter implements Adapter {
|
|||||||
}
|
}
|
||||||
this.storage.set(
|
this.storage.set(
|
||||||
keys,
|
keys,
|
||||||
Array.isArray(data) ? JSON.stringify(data) : JSON.stringify({ ...(oldData ?? {}), ...data }),
|
Array.isArray(data) ? this.options.encode(data) : this.options.encode({ ...(oldData ?? {}), ...data }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
src/cache/adapters/limited.ts
vendored
35
src/cache/adapters/limited.ts
vendored
@ -7,7 +7,7 @@ export interface ResourceLimitedMemoryAdapter {
|
|||||||
limit?: number;
|
limit?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LimitedMemoryAdapterOptions {
|
export interface LimitedMemoryAdapterOptions<T> {
|
||||||
default?: ResourceLimitedMemoryAdapter;
|
default?: ResourceLimitedMemoryAdapter;
|
||||||
|
|
||||||
guild?: ResourceLimitedMemoryAdapter;
|
guild?: ResourceLimitedMemoryAdapter;
|
||||||
@ -26,24 +26,33 @@ export interface LimitedMemoryAdapterOptions {
|
|||||||
thread?: ResourceLimitedMemoryAdapter;
|
thread?: ResourceLimitedMemoryAdapter;
|
||||||
overwrite?: ResourceLimitedMemoryAdapter;
|
overwrite?: ResourceLimitedMemoryAdapter;
|
||||||
message?: ResourceLimitedMemoryAdapter;
|
message?: ResourceLimitedMemoryAdapter;
|
||||||
|
|
||||||
|
encode(data: any): T;
|
||||||
|
decode(data: T): unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LimitedMemoryAdapter implements Adapter {
|
export class LimitedMemoryAdapter<T> implements Adapter {
|
||||||
isAsync = false;
|
isAsync = false;
|
||||||
|
|
||||||
readonly storage = new Map<string, LimitedCollection<string, string>>();
|
readonly storage = new Map<string, LimitedCollection<string, T>>();
|
||||||
readonly relationships = new Map<string, Map<string, string[]>>();
|
readonly relationships = new Map<string, Map<string, string[]>>();
|
||||||
|
|
||||||
options: MakeRequired<LimitedMemoryAdapterOptions, 'default'>;
|
options: MakeRequired<LimitedMemoryAdapterOptions<T>, 'default'>;
|
||||||
|
|
||||||
constructor(options: LimitedMemoryAdapterOptions) {
|
constructor(options: LimitedMemoryAdapterOptions<T>) {
|
||||||
this.options = MergeOptions(
|
this.options = MergeOptions(
|
||||||
{
|
{
|
||||||
default: {
|
default: {
|
||||||
expire: undefined,
|
expire: undefined,
|
||||||
limit: Number.POSITIVE_INFINITY,
|
limit: Number.POSITIVE_INFINITY,
|
||||||
},
|
},
|
||||||
} satisfies LimitedMemoryAdapterOptions,
|
encode(data) {
|
||||||
|
return JSON.stringify(data) as T;
|
||||||
|
},
|
||||||
|
decode(data) {
|
||||||
|
return JSON.parse(data as string);
|
||||||
|
},
|
||||||
|
} satisfies LimitedMemoryAdapterOptions<T>,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -56,7 +65,7 @@ export class LimitedMemoryAdapter implements Adapter {
|
|||||||
for (const iterator of [...this.storage.values()].flatMap(x => x.entries()))
|
for (const iterator of [...this.storage.values()].flatMap(x => x.entries()))
|
||||||
for (const [key, value] of iterator) {
|
for (const [key, value] of iterator) {
|
||||||
if (key.split('.').every((value, i) => (sq[i] === '*' ? !!value : sq[i] === value))) {
|
if (key.split('.').every((value, i) => (sq[i] === '*' ? !!value : sq[i] === value))) {
|
||||||
values.push(keys ? key : JSON.parse(value.value));
|
values.push(keys ? key : this.options.decode(value.value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,14 +77,14 @@ export class LimitedMemoryAdapter implements Adapter {
|
|||||||
return keys
|
return keys
|
||||||
.map(key => {
|
.map(key => {
|
||||||
const data = iterator.find(x => x.has(key))?.get(key);
|
const data = iterator.find(x => x.has(key))?.get(key);
|
||||||
return data ? JSON.parse(data) : null;
|
return data ? this.options.decode(data) : null;
|
||||||
})
|
})
|
||||||
.filter(x => x);
|
.filter(x => x);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(keys: string) {
|
get(keys: string) {
|
||||||
const data = [...this.storage.values()].find(x => x.has(keys))?.get(keys);
|
const data = [...this.storage.values()].find(x => x.has(keys))?.get(keys);
|
||||||
return data ? JSON.parse(data) : null;
|
return data ? this.options.decode(data) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private __set(key: string, data: any) {
|
private __set(key: string, data: any) {
|
||||||
@ -87,9 +96,11 @@ export class LimitedMemoryAdapter implements Adapter {
|
|||||||
namespace,
|
namespace,
|
||||||
new LimitedCollection({
|
new LimitedCollection({
|
||||||
expire:
|
expire:
|
||||||
this.options[key.split('.')[0] as keyof LimitedMemoryAdapterOptions]?.expire ?? this.options.default.expire,
|
this.options[key.split('.')[0] as Exclude<keyof LimitedMemoryAdapterOptions<T>, 'decode' | 'encode'>]
|
||||||
|
?.expire ?? this.options.default.expire,
|
||||||
limit:
|
limit:
|
||||||
this.options[key.split('.')[0] as keyof LimitedMemoryAdapterOptions]?.limit ?? this.options.default.limit,
|
this.options[key.split('.')[0] as Exclude<keyof LimitedMemoryAdapterOptions<T>, 'decode' | 'encode'>]
|
||||||
|
?.limit ?? this.options.default.limit,
|
||||||
resetOnDemand: true,
|
resetOnDemand: true,
|
||||||
onDelete(k) {
|
onDelete(k) {
|
||||||
const relationshipNamespace = key.split('.')[0];
|
const relationshipNamespace = key.split('.')[0];
|
||||||
@ -126,7 +137,7 @@ export class LimitedMemoryAdapter implements Adapter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.storage.get(namespace)!.set(key, JSON.stringify(data));
|
this.storage.get(namespace)!.set(key, this.options.encode(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkSet(keys: [string, any][]) {
|
bulkSet(keys: [string, any][]) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user