11import { REVALIDATED_TAGS_KEY } from "../constants" ;
22import { isImplicitTag } from "../helpers/isImplicitTag" ;
33import { CacheHandlerValue , Handler } from "./cache-handler.types" ;
4- import { CreateRedisStringsHandlerOptions } from "./redis-strings.types" ;
4+ import {
5+ type CacheValueSerializer ,
6+ type CreateRedisStringsHandlerOptions ,
7+ } from "./redis-strings.types" ;
58import {
69 convertStringsToBuffers ,
710 parseBuffersToStrings ,
@@ -10,6 +13,19 @@ import type { RedisClientType } from "@redis/client";
1013import { RedisClusterCacheAdapter } from "../helpers/redisClusterAdapter" ;
1114import { withAbortSignalProxy } from "../helpers/withAbortSignalProxy" ;
1215
16+ export type { CacheValueSerializer } from "./redis-strings.types" ;
17+
18+ export const jsonCacheValueSerializer : CacheValueSerializer = {
19+ serialize ( value ) {
20+ return JSON . stringify ( value ) ;
21+ } ,
22+ deserialize ( stored ) {
23+ return JSON . parse (
24+ typeof stored === "string" ? stored : String ( stored ) ,
25+ ) as CacheHandlerValue | null ;
26+ } ,
27+ } ;
28+
1329/**
1430 * Creates a Handler for handling cache operations using Redis strings.
1531 *
@@ -33,6 +49,7 @@ export default function createHandler({
3349 timeoutMs = 5_000 ,
3450 keyExpirationStrategy = "EXPIREAT" ,
3551 revalidateTagQuerySize = 10_000 ,
52+ valueSerializer = jsonCacheValueSerializer ,
3653} : CreateRedisStringsHandlerOptions <
3754 RedisClientType | RedisClusterCacheAdapter
3855> ) : Handler {
@@ -176,7 +193,7 @@ export default function createHandler({
176193 return null ;
177194 }
178195
179- const cacheValue = JSON . parse ( result ) as CacheHandlerValue | null ;
196+ const cacheValue = valueSerializer . deserialize ( result ) ;
180197
181198 if ( ! cacheValue ) {
182199 return null ;
@@ -237,7 +254,7 @@ export default function createHandler({
237254 parseBuffersToStrings ( { ...cacheHandlerValue , value : valueForStorage } ) ;
238255 }
239256
240- const serializedValue = JSON . stringify ( {
257+ const serializedValue = valueSerializer . serialize ( {
241258 ...cacheHandlerValue ,
242259 value : valueForStorage ,
243260 } ) ;
0 commit comments