From dd84b9c7f38811724fe806b555fa6e3426cd254e Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Sat, 4 Jun 2022 07:04:33 +0200 Subject: [PATCH] fix(redis): valid integer for ttl --- lib/util/cache/package/redis.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/util/cache/package/redis.ts b/lib/util/cache/package/redis.ts index 3cbfaa09b6..17659c05b1 100644 --- a/lib/util/cache/package/redis.ts +++ b/lib/util/cache/package/redis.ts @@ -1,4 +1,5 @@ /* istanbul ignore file */ +import is from '@sindresorhus/is'; import { DateTime } from 'luxon'; import { createClient } from 'redis'; import { logger } from '../../../logger'; @@ -54,14 +55,22 @@ export async function set( value: unknown, ttlMinutes = 5 ): Promise<void> { - logger.trace({ namespace, key, ttlMinutes }, 'Saving cached value'); + let minutes = ttlMinutes; + if (!is.integer(minutes)) { + if (is.number(minutes)) { + minutes = Math.floor(minutes); + } else { + minutes = 5; + } + } + logger.trace({ namespace, key, minutes }, 'Saving cached value'); await client?.set( getKey(namespace, key), JSON.stringify({ value, - expiry: DateTime.local().plus({ minutes: ttlMinutes }), + expiry: DateTime.local().plus({ minutes }), }), - { EX: ttlMinutes * 60 } + { EX: minutes * 60 } ); } -- GitLab