Skip to content

Commit 144cbfc

Browse files
committed
accept arrays in item size calculation
1 parent 25766e7 commit 144cbfc

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

packages/node/src/utils/redisCache.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,22 @@ export function shouldConsiderForCache(redisCommand: string, key: string, prefix
6969

7070
/** Calculates size based on the cache response value */
7171
export function calculateCacheItemSize(response: unknown): number | undefined {
72-
try {
73-
if (Buffer.isBuffer(response)) return response.byteLength;
74-
else if (typeof response === 'string') return response.length;
75-
else if (typeof response === 'number') return response.toString().length;
76-
else if (response === null || response === undefined) return 0;
77-
return JSON.stringify(response).length;
78-
} catch (e) {
79-
return undefined;
80-
}
72+
const getSize = (value: unknown): number | undefined => {
73+
try {
74+
if (Buffer.isBuffer(value)) return value.byteLength;
75+
else if (typeof value === 'string') return value.length;
76+
else if (typeof value === 'number') return value.toString().length;
77+
else if (value === null || value === undefined) return 0;
78+
return JSON.stringify(value).length;
79+
} catch (e) {
80+
return undefined;
81+
}
82+
};
83+
84+
return Array.isArray(response)
85+
? response.reduce((acc: number | undefined, curr) => {
86+
const size = getSize(curr);
87+
return typeof size === 'number' ? (acc !== undefined ? acc + size : size) : acc;
88+
}, 0)
89+
: getSize(response);
8190
}

packages/node/test/integrations/tracing/redis.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,50 @@ describe('Redis', () => {
180180
});
181181
});
182182
});
183+
184+
describe('calculateCacheItemSize', () => {
185+
it('should return byte length for Buffer input', () => {
186+
const buffer = Buffer.from('test');
187+
const result = calculateCacheItemSize(buffer);
188+
expect(result).toBe(4);
189+
});
190+
191+
it('should return string length for string input', () => {
192+
const str = 'test';
193+
const result = calculateCacheItemSize(str);
194+
expect(result).toBe(4);
195+
});
196+
197+
it('should return number length for number input', () => {
198+
const num = 1234;
199+
const result = calculateCacheItemSize(num);
200+
expect(result).toBe(4);
201+
});
202+
203+
it('should return 0 for null or undefined input', () => {
204+
const resultForNull = calculateCacheItemSize(null);
205+
const resultForUndefined = calculateCacheItemSize(undefined);
206+
expect(resultForNull).toBe(0);
207+
expect(resultForUndefined).toBe(0);
208+
});
209+
210+
it('should return total size for array input', () => {
211+
const arr = ['test', Buffer.from('test'), 1234];
212+
const result = calculateCacheItemSize(arr);
213+
expect(result).toBe(12);
214+
});
215+
216+
it('should return JSON string length for object input', () => {
217+
const obj = { key: 'value' };
218+
const result = calculateCacheItemSize(obj);
219+
expect(result).toBe(15);
220+
});
221+
222+
it('should return undefined for circular objects', () => {
223+
const circularObject: { self?: any } = {};
224+
circularObject.self = circularObject; // This creates a circular reference
225+
const result = calculateCacheItemSize(circularObject);
226+
expect(result).toBeUndefined();
227+
});
228+
});
183229
});

0 commit comments

Comments
 (0)