Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/RedisStringsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { commandOptions, createClient, RedisClientOptions } from 'redis';
import { SyncedMap } from './SyncedMap';
import { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';
import { debug } from './utils/debug';
import { bufferReviver, bufferReplacer } from './utils/json';
import { bufferAndMapReviver, bufferAndMapReplacer } from './utils/json';

export type CommandOptions = ReturnType<typeof commandOptions>;
export type Client = ReturnType<typeof createClient>;
Expand Down Expand Up @@ -405,7 +405,7 @@ export default class RedisStringsHandler {

const cacheEntry: CacheEntry | null = JSON.parse(
serializedCacheEntry,
bufferReviver,
bufferAndMapReviver,
);

debug(
Expand Down Expand Up @@ -606,7 +606,10 @@ export default class RedisStringsHandler {
tags: ctx?.tags || [],
value: data,
};
const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);
const serializedCacheEntry = JSON.stringify(
cacheEntry,
bufferAndMapReplacer,
);

// pre seed data into deduplicated get client. This will reduce redis load by not requesting
// the same value from redis which was just set.
Expand Down
27 changes: 25 additions & 2 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function bufferReviver(_: string, value: any): any {
export function bufferAndMapReviver(_: string, value: any): any {
if (value && typeof value === 'object' && typeof value.$binary === 'string') {
return Buffer.from(value.$binary, 'base64');
}
if (
value &&
typeof value === 'object' &&
typeof value.$map === 'object' &&
!!value.$map
) {
return new Map(
Object.entries(value.$map).map(([key, value]) => {
const revivedValue = bufferAndMapReviver('', value);
return [key, revivedValue];
}),
);
}
return value;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function bufferReplacer(_: string, value: any): any {
export function bufferAndMapReplacer(_: string, value: any): any {
if (Buffer.isBuffer(value)) {
return {
$binary: value.toString('base64'),
Expand All @@ -22,5 +35,15 @@ export function bufferReplacer(_: string, value: any): any {
$binary: Buffer.from(value.data).toString('base64'),
};
}
if (value && typeof value === 'object' && value instanceof Map) {
return {
$map: Object.fromEntries(
Array.from(value.entries()).map(([key, value]) => {
const replacedValue = bufferAndMapReplacer('', value);
return [key, replacedValue];
}),
),
};
}
return value;
}
Loading