(nodeData: INodeData, _: string, options: ICommonObject)
| 46 | } |
| 47 | |
| 48 | async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 49 | const ttl = nodeData.inputs?.ttl as string |
| 50 | |
| 51 | let client = await getRedisClient(nodeData, options) |
| 52 | const redisClient = new LangchainRedisCache(client) |
| 53 | |
| 54 | redisClient.lookup = async (prompt: string, llmKey: string) => { |
| 55 | try { |
| 56 | const pingResp = await client.ping() |
| 57 | if (pingResp !== 'PONG') { |
| 58 | client = await getRedisClient(nodeData, options) |
| 59 | } |
| 60 | } catch (error) { |
| 61 | client = await getRedisClient(nodeData, options) |
| 62 | } |
| 63 | |
| 64 | let idx = 0 |
| 65 | let key = getCacheKey(prompt, llmKey, String(idx)) |
| 66 | let value = await client.get(key) |
| 67 | const generations: Generation[] = [] |
| 68 | |
| 69 | while (value) { |
| 70 | const storedGeneration = JSON.parse(value) |
| 71 | generations.push(deserializeStoredGeneration(storedGeneration)) |
| 72 | idx += 1 |
| 73 | key = getCacheKey(prompt, llmKey, String(idx)) |
| 74 | value = await client.get(key) |
| 75 | } |
| 76 | |
| 77 | client.quit() |
| 78 | |
| 79 | return generations.length > 0 ? generations : null |
| 80 | } |
| 81 | |
| 82 | redisClient.update = async (prompt: string, llmKey: string, value: Generation[]) => { |
| 83 | try { |
| 84 | const pingResp = await client.ping() |
| 85 | if (pingResp !== 'PONG') { |
| 86 | client = await getRedisClient(nodeData, options) |
| 87 | } |
| 88 | } catch (error) { |
| 89 | client = await getRedisClient(nodeData, options) |
| 90 | } |
| 91 | |
| 92 | for (let i = 0; i < value.length; i += 1) { |
| 93 | const key = getCacheKey(prompt, llmKey, String(i)) |
| 94 | if (ttl) { |
| 95 | await client.set(key, JSON.stringify(serializeGeneration(value[i])), 'PX', parseInt(ttl, 10)) |
| 96 | } else { |
| 97 | await client.set(key, JSON.stringify(serializeGeneration(value[i]))) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | client.quit() |
| 102 | } |
| 103 | |
| 104 | client.quit() |
| 105 |
nothing calls this directly
no test coverage detected