Repository: FlowiseAI/Flowise
File: packages/components/nodes/cache/RedisCache/RedisCache.ts (TypeScript)

Code before:

        return redisClient
    }
}
const getRedisClient = async (nodeData: INodeData, options: ICommonObject) => {
    let client: Redis

    const credentialData = await getCredentialData(nodeData.credential ?? '', options)
    const redisUrl = getCredentialParam('redisUrl', credentialData, nodeData)

    if (!redisUrl || redisUrl === '') {
        const username = getCredentialParam('redisCacheUser', credentialData, nodeData)
        const password = getCredentialParam('redisCachePwd', credentialData, nodeData)
        const portStr = getCredentialParam('redisCachePort', credentialData, nodeData)
        const host = getCredentialParam('redisCacheHost', credentialData, nodeData)
        const sslEnabled = getCredentialParam('redisCacheSslEnabled', credentialData, nodeData)

        const tlsOptions = sslEnabled === true ? { tls: { rejectUnauthorized: false } } : {}

        client = new Redis({
            port: portStr ? parseInt(portStr) : 6379,
            host,
            username,
            password,
            ...tlsOptions,
            keepAlive: 60000,
            retryStrategy: (times) => Math.min(times * 100, 3000),
        })
    } else {
        client = new Redis(redisUrl, {
            keepAlive: 60000,
            retryStrategy: (times) => Math.min(times * 100, 3000),
        })
    }

    return client
}
const getCacheKey = (...strings: string[]): string => hash(strings.join('_'))
const deserializeStoredGeneration = (storedGeneration: StoredGeneration) => {
    if (storedGeneration.message !== undefined) {
        return {
            text: storedGeneration.text,
            message: mapStoredMessageToChatMessage(storedGeneration.message)
        }
    } else {
        return { text: storedGeneration.text }
    }
}
const serializeGeneration = (generation: Generation) => {
    const serializedValue: StoredGeneration = {
        text: generation.text

Review comment (suggestion):
instead of hardcoding the values, can we have:

`REDIS_KEEP_ALIVE` env variable? `keepAlive: process.env.REDIS_KEEP_ALIVE ?? undefined,`

Code after:

        return redisClient
    }
}
const getRedisClient = async (nodeData: INodeData, options: ICommonObject) => {
    let client: Redis

    const credentialData = await getCredentialData(nodeData.credential ?? '', options)
    const redisUrl = getCredentialParam('redisUrl', credentialData, nodeData)

    if (!redisUrl || redisUrl === '') {
        const username = getCredentialParam('redisCacheUser', credentialData, nodeData)
        const password = getCredentialParam('redisCachePwd', credentialData, nodeData)
        const portStr = getCredentialParam('redisCachePort', credentialData, nodeData)
        const host = getCredentialParam('redisCacheHost', credentialData, nodeData)
        const sslEnabled = getCredentialParam('redisCacheSslEnabled', credentialData, nodeData)

        const tlsOptions = sslEnabled === true ? { tls: { rejectUnauthorized: false } } : {}

        client = new Redis({
            port: portStr ? parseInt(portStr) : 6379,
            host,
            username,
            password,
            keepAlive:
                process.env.REDIS_KEEP_ALIVE && !isNaN(parseInt(process.env.REDIS_KEEP_ALIVE, 10))
                    ? parseInt(process.env.REDIS_KEEP_ALIVE, 10)
                    : undefined,
            ...tlsOptions
        })
    } else {
        client = new Redis(redisUrl, {
            keepAlive:
                process.env.REDIS_KEEP_ALIVE && !isNaN(parseInt(process.env.REDIS_KEEP_ALIVE, 10))
                    ? parseInt(process.env.REDIS_KEEP_ALIVE, 10)
                    : undefined
        })
    }

    return client
}
const getCacheKey = (...strings: string[]): string => hash(strings.join('_'))
const deserializeStoredGeneration = (storedGeneration: StoredGeneration) => {
    if (storedGeneration.message !== undefined) {
        return {
            text: storedGeneration.text,
            message: mapStoredMessageToChatMessage(storedGeneration.message)
        }
    } else {
        return { text: storedGeneration.text }
    }